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