applesmc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
  3. * sensors, fan control, keyboard backlight control) used in Intel-based Apple
  4. * computers.
  5. *
  6. * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
  7. * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
  8. *
  9. * Based on hdaps.c driver:
  10. * Copyright (C) 2005 Robert Love <rml@novell.com>
  11. * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
  12. *
  13. * Fan control based on smcFanControl:
  14. * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License v2 as published by the
  18. * Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but WITHOUT
  21. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  23. * more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along with
  26. * this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/input-polldev.h>
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <linux/timer.h>
  37. #include <linux/dmi.h>
  38. #include <linux/mutex.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/io.h>
  41. #include <linux/leds.h>
  42. #include <linux/hwmon.h>
  43. #include <linux/workqueue.h>
  44. /* data port used by Apple SMC */
  45. #define APPLESMC_DATA_PORT 0x300
  46. /* command/status port used by Apple SMC */
  47. #define APPLESMC_CMD_PORT 0x304
  48. #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
  49. #define APPLESMC_MAX_DATA_LENGTH 32
  50. /* wait up to 32 ms for a status change. */
  51. #define APPLESMC_MIN_WAIT 0x0040
  52. #define APPLESMC_MAX_WAIT 0x8000
  53. #define APPLESMC_STATUS_MASK 0x0f
  54. #define APPLESMC_READ_CMD 0x10
  55. #define APPLESMC_WRITE_CMD 0x11
  56. #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
  57. #define APPLESMC_GET_KEY_TYPE_CMD 0x13
  58. #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
  59. #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
  60. #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
  61. #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
  62. #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
  63. #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
  64. #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
  65. #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
  66. #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
  67. #define FANS_COUNT "FNum" /* r-o ui8 */
  68. #define FANS_MANUAL "FS! " /* r-w ui16 */
  69. #define FAN_ID_FMT "F%dID" /* r-o char[16] */
  70. /* List of keys used to read/write fan speeds */
  71. static const char *const fan_speed_fmt[] = {
  72. "F%dAc", /* actual speed */
  73. "F%dMn", /* minimum speed (rw) */
  74. "F%dMx", /* maximum speed */
  75. "F%dSf", /* safe speed - not all models */
  76. "F%dTg", /* target speed (manual: rw) */
  77. };
  78. #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
  79. #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
  80. #define APPLESMC_POLL_INTERVAL 50 /* msecs */
  81. #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
  82. #define APPLESMC_INPUT_FLAT 4
  83. #define SENSOR_X 0
  84. #define SENSOR_Y 1
  85. #define SENSOR_Z 2
  86. #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
  87. #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
  88. /* Dynamic device node attributes */
  89. struct applesmc_dev_attr {
  90. struct sensor_device_attribute sda; /* hwmon attributes */
  91. char name[32]; /* room for node file name */
  92. };
  93. /* Dynamic device node group */
  94. struct applesmc_node_group {
  95. char *format; /* format string */
  96. void *show; /* show function */
  97. void *store; /* store function */
  98. int option; /* function argument */
  99. struct applesmc_dev_attr *nodes; /* dynamic node array */
  100. };
  101. /* AppleSMC entry - cached register information */
  102. struct applesmc_entry {
  103. char key[5]; /* four-letter key code */
  104. u8 valid; /* set when entry is successfully read once */
  105. u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
  106. char type[5]; /* four-letter type code */
  107. u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
  108. };
  109. /* Register lookup and registers common to all SMCs */
  110. static struct applesmc_registers {
  111. struct mutex mutex; /* register read/write mutex */
  112. unsigned int key_count; /* number of SMC registers */
  113. unsigned int fan_count; /* number of fans */
  114. unsigned int temp_count; /* number of temperature registers */
  115. unsigned int temp_begin; /* temperature lower index bound */
  116. unsigned int temp_end; /* temperature upper index bound */
  117. int num_light_sensors; /* number of light sensors */
  118. bool has_accelerometer; /* has motion sensor */
  119. bool has_key_backlight; /* has keyboard backlight */
  120. bool init_complete; /* true when fully initialized */
  121. struct applesmc_entry *cache; /* cached key entries */
  122. } smcreg = {
  123. .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
  124. };
  125. static const int debug;
  126. static struct platform_device *pdev;
  127. static s16 rest_x;
  128. static s16 rest_y;
  129. static u8 backlight_state[2];
  130. static struct device *hwmon_dev;
  131. static struct input_polled_dev *applesmc_idev;
  132. /*
  133. * Last index written to key_at_index sysfs file, and value to use for all other
  134. * key_at_index_* sysfs files.
  135. */
  136. static unsigned int key_at_index;
  137. static struct workqueue_struct *applesmc_led_wq;
  138. /*
  139. * __wait_status - Wait up to 32ms for the status port to get a certain value
  140. * (masked with 0x0f), returning zero if the value is obtained. Callers must
  141. * hold applesmc_lock.
  142. */
  143. static int __wait_status(u8 val)
  144. {
  145. int us;
  146. val = val & APPLESMC_STATUS_MASK;
  147. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  148. udelay(us);
  149. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val)
  150. return 0;
  151. }
  152. return -EIO;
  153. }
  154. /*
  155. * special treatment of command port - on newer macbooks, it seems necessary
  156. * to resend the command byte before polling the status again. Callers must
  157. * hold applesmc_lock.
  158. */
  159. static int send_command(u8 cmd)
  160. {
  161. int us;
  162. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  163. outb(cmd, APPLESMC_CMD_PORT);
  164. udelay(us);
  165. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
  166. return 0;
  167. }
  168. return -EIO;
  169. }
  170. static int send_argument(const char *key)
  171. {
  172. int i;
  173. for (i = 0; i < 4; i++) {
  174. outb(key[i], APPLESMC_DATA_PORT);
  175. if (__wait_status(0x04))
  176. return -EIO;
  177. }
  178. return 0;
  179. }
  180. static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
  181. {
  182. u8 status, data = 0;
  183. int i;
  184. if (send_command(cmd) || send_argument(key)) {
  185. pr_warn("%.4s: read arg fail\n", key);
  186. return -EIO;
  187. }
  188. /* This has no effect on newer (2012) SMCs */
  189. outb(len, APPLESMC_DATA_PORT);
  190. for (i = 0; i < len; i++) {
  191. if (__wait_status(0x05)) {
  192. pr_warn("%.4s: read data fail\n", key);
  193. return -EIO;
  194. }
  195. buffer[i] = inb(APPLESMC_DATA_PORT);
  196. }
  197. /* Read the data port until bit0 is cleared */
  198. for (i = 0; i < 16; i++) {
  199. udelay(APPLESMC_MIN_WAIT);
  200. status = inb(APPLESMC_CMD_PORT);
  201. if (!(status & 0x01))
  202. break;
  203. data = inb(APPLESMC_DATA_PORT);
  204. }
  205. if (i)
  206. pr_warn("flushed %d bytes, last value is: %d\n", i, data);
  207. return 0;
  208. }
  209. static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
  210. {
  211. int i;
  212. if (send_command(cmd) || send_argument(key)) {
  213. pr_warn("%s: write arg fail\n", key);
  214. return -EIO;
  215. }
  216. outb(len, APPLESMC_DATA_PORT);
  217. for (i = 0; i < len; i++) {
  218. if (__wait_status(0x04)) {
  219. pr_warn("%s: write data fail\n", key);
  220. return -EIO;
  221. }
  222. outb(buffer[i], APPLESMC_DATA_PORT);
  223. }
  224. return 0;
  225. }
  226. static int read_register_count(unsigned int *count)
  227. {
  228. __be32 be;
  229. int ret;
  230. ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
  231. if (ret)
  232. return ret;
  233. *count = be32_to_cpu(be);
  234. return 0;
  235. }
  236. /*
  237. * Serialized I/O
  238. *
  239. * Returns zero on success or a negative error on failure.
  240. * All functions below are concurrency safe - callers should NOT hold lock.
  241. */
  242. static int applesmc_read_entry(const struct applesmc_entry *entry,
  243. u8 *buf, u8 len)
  244. {
  245. int ret;
  246. if (entry->len != len)
  247. return -EINVAL;
  248. mutex_lock(&smcreg.mutex);
  249. ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
  250. mutex_unlock(&smcreg.mutex);
  251. return ret;
  252. }
  253. static int applesmc_write_entry(const struct applesmc_entry *entry,
  254. const u8 *buf, u8 len)
  255. {
  256. int ret;
  257. if (entry->len != len)
  258. return -EINVAL;
  259. mutex_lock(&smcreg.mutex);
  260. ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
  261. mutex_unlock(&smcreg.mutex);
  262. return ret;
  263. }
  264. static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
  265. {
  266. struct applesmc_entry *cache = &smcreg.cache[index];
  267. u8 key[4], info[6];
  268. __be32 be;
  269. int ret = 0;
  270. if (cache->valid)
  271. return cache;
  272. mutex_lock(&smcreg.mutex);
  273. if (cache->valid)
  274. goto out;
  275. be = cpu_to_be32(index);
  276. ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
  277. if (ret)
  278. goto out;
  279. ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
  280. if (ret)
  281. goto out;
  282. memcpy(cache->key, key, 4);
  283. cache->len = info[0];
  284. memcpy(cache->type, &info[1], 4);
  285. cache->flags = info[5];
  286. cache->valid = 1;
  287. out:
  288. mutex_unlock(&smcreg.mutex);
  289. if (ret)
  290. return ERR_PTR(ret);
  291. return cache;
  292. }
  293. static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
  294. {
  295. int begin = 0, end = smcreg.key_count;
  296. const struct applesmc_entry *entry;
  297. while (begin != end) {
  298. int middle = begin + (end - begin) / 2;
  299. entry = applesmc_get_entry_by_index(middle);
  300. if (IS_ERR(entry)) {
  301. *lo = 0;
  302. return PTR_ERR(entry);
  303. }
  304. if (strcmp(entry->key, key) < 0)
  305. begin = middle + 1;
  306. else
  307. end = middle;
  308. }
  309. *lo = begin;
  310. return 0;
  311. }
  312. static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
  313. {
  314. int begin = 0, end = smcreg.key_count;
  315. const struct applesmc_entry *entry;
  316. while (begin != end) {
  317. int middle = begin + (end - begin) / 2;
  318. entry = applesmc_get_entry_by_index(middle);
  319. if (IS_ERR(entry)) {
  320. *hi = smcreg.key_count;
  321. return PTR_ERR(entry);
  322. }
  323. if (strcmp(key, entry->key) < 0)
  324. end = middle;
  325. else
  326. begin = middle + 1;
  327. }
  328. *hi = begin;
  329. return 0;
  330. }
  331. static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
  332. {
  333. int begin, end;
  334. int ret;
  335. ret = applesmc_get_lower_bound(&begin, key);
  336. if (ret)
  337. return ERR_PTR(ret);
  338. ret = applesmc_get_upper_bound(&end, key);
  339. if (ret)
  340. return ERR_PTR(ret);
  341. if (end - begin != 1)
  342. return ERR_PTR(-EINVAL);
  343. return applesmc_get_entry_by_index(begin);
  344. }
  345. static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
  346. {
  347. const struct applesmc_entry *entry;
  348. entry = applesmc_get_entry_by_key(key);
  349. if (IS_ERR(entry))
  350. return PTR_ERR(entry);
  351. return applesmc_read_entry(entry, buffer, len);
  352. }
  353. static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
  354. {
  355. const struct applesmc_entry *entry;
  356. entry = applesmc_get_entry_by_key(key);
  357. if (IS_ERR(entry))
  358. return PTR_ERR(entry);
  359. return applesmc_write_entry(entry, buffer, len);
  360. }
  361. static int applesmc_has_key(const char *key, bool *value)
  362. {
  363. const struct applesmc_entry *entry;
  364. entry = applesmc_get_entry_by_key(key);
  365. if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
  366. return PTR_ERR(entry);
  367. *value = !IS_ERR(entry);
  368. return 0;
  369. }
  370. /*
  371. * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
  372. */
  373. static int applesmc_read_motion_sensor(int index, s16 *value)
  374. {
  375. u8 buffer[2];
  376. int ret;
  377. switch (index) {
  378. case SENSOR_X:
  379. ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
  380. break;
  381. case SENSOR_Y:
  382. ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
  383. break;
  384. case SENSOR_Z:
  385. ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
  386. break;
  387. default:
  388. ret = -EINVAL;
  389. }
  390. *value = ((s16)buffer[0] << 8) | buffer[1];
  391. return ret;
  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. /*
  414. * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
  415. */
  416. static int applesmc_init_smcreg_try(void)
  417. {
  418. struct applesmc_registers *s = &smcreg;
  419. bool left_light_sensor, right_light_sensor;
  420. unsigned int count;
  421. u8 tmp[1];
  422. int ret;
  423. if (s->init_complete)
  424. return 0;
  425. ret = read_register_count(&count);
  426. if (ret)
  427. return ret;
  428. if (s->cache && s->key_count != count) {
  429. pr_warn("key count changed from %d to %d\n",
  430. s->key_count, count);
  431. kfree(s->cache);
  432. s->cache = NULL;
  433. }
  434. s->key_count = count;
  435. if (!s->cache)
  436. s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
  437. if (!s->cache)
  438. return -ENOMEM;
  439. ret = applesmc_read_key(FANS_COUNT, tmp, 1);
  440. if (ret)
  441. return ret;
  442. s->fan_count = tmp[0];
  443. ret = applesmc_get_lower_bound(&s->temp_begin, "T");
  444. if (ret)
  445. return ret;
  446. ret = applesmc_get_lower_bound(&s->temp_end, "U");
  447. if (ret)
  448. return ret;
  449. s->temp_count = s->temp_end - s->temp_begin;
  450. ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
  451. if (ret)
  452. return ret;
  453. ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
  454. if (ret)
  455. return ret;
  456. ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
  457. if (ret)
  458. return ret;
  459. ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
  460. if (ret)
  461. return ret;
  462. s->num_light_sensors = left_light_sensor + right_light_sensor;
  463. s->init_complete = true;
  464. pr_info("key=%d fan=%d temp=%d acc=%d lux=%d kbd=%d\n",
  465. s->key_count, s->fan_count, s->temp_count,
  466. s->has_accelerometer,
  467. s->num_light_sensors,
  468. s->has_key_backlight);
  469. return 0;
  470. }
  471. /*
  472. * applesmc_init_smcreg - Initialize register cache.
  473. *
  474. * Retries until initialization is successful, or the operation times out.
  475. *
  476. */
  477. static int applesmc_init_smcreg(void)
  478. {
  479. int ms, ret;
  480. for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
  481. ret = applesmc_init_smcreg_try();
  482. if (!ret) {
  483. if (ms)
  484. pr_info("init_smcreg() took %d ms\n", ms);
  485. return 0;
  486. }
  487. msleep(INIT_WAIT_MSECS);
  488. }
  489. kfree(smcreg.cache);
  490. smcreg.cache = NULL;
  491. return ret;
  492. }
  493. static void applesmc_destroy_smcreg(void)
  494. {
  495. kfree(smcreg.cache);
  496. smcreg.cache = NULL;
  497. smcreg.init_complete = false;
  498. }
  499. /* Device model stuff */
  500. static int applesmc_probe(struct platform_device *dev)
  501. {
  502. int ret;
  503. ret = applesmc_init_smcreg();
  504. if (ret)
  505. return ret;
  506. applesmc_device_init();
  507. return 0;
  508. }
  509. /* Synchronize device with memorized backlight state */
  510. static int applesmc_pm_resume(struct device *dev)
  511. {
  512. if (smcreg.has_key_backlight)
  513. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  514. return 0;
  515. }
  516. /* Reinitialize device on resume from hibernation */
  517. static int applesmc_pm_restore(struct device *dev)
  518. {
  519. applesmc_device_init();
  520. return applesmc_pm_resume(dev);
  521. }
  522. static const struct dev_pm_ops applesmc_pm_ops = {
  523. .resume = applesmc_pm_resume,
  524. .restore = applesmc_pm_restore,
  525. };
  526. static struct platform_driver applesmc_driver = {
  527. .probe = applesmc_probe,
  528. .driver = {
  529. .name = "applesmc",
  530. .owner = THIS_MODULE,
  531. .pm = &applesmc_pm_ops,
  532. },
  533. };
  534. /*
  535. * applesmc_calibrate - Set our "resting" values. Callers must
  536. * hold applesmc_lock.
  537. */
  538. static void applesmc_calibrate(void)
  539. {
  540. applesmc_read_motion_sensor(SENSOR_X, &rest_x);
  541. applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
  542. rest_x = -rest_x;
  543. }
  544. static void applesmc_idev_poll(struct input_polled_dev *dev)
  545. {
  546. struct input_dev *idev = dev->input;
  547. s16 x, y;
  548. if (applesmc_read_motion_sensor(SENSOR_X, &x))
  549. return;
  550. if (applesmc_read_motion_sensor(SENSOR_Y, &y))
  551. return;
  552. x = -x;
  553. input_report_abs(idev, ABS_X, x - rest_x);
  554. input_report_abs(idev, ABS_Y, y - rest_y);
  555. input_sync(idev);
  556. }
  557. /* Sysfs Files */
  558. static ssize_t applesmc_name_show(struct device *dev,
  559. struct device_attribute *attr, char *buf)
  560. {
  561. return snprintf(buf, PAGE_SIZE, "applesmc\n");
  562. }
  563. static ssize_t applesmc_position_show(struct device *dev,
  564. struct device_attribute *attr, char *buf)
  565. {
  566. int ret;
  567. s16 x, y, z;
  568. ret = applesmc_read_motion_sensor(SENSOR_X, &x);
  569. if (ret)
  570. goto out;
  571. ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
  572. if (ret)
  573. goto out;
  574. ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
  575. if (ret)
  576. goto out;
  577. out:
  578. if (ret)
  579. return ret;
  580. else
  581. return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
  582. }
  583. static ssize_t applesmc_light_show(struct device *dev,
  584. struct device_attribute *attr, char *sysfsbuf)
  585. {
  586. const struct applesmc_entry *entry;
  587. static int data_length;
  588. int ret;
  589. u8 left = 0, right = 0;
  590. u8 buffer[10];
  591. if (!data_length) {
  592. entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
  593. if (IS_ERR(entry))
  594. return PTR_ERR(entry);
  595. if (entry->len > 10)
  596. return -ENXIO;
  597. data_length = entry->len;
  598. pr_info("light sensor data length set to %d\n", data_length);
  599. }
  600. ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
  601. /* newer macbooks report a single 10-bit bigendian value */
  602. if (data_length == 10) {
  603. left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
  604. goto out;
  605. }
  606. left = buffer[2];
  607. if (ret)
  608. goto out;
  609. ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
  610. right = buffer[2];
  611. out:
  612. if (ret)
  613. return ret;
  614. else
  615. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
  616. }
  617. /* Displays sensor key as label */
  618. static ssize_t applesmc_show_sensor_label(struct device *dev,
  619. struct device_attribute *devattr, char *sysfsbuf)
  620. {
  621. int index = smcreg.temp_begin + to_index(devattr);
  622. const struct applesmc_entry *entry;
  623. entry = applesmc_get_entry_by_index(index);
  624. if (IS_ERR(entry))
  625. return PTR_ERR(entry);
  626. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
  627. }
  628. /* Displays degree Celsius * 1000 */
  629. static ssize_t applesmc_show_temperature(struct device *dev,
  630. struct device_attribute *devattr, char *sysfsbuf)
  631. {
  632. int index = smcreg.temp_begin + to_index(devattr);
  633. const struct applesmc_entry *entry;
  634. int ret;
  635. u8 buffer[2];
  636. unsigned int temp;
  637. entry = applesmc_get_entry_by_index(index);
  638. if (IS_ERR(entry))
  639. return PTR_ERR(entry);
  640. if (entry->len > 2)
  641. return -EINVAL;
  642. ret = applesmc_read_entry(entry, buffer, entry->len);
  643. if (ret)
  644. return ret;
  645. if (entry->len == 2) {
  646. temp = buffer[0] * 1000;
  647. temp += (buffer[1] >> 6) * 250;
  648. } else {
  649. temp = buffer[0] * 4000;
  650. }
  651. return snprintf(sysfsbuf, PAGE_SIZE, "%u\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.temp_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);