applesmc.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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. *
  8. * Based on hdaps.c driver:
  9. * Copyright (C) 2005 Robert Love <rml@novell.com>
  10. * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
  11. *
  12. * Fan control based on smcFanControl:
  13. * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License v2 as published by the
  17. * Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  22. * more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along with
  25. * this program; if not, write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/input-polldev.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/timer.h>
  34. #include <linux/dmi.h>
  35. #include <linux/mutex.h>
  36. #include <linux/hwmon-sysfs.h>
  37. #include <linux/io.h>
  38. #include <linux/leds.h>
  39. #include <linux/hwmon.h>
  40. #include <linux/workqueue.h>
  41. /* data port used by Apple SMC */
  42. #define APPLESMC_DATA_PORT 0x300
  43. /* command/status port used by Apple SMC */
  44. #define APPLESMC_CMD_PORT 0x304
  45. #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
  46. #define APPLESMC_MAX_DATA_LENGTH 32
  47. #define APPLESMC_MIN_WAIT 0x0040
  48. #define APPLESMC_MAX_WAIT 0x8000
  49. #define APPLESMC_STATUS_MASK 0x0f
  50. #define APPLESMC_READ_CMD 0x10
  51. #define APPLESMC_WRITE_CMD 0x11
  52. #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
  53. #define APPLESMC_GET_KEY_TYPE_CMD 0x13
  54. #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
  55. #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
  56. #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
  57. #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
  58. #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
  59. #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
  60. #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
  61. #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
  62. #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
  63. #define FANS_COUNT "FNum" /* r-o ui8 */
  64. #define FANS_MANUAL "FS! " /* r-w ui16 */
  65. #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
  66. #define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
  67. #define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
  68. #define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
  69. #define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
  70. #define FAN_POSITION "F0ID" /* r-o char[16] */
  71. /*
  72. * Temperature sensors keys (sp78 - 2 bytes).
  73. */
  74. static const char *temperature_sensors_sets[][41] = {
  75. /* Set 0: Macbook Pro */
  76. { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
  77. "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
  78. /* Set 1: Macbook2 set */
  79. { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H",
  80. "Th0S", "Th1H", NULL },
  81. /* Set 2: Macbook set */
  82. { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
  83. "Th1H", "Ts0P", NULL },
  84. /* Set 3: Macmini set */
  85. { "TC0D", "TC0P", NULL },
  86. /* Set 4: Mac Pro (2 x Quad-Core) */
  87. { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
  88. "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
  89. "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
  90. "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
  91. "TM9S", "TN0H", "TS0C", NULL },
  92. /* Set 5: iMac */
  93. { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P",
  94. "Tp0C", NULL },
  95. /* Set 6: Macbook3 set */
  96. { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H",
  97. "Th0S", "Th1H", NULL },
  98. /* Set 7: Macbook Air */
  99. { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
  100. "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
  101. /* Set 8: Macbook Pro 4,1 (Penryn) */
  102. { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H",
  103. "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
  104. /* Set 9: Macbook Pro 3,1 (Santa Rosa) */
  105. { "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P",
  106. "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
  107. /* Set 10: iMac 5,1 */
  108. { "TA0P", "TC0D", "TC0P", "TG0D", "TH0P", "TO0P", "Tm0P", NULL },
  109. /* Set 11: Macbook 5,1 */
  110. { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0P", "TN0D", "TN0P",
  111. "TTF0", "Th0H", "Th1H", "ThFH", "Ts0P", "Ts0S", NULL },
  112. /* Set 12: Macbook Pro 5,1 */
  113. { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
  114. "TG0F", "TG0H", "TG0P", "TG0T", "TG1H", "TN0D", "TN0P", "TTF0",
  115. "Th2H", "Tm0P", "Ts0P", "Ts0S", NULL },
  116. /* Set 13: iMac 8,1 */
  117. { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
  118. "TL0P", "TO0P", "TW0P", "Tm0P", "Tp0P", NULL },
  119. /* Set 14: iMac 6,1 */
  120. { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
  121. "TO0P", "Tp0P", NULL },
  122. /* Set 15: MacBook Air 2,1 */
  123. { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TN0D", "TTF0",
  124. "TV0P", "TVFP", "TW0P", "Th0P", "Tp0P", "Tp1P", "TpFP", "Ts0P",
  125. "Ts0S", NULL },
  126. /* Set 16: Mac Pro 3,1 (2 x Quad-Core) */
  127. { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
  128. "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "TH0P", "TH1P",
  129. "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", "TM1P",
  130. "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", "TM9S",
  131. "TN0C", "TN0D", "TN0H", "TS0C", "Tp0C", "Tp1C", "Tv0S", "Tv1S",
  132. NULL },
  133. };
  134. /* List of keys used to read/write fan speeds */
  135. static const char* fan_speed_keys[] = {
  136. FAN_ACTUAL_SPEED,
  137. FAN_MIN_SPEED,
  138. FAN_MAX_SPEED,
  139. FAN_SAFE_SPEED,
  140. FAN_TARGET_SPEED
  141. };
  142. #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
  143. #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
  144. #define APPLESMC_POLL_INTERVAL 50 /* msecs */
  145. #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
  146. #define APPLESMC_INPUT_FLAT 4
  147. #define SENSOR_X 0
  148. #define SENSOR_Y 1
  149. #define SENSOR_Z 2
  150. /* Structure to be passed to DMI_MATCH function */
  151. struct dmi_match_data {
  152. /* Indicates whether this computer has an accelerometer. */
  153. int accelerometer;
  154. /* Indicates whether this computer has light sensors and keyboard backlight. */
  155. int light;
  156. /* Indicates which temperature sensors set to use. */
  157. int temperature_set;
  158. };
  159. static const int debug;
  160. static struct platform_device *pdev;
  161. static s16 rest_x;
  162. static s16 rest_y;
  163. static u8 backlight_state[2];
  164. static struct device *hwmon_dev;
  165. static struct input_polled_dev *applesmc_idev;
  166. /* Indicates whether this computer has an accelerometer. */
  167. static unsigned int applesmc_accelerometer;
  168. /* Indicates whether this computer has light sensors and keyboard backlight. */
  169. static unsigned int applesmc_light;
  170. /* Indicates which temperature sensors set to use. */
  171. static unsigned int applesmc_temperature_set;
  172. static DEFINE_MUTEX(applesmc_lock);
  173. /*
  174. * Last index written to key_at_index sysfs file, and value to use for all other
  175. * key_at_index_* sysfs files.
  176. */
  177. static unsigned int key_at_index;
  178. static struct workqueue_struct *applesmc_led_wq;
  179. /*
  180. * __wait_status - Wait up to 32ms for the status port to get a certain value
  181. * (masked with 0x0f), returning zero if the value is obtained. Callers must
  182. * hold applesmc_lock.
  183. */
  184. static int __wait_status(u8 val)
  185. {
  186. int us;
  187. val = val & APPLESMC_STATUS_MASK;
  188. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  189. udelay(us);
  190. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
  191. if (debug)
  192. printk(KERN_DEBUG
  193. "Waited %d us for status %x\n",
  194. 2 * us - APPLESMC_MIN_WAIT, val);
  195. return 0;
  196. }
  197. }
  198. printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
  199. val, inb(APPLESMC_CMD_PORT));
  200. return -EIO;
  201. }
  202. /*
  203. * special treatment of command port - on newer macbooks, it seems necessary
  204. * to resend the command byte before polling the status again. Callers must
  205. * hold applesmc_lock.
  206. */
  207. static int send_command(u8 cmd)
  208. {
  209. int us;
  210. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  211. outb(cmd, APPLESMC_CMD_PORT);
  212. udelay(us);
  213. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
  214. return 0;
  215. }
  216. printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
  217. cmd, inb(APPLESMC_CMD_PORT));
  218. return -EIO;
  219. }
  220. /*
  221. * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
  222. * Returns zero on success or a negative error on failure. Callers must
  223. * hold applesmc_lock.
  224. */
  225. static int applesmc_read_key(const char* key, u8* buffer, u8 len)
  226. {
  227. int i;
  228. if (len > APPLESMC_MAX_DATA_LENGTH) {
  229. printk(KERN_ERR "applesmc_read_key: cannot read more than "
  230. "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
  231. return -EINVAL;
  232. }
  233. if (send_command(APPLESMC_READ_CMD))
  234. return -EIO;
  235. for (i = 0; i < 4; i++) {
  236. outb(key[i], APPLESMC_DATA_PORT);
  237. if (__wait_status(0x04))
  238. return -EIO;
  239. }
  240. if (debug)
  241. printk(KERN_DEBUG "<%s", key);
  242. outb(len, APPLESMC_DATA_PORT);
  243. if (debug)
  244. printk(KERN_DEBUG ">%x", len);
  245. for (i = 0; i < len; i++) {
  246. if (__wait_status(0x05))
  247. return -EIO;
  248. buffer[i] = inb(APPLESMC_DATA_PORT);
  249. if (debug)
  250. printk(KERN_DEBUG "<%x", buffer[i]);
  251. }
  252. if (debug)
  253. printk(KERN_DEBUG "\n");
  254. return 0;
  255. }
  256. /*
  257. * applesmc_write_key - writes len bytes from buffer to a given key.
  258. * Returns zero on success or a negative error on failure. Callers must
  259. * hold applesmc_lock.
  260. */
  261. static int applesmc_write_key(const char* key, u8* buffer, u8 len)
  262. {
  263. int i;
  264. if (len > APPLESMC_MAX_DATA_LENGTH) {
  265. printk(KERN_ERR "applesmc_write_key: cannot write more than "
  266. "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
  267. return -EINVAL;
  268. }
  269. if (send_command(APPLESMC_WRITE_CMD))
  270. return -EIO;
  271. for (i = 0; i < 4; i++) {
  272. outb(key[i], APPLESMC_DATA_PORT);
  273. if (__wait_status(0x04))
  274. return -EIO;
  275. }
  276. outb(len, APPLESMC_DATA_PORT);
  277. for (i = 0; i < len; i++) {
  278. if (__wait_status(0x04))
  279. return -EIO;
  280. outb(buffer[i], APPLESMC_DATA_PORT);
  281. }
  282. return 0;
  283. }
  284. /*
  285. * applesmc_get_key_at_index - get key at index, and put the result in key
  286. * (char[6]). Returns zero on success or a negative error on failure. Callers
  287. * must hold applesmc_lock.
  288. */
  289. static int applesmc_get_key_at_index(int index, char* key)
  290. {
  291. int i;
  292. u8 readkey[4];
  293. readkey[0] = index >> 24;
  294. readkey[1] = index >> 16;
  295. readkey[2] = index >> 8;
  296. readkey[3] = index;
  297. if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
  298. return -EIO;
  299. for (i = 0; i < 4; i++) {
  300. outb(readkey[i], APPLESMC_DATA_PORT);
  301. if (__wait_status(0x04))
  302. return -EIO;
  303. }
  304. outb(4, APPLESMC_DATA_PORT);
  305. for (i = 0; i < 4; i++) {
  306. if (__wait_status(0x05))
  307. return -EIO;
  308. key[i] = inb(APPLESMC_DATA_PORT);
  309. }
  310. key[4] = 0;
  311. return 0;
  312. }
  313. /*
  314. * applesmc_get_key_type - get key type, and put the result in type (char[6]).
  315. * Returns zero on success or a negative error on failure. Callers must
  316. * hold applesmc_lock.
  317. */
  318. static int applesmc_get_key_type(char* key, char* type)
  319. {
  320. int i;
  321. if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
  322. return -EIO;
  323. for (i = 0; i < 4; i++) {
  324. outb(key[i], APPLESMC_DATA_PORT);
  325. if (__wait_status(0x04))
  326. return -EIO;
  327. }
  328. outb(6, APPLESMC_DATA_PORT);
  329. for (i = 0; i < 6; i++) {
  330. if (__wait_status(0x05))
  331. return -EIO;
  332. type[i] = inb(APPLESMC_DATA_PORT);
  333. }
  334. type[5] = 0;
  335. return 0;
  336. }
  337. /*
  338. * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
  339. * hold applesmc_lock.
  340. */
  341. static int applesmc_read_motion_sensor(int index, s16* value)
  342. {
  343. u8 buffer[2];
  344. int ret;
  345. switch (index) {
  346. case SENSOR_X:
  347. ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
  348. break;
  349. case SENSOR_Y:
  350. ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
  351. break;
  352. case SENSOR_Z:
  353. ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
  354. break;
  355. default:
  356. ret = -EINVAL;
  357. }
  358. *value = ((s16)buffer[0] << 8) | buffer[1];
  359. return ret;
  360. }
  361. /*
  362. * applesmc_device_init - initialize the accelerometer. Returns zero on success
  363. * and negative error code on failure. Can sleep.
  364. */
  365. static int applesmc_device_init(void)
  366. {
  367. int total, ret = -ENXIO;
  368. u8 buffer[2];
  369. if (!applesmc_accelerometer)
  370. return 0;
  371. mutex_lock(&applesmc_lock);
  372. for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
  373. if (debug)
  374. printk(KERN_DEBUG "applesmc try %d\n", total);
  375. if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
  376. (buffer[0] != 0x00 || buffer[1] != 0x00)) {
  377. if (total == INIT_TIMEOUT_MSECS) {
  378. printk(KERN_DEBUG "applesmc: device has"
  379. " already been initialized"
  380. " (0x%02x, 0x%02x).\n",
  381. buffer[0], buffer[1]);
  382. } else {
  383. printk(KERN_DEBUG "applesmc: device"
  384. " successfully initialized"
  385. " (0x%02x, 0x%02x).\n",
  386. buffer[0], buffer[1]);
  387. }
  388. ret = 0;
  389. goto out;
  390. }
  391. buffer[0] = 0xe0;
  392. buffer[1] = 0x00;
  393. applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
  394. msleep(INIT_WAIT_MSECS);
  395. }
  396. printk(KERN_WARNING "applesmc: failed to init the device\n");
  397. out:
  398. mutex_unlock(&applesmc_lock);
  399. return ret;
  400. }
  401. /*
  402. * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
  403. * applesmc_lock.
  404. */
  405. static int applesmc_get_fan_count(void)
  406. {
  407. int ret;
  408. u8 buffer[1];
  409. mutex_lock(&applesmc_lock);
  410. ret = applesmc_read_key(FANS_COUNT, buffer, 1);
  411. mutex_unlock(&applesmc_lock);
  412. if (ret)
  413. return ret;
  414. else
  415. return buffer[0];
  416. }
  417. /* Device model stuff */
  418. static int applesmc_probe(struct platform_device *dev)
  419. {
  420. int ret;
  421. ret = applesmc_device_init();
  422. if (ret)
  423. return ret;
  424. printk(KERN_INFO "applesmc: device successfully initialized.\n");
  425. return 0;
  426. }
  427. /* Synchronize device with memorized backlight state */
  428. static int applesmc_pm_resume(struct device *dev)
  429. {
  430. mutex_lock(&applesmc_lock);
  431. if (applesmc_light)
  432. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  433. mutex_unlock(&applesmc_lock);
  434. return 0;
  435. }
  436. /* Reinitialize device on resume from hibernation */
  437. static int applesmc_pm_restore(struct device *dev)
  438. {
  439. int ret = applesmc_device_init();
  440. if (ret)
  441. return ret;
  442. return applesmc_pm_resume(dev);
  443. }
  444. static const struct dev_pm_ops applesmc_pm_ops = {
  445. .resume = applesmc_pm_resume,
  446. .restore = applesmc_pm_restore,
  447. };
  448. static struct platform_driver applesmc_driver = {
  449. .probe = applesmc_probe,
  450. .driver = {
  451. .name = "applesmc",
  452. .owner = THIS_MODULE,
  453. .pm = &applesmc_pm_ops,
  454. },
  455. };
  456. /*
  457. * applesmc_calibrate - Set our "resting" values. Callers must
  458. * hold applesmc_lock.
  459. */
  460. static void applesmc_calibrate(void)
  461. {
  462. applesmc_read_motion_sensor(SENSOR_X, &rest_x);
  463. applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
  464. rest_x = -rest_x;
  465. }
  466. static void applesmc_idev_poll(struct input_polled_dev *dev)
  467. {
  468. struct input_dev *idev = dev->input;
  469. s16 x, y;
  470. mutex_lock(&applesmc_lock);
  471. if (applesmc_read_motion_sensor(SENSOR_X, &x))
  472. goto out;
  473. if (applesmc_read_motion_sensor(SENSOR_Y, &y))
  474. goto out;
  475. x = -x;
  476. input_report_abs(idev, ABS_X, x - rest_x);
  477. input_report_abs(idev, ABS_Y, y - rest_y);
  478. input_sync(idev);
  479. out:
  480. mutex_unlock(&applesmc_lock);
  481. }
  482. /* Sysfs Files */
  483. static ssize_t applesmc_name_show(struct device *dev,
  484. struct device_attribute *attr, char *buf)
  485. {
  486. return snprintf(buf, PAGE_SIZE, "applesmc\n");
  487. }
  488. static ssize_t applesmc_position_show(struct device *dev,
  489. struct device_attribute *attr, char *buf)
  490. {
  491. int ret;
  492. s16 x, y, z;
  493. mutex_lock(&applesmc_lock);
  494. ret = applesmc_read_motion_sensor(SENSOR_X, &x);
  495. if (ret)
  496. goto out;
  497. ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
  498. if (ret)
  499. goto out;
  500. ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
  501. if (ret)
  502. goto out;
  503. out:
  504. mutex_unlock(&applesmc_lock);
  505. if (ret)
  506. return ret;
  507. else
  508. return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
  509. }
  510. static ssize_t applesmc_light_show(struct device *dev,
  511. struct device_attribute *attr, char *sysfsbuf)
  512. {
  513. static int data_length;
  514. int ret;
  515. u8 left = 0, right = 0;
  516. u8 buffer[10], query[6];
  517. mutex_lock(&applesmc_lock);
  518. if (!data_length) {
  519. ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
  520. if (ret)
  521. goto out;
  522. data_length = clamp_val(query[0], 0, 10);
  523. printk(KERN_INFO "applesmc: light sensor data length set to "
  524. "%d\n", data_length);
  525. }
  526. ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
  527. /* newer macbooks report a single 10-bit bigendian value */
  528. if (data_length == 10) {
  529. left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
  530. goto out;
  531. }
  532. left = buffer[2];
  533. if (ret)
  534. goto out;
  535. ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
  536. right = buffer[2];
  537. out:
  538. mutex_unlock(&applesmc_lock);
  539. if (ret)
  540. return ret;
  541. else
  542. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
  543. }
  544. /* Displays degree Celsius * 1000 */
  545. static ssize_t applesmc_show_temperature(struct device *dev,
  546. struct device_attribute *devattr, char *sysfsbuf)
  547. {
  548. int ret;
  549. u8 buffer[2];
  550. unsigned int temp;
  551. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  552. const char* key =
  553. temperature_sensors_sets[applesmc_temperature_set][attr->index];
  554. mutex_lock(&applesmc_lock);
  555. ret = applesmc_read_key(key, buffer, 2);
  556. temp = buffer[0]*1000;
  557. temp += (buffer[1] >> 6) * 250;
  558. mutex_unlock(&applesmc_lock);
  559. if (ret)
  560. return ret;
  561. else
  562. return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
  563. }
  564. static ssize_t applesmc_show_fan_speed(struct device *dev,
  565. struct device_attribute *attr, char *sysfsbuf)
  566. {
  567. int ret;
  568. unsigned int speed = 0;
  569. char newkey[5];
  570. u8 buffer[2];
  571. struct sensor_device_attribute_2 *sensor_attr =
  572. to_sensor_dev_attr_2(attr);
  573. newkey[0] = fan_speed_keys[sensor_attr->nr][0];
  574. newkey[1] = '0' + sensor_attr->index;
  575. newkey[2] = fan_speed_keys[sensor_attr->nr][2];
  576. newkey[3] = fan_speed_keys[sensor_attr->nr][3];
  577. newkey[4] = 0;
  578. mutex_lock(&applesmc_lock);
  579. ret = applesmc_read_key(newkey, buffer, 2);
  580. speed = ((buffer[0] << 8 | buffer[1]) >> 2);
  581. mutex_unlock(&applesmc_lock);
  582. if (ret)
  583. return ret;
  584. else
  585. return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
  586. }
  587. static ssize_t applesmc_store_fan_speed(struct device *dev,
  588. struct device_attribute *attr,
  589. const char *sysfsbuf, size_t count)
  590. {
  591. int ret;
  592. u32 speed;
  593. char newkey[5];
  594. u8 buffer[2];
  595. struct sensor_device_attribute_2 *sensor_attr =
  596. to_sensor_dev_attr_2(attr);
  597. speed = simple_strtoul(sysfsbuf, NULL, 10);
  598. if (speed > 0x4000) /* Bigger than a 14-bit value */
  599. return -EINVAL;
  600. newkey[0] = fan_speed_keys[sensor_attr->nr][0];
  601. newkey[1] = '0' + sensor_attr->index;
  602. newkey[2] = fan_speed_keys[sensor_attr->nr][2];
  603. newkey[3] = fan_speed_keys[sensor_attr->nr][3];
  604. newkey[4] = 0;
  605. mutex_lock(&applesmc_lock);
  606. buffer[0] = (speed >> 6) & 0xff;
  607. buffer[1] = (speed << 2) & 0xff;
  608. ret = applesmc_write_key(newkey, buffer, 2);
  609. mutex_unlock(&applesmc_lock);
  610. if (ret)
  611. return ret;
  612. else
  613. return count;
  614. }
  615. static ssize_t applesmc_show_fan_manual(struct device *dev,
  616. struct device_attribute *devattr, char *sysfsbuf)
  617. {
  618. int ret;
  619. u16 manual = 0;
  620. u8 buffer[2];
  621. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  622. mutex_lock(&applesmc_lock);
  623. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  624. manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
  625. mutex_unlock(&applesmc_lock);
  626. if (ret)
  627. return ret;
  628. else
  629. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
  630. }
  631. static ssize_t applesmc_store_fan_manual(struct device *dev,
  632. struct device_attribute *devattr,
  633. const char *sysfsbuf, size_t count)
  634. {
  635. int ret;
  636. u8 buffer[2];
  637. u32 input;
  638. u16 val;
  639. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  640. input = simple_strtoul(sysfsbuf, NULL, 10);
  641. mutex_lock(&applesmc_lock);
  642. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  643. val = (buffer[0] << 8 | buffer[1]);
  644. if (ret)
  645. goto out;
  646. if (input)
  647. val = val | (0x01 << attr->index);
  648. else
  649. val = val & ~(0x01 << attr->index);
  650. buffer[0] = (val >> 8) & 0xFF;
  651. buffer[1] = val & 0xFF;
  652. ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
  653. out:
  654. mutex_unlock(&applesmc_lock);
  655. if (ret)
  656. return ret;
  657. else
  658. return count;
  659. }
  660. static ssize_t applesmc_show_fan_position(struct device *dev,
  661. struct device_attribute *attr, char *sysfsbuf)
  662. {
  663. int ret;
  664. char newkey[5];
  665. u8 buffer[17];
  666. struct sensor_device_attribute_2 *sensor_attr =
  667. to_sensor_dev_attr_2(attr);
  668. newkey[0] = FAN_POSITION[0];
  669. newkey[1] = '0' + sensor_attr->index;
  670. newkey[2] = FAN_POSITION[2];
  671. newkey[3] = FAN_POSITION[3];
  672. newkey[4] = 0;
  673. mutex_lock(&applesmc_lock);
  674. ret = applesmc_read_key(newkey, buffer, 16);
  675. buffer[16] = 0;
  676. mutex_unlock(&applesmc_lock);
  677. if (ret)
  678. return ret;
  679. else
  680. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
  681. }
  682. static ssize_t applesmc_calibrate_show(struct device *dev,
  683. struct device_attribute *attr, char *sysfsbuf)
  684. {
  685. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
  686. }
  687. static ssize_t applesmc_calibrate_store(struct device *dev,
  688. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  689. {
  690. mutex_lock(&applesmc_lock);
  691. applesmc_calibrate();
  692. mutex_unlock(&applesmc_lock);
  693. return count;
  694. }
  695. static void applesmc_backlight_set(struct work_struct *work)
  696. {
  697. mutex_lock(&applesmc_lock);
  698. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  699. mutex_unlock(&applesmc_lock);
  700. }
  701. static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
  702. static void applesmc_brightness_set(struct led_classdev *led_cdev,
  703. enum led_brightness value)
  704. {
  705. int ret;
  706. backlight_state[0] = value;
  707. ret = queue_work(applesmc_led_wq, &backlight_work);
  708. if (debug && (!ret))
  709. printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
  710. }
  711. static ssize_t applesmc_key_count_show(struct device *dev,
  712. struct device_attribute *attr, char *sysfsbuf)
  713. {
  714. int ret;
  715. u8 buffer[4];
  716. u32 count;
  717. mutex_lock(&applesmc_lock);
  718. ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
  719. count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
  720. ((u32)buffer[2]<<8) + buffer[3];
  721. mutex_unlock(&applesmc_lock);
  722. if (ret)
  723. return ret;
  724. else
  725. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
  726. }
  727. static ssize_t applesmc_key_at_index_read_show(struct device *dev,
  728. struct device_attribute *attr, char *sysfsbuf)
  729. {
  730. char key[5];
  731. char info[6];
  732. int ret;
  733. mutex_lock(&applesmc_lock);
  734. ret = applesmc_get_key_at_index(key_at_index, key);
  735. if (ret || !key[0]) {
  736. mutex_unlock(&applesmc_lock);
  737. return -EINVAL;
  738. }
  739. ret = applesmc_get_key_type(key, info);
  740. if (ret) {
  741. mutex_unlock(&applesmc_lock);
  742. return ret;
  743. }
  744. /*
  745. * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
  746. * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
  747. */
  748. ret = applesmc_read_key(key, sysfsbuf, info[0]);
  749. mutex_unlock(&applesmc_lock);
  750. if (!ret) {
  751. return info[0];
  752. } else {
  753. return ret;
  754. }
  755. }
  756. static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
  757. struct device_attribute *attr, char *sysfsbuf)
  758. {
  759. char key[5];
  760. char info[6];
  761. int ret;
  762. mutex_lock(&applesmc_lock);
  763. ret = applesmc_get_key_at_index(key_at_index, key);
  764. if (ret || !key[0]) {
  765. mutex_unlock(&applesmc_lock);
  766. return -EINVAL;
  767. }
  768. ret = applesmc_get_key_type(key, info);
  769. mutex_unlock(&applesmc_lock);
  770. if (!ret)
  771. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
  772. else
  773. return ret;
  774. }
  775. static ssize_t applesmc_key_at_index_type_show(struct device *dev,
  776. struct device_attribute *attr, char *sysfsbuf)
  777. {
  778. char key[5];
  779. char info[6];
  780. int ret;
  781. mutex_lock(&applesmc_lock);
  782. ret = applesmc_get_key_at_index(key_at_index, key);
  783. if (ret || !key[0]) {
  784. mutex_unlock(&applesmc_lock);
  785. return -EINVAL;
  786. }
  787. ret = applesmc_get_key_type(key, info);
  788. mutex_unlock(&applesmc_lock);
  789. if (!ret)
  790. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
  791. else
  792. return ret;
  793. }
  794. static ssize_t applesmc_key_at_index_name_show(struct device *dev,
  795. struct device_attribute *attr, char *sysfsbuf)
  796. {
  797. char key[5];
  798. int ret;
  799. mutex_lock(&applesmc_lock);
  800. ret = applesmc_get_key_at_index(key_at_index, key);
  801. mutex_unlock(&applesmc_lock);
  802. if (!ret && key[0])
  803. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
  804. else
  805. return -EINVAL;
  806. }
  807. static ssize_t applesmc_key_at_index_show(struct device *dev,
  808. struct device_attribute *attr, char *sysfsbuf)
  809. {
  810. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
  811. }
  812. static ssize_t applesmc_key_at_index_store(struct device *dev,
  813. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  814. {
  815. mutex_lock(&applesmc_lock);
  816. key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
  817. mutex_unlock(&applesmc_lock);
  818. return count;
  819. }
  820. static struct led_classdev applesmc_backlight = {
  821. .name = "smc::kbd_backlight",
  822. .default_trigger = "nand-disk",
  823. .brightness_set = applesmc_brightness_set,
  824. };
  825. static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
  826. static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
  827. static DEVICE_ATTR(calibrate, 0644,
  828. applesmc_calibrate_show, applesmc_calibrate_store);
  829. static struct attribute *accelerometer_attributes[] = {
  830. &dev_attr_position.attr,
  831. &dev_attr_calibrate.attr,
  832. NULL
  833. };
  834. static const struct attribute_group accelerometer_attributes_group =
  835. { .attrs = accelerometer_attributes };
  836. static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
  837. static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
  838. static DEVICE_ATTR(key_at_index, 0644,
  839. applesmc_key_at_index_show, applesmc_key_at_index_store);
  840. static DEVICE_ATTR(key_at_index_name, 0444,
  841. applesmc_key_at_index_name_show, NULL);
  842. static DEVICE_ATTR(key_at_index_type, 0444,
  843. applesmc_key_at_index_type_show, NULL);
  844. static DEVICE_ATTR(key_at_index_data_length, 0444,
  845. applesmc_key_at_index_data_length_show, NULL);
  846. static DEVICE_ATTR(key_at_index_data, 0444,
  847. applesmc_key_at_index_read_show, NULL);
  848. static struct attribute *key_enumeration_attributes[] = {
  849. &dev_attr_key_count.attr,
  850. &dev_attr_key_at_index.attr,
  851. &dev_attr_key_at_index_name.attr,
  852. &dev_attr_key_at_index_type.attr,
  853. &dev_attr_key_at_index_data_length.attr,
  854. &dev_attr_key_at_index_data.attr,
  855. NULL
  856. };
  857. static const struct attribute_group key_enumeration_group =
  858. { .attrs = key_enumeration_attributes };
  859. /*
  860. * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
  861. * - show actual speed
  862. * - show/store minimum speed
  863. * - show maximum speed
  864. * - show safe speed
  865. * - show/store target speed
  866. * - show/store manual mode
  867. */
  868. #define sysfs_fan_speeds_offset(offset) \
  869. static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
  870. applesmc_show_fan_speed, NULL, 0, offset-1); \
  871. \
  872. static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
  873. applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
  874. \
  875. static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
  876. applesmc_show_fan_speed, NULL, 2, offset-1); \
  877. \
  878. static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
  879. applesmc_show_fan_speed, NULL, 3, offset-1); \
  880. \
  881. static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
  882. applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
  883. \
  884. static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
  885. applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
  886. \
  887. static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
  888. applesmc_show_fan_position, NULL, offset-1); \
  889. \
  890. static struct attribute *fan##offset##_attributes[] = { \
  891. &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
  892. &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
  893. &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
  894. &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
  895. &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
  896. &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
  897. &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
  898. NULL \
  899. };
  900. /*
  901. * Create the needed functions for each fan using the macro defined above
  902. * (4 fans are supported)
  903. */
  904. sysfs_fan_speeds_offset(1);
  905. sysfs_fan_speeds_offset(2);
  906. sysfs_fan_speeds_offset(3);
  907. sysfs_fan_speeds_offset(4);
  908. static const struct attribute_group fan_attribute_groups[] = {
  909. { .attrs = fan1_attributes },
  910. { .attrs = fan2_attributes },
  911. { .attrs = fan3_attributes },
  912. { .attrs = fan4_attributes },
  913. };
  914. /*
  915. * Temperature sensors sysfs entries.
  916. */
  917. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  918. applesmc_show_temperature, NULL, 0);
  919. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
  920. applesmc_show_temperature, NULL, 1);
  921. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
  922. applesmc_show_temperature, NULL, 2);
  923. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
  924. applesmc_show_temperature, NULL, 3);
  925. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
  926. applesmc_show_temperature, NULL, 4);
  927. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
  928. applesmc_show_temperature, NULL, 5);
  929. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
  930. applesmc_show_temperature, NULL, 6);
  931. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
  932. applesmc_show_temperature, NULL, 7);
  933. static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
  934. applesmc_show_temperature, NULL, 8);
  935. static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
  936. applesmc_show_temperature, NULL, 9);
  937. static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
  938. applesmc_show_temperature, NULL, 10);
  939. static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
  940. applesmc_show_temperature, NULL, 11);
  941. static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
  942. applesmc_show_temperature, NULL, 12);
  943. static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
  944. applesmc_show_temperature, NULL, 13);
  945. static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
  946. applesmc_show_temperature, NULL, 14);
  947. static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
  948. applesmc_show_temperature, NULL, 15);
  949. static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
  950. applesmc_show_temperature, NULL, 16);
  951. static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
  952. applesmc_show_temperature, NULL, 17);
  953. static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
  954. applesmc_show_temperature, NULL, 18);
  955. static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
  956. applesmc_show_temperature, NULL, 19);
  957. static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
  958. applesmc_show_temperature, NULL, 20);
  959. static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
  960. applesmc_show_temperature, NULL, 21);
  961. static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
  962. applesmc_show_temperature, NULL, 22);
  963. static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
  964. applesmc_show_temperature, NULL, 23);
  965. static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
  966. applesmc_show_temperature, NULL, 24);
  967. static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
  968. applesmc_show_temperature, NULL, 25);
  969. static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
  970. applesmc_show_temperature, NULL, 26);
  971. static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
  972. applesmc_show_temperature, NULL, 27);
  973. static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
  974. applesmc_show_temperature, NULL, 28);
  975. static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
  976. applesmc_show_temperature, NULL, 29);
  977. static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
  978. applesmc_show_temperature, NULL, 30);
  979. static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
  980. applesmc_show_temperature, NULL, 31);
  981. static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
  982. applesmc_show_temperature, NULL, 32);
  983. static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
  984. applesmc_show_temperature, NULL, 33);
  985. static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
  986. applesmc_show_temperature, NULL, 34);
  987. static SENSOR_DEVICE_ATTR(temp36_input, S_IRUGO,
  988. applesmc_show_temperature, NULL, 35);
  989. static SENSOR_DEVICE_ATTR(temp37_input, S_IRUGO,
  990. applesmc_show_temperature, NULL, 36);
  991. static SENSOR_DEVICE_ATTR(temp38_input, S_IRUGO,
  992. applesmc_show_temperature, NULL, 37);
  993. static SENSOR_DEVICE_ATTR(temp39_input, S_IRUGO,
  994. applesmc_show_temperature, NULL, 38);
  995. static SENSOR_DEVICE_ATTR(temp40_input, S_IRUGO,
  996. applesmc_show_temperature, NULL, 39);
  997. static struct attribute *temperature_attributes[] = {
  998. &sensor_dev_attr_temp1_input.dev_attr.attr,
  999. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1000. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1001. &sensor_dev_attr_temp4_input.dev_attr.attr,
  1002. &sensor_dev_attr_temp5_input.dev_attr.attr,
  1003. &sensor_dev_attr_temp6_input.dev_attr.attr,
  1004. &sensor_dev_attr_temp7_input.dev_attr.attr,
  1005. &sensor_dev_attr_temp8_input.dev_attr.attr,
  1006. &sensor_dev_attr_temp9_input.dev_attr.attr,
  1007. &sensor_dev_attr_temp10_input.dev_attr.attr,
  1008. &sensor_dev_attr_temp11_input.dev_attr.attr,
  1009. &sensor_dev_attr_temp12_input.dev_attr.attr,
  1010. &sensor_dev_attr_temp13_input.dev_attr.attr,
  1011. &sensor_dev_attr_temp14_input.dev_attr.attr,
  1012. &sensor_dev_attr_temp15_input.dev_attr.attr,
  1013. &sensor_dev_attr_temp16_input.dev_attr.attr,
  1014. &sensor_dev_attr_temp17_input.dev_attr.attr,
  1015. &sensor_dev_attr_temp18_input.dev_attr.attr,
  1016. &sensor_dev_attr_temp19_input.dev_attr.attr,
  1017. &sensor_dev_attr_temp20_input.dev_attr.attr,
  1018. &sensor_dev_attr_temp21_input.dev_attr.attr,
  1019. &sensor_dev_attr_temp22_input.dev_attr.attr,
  1020. &sensor_dev_attr_temp23_input.dev_attr.attr,
  1021. &sensor_dev_attr_temp24_input.dev_attr.attr,
  1022. &sensor_dev_attr_temp25_input.dev_attr.attr,
  1023. &sensor_dev_attr_temp26_input.dev_attr.attr,
  1024. &sensor_dev_attr_temp27_input.dev_attr.attr,
  1025. &sensor_dev_attr_temp28_input.dev_attr.attr,
  1026. &sensor_dev_attr_temp29_input.dev_attr.attr,
  1027. &sensor_dev_attr_temp30_input.dev_attr.attr,
  1028. &sensor_dev_attr_temp31_input.dev_attr.attr,
  1029. &sensor_dev_attr_temp32_input.dev_attr.attr,
  1030. &sensor_dev_attr_temp33_input.dev_attr.attr,
  1031. &sensor_dev_attr_temp34_input.dev_attr.attr,
  1032. &sensor_dev_attr_temp35_input.dev_attr.attr,
  1033. &sensor_dev_attr_temp36_input.dev_attr.attr,
  1034. &sensor_dev_attr_temp37_input.dev_attr.attr,
  1035. &sensor_dev_attr_temp38_input.dev_attr.attr,
  1036. &sensor_dev_attr_temp39_input.dev_attr.attr,
  1037. &sensor_dev_attr_temp40_input.dev_attr.attr,
  1038. NULL
  1039. };
  1040. static const struct attribute_group temperature_attributes_group =
  1041. { .attrs = temperature_attributes };
  1042. /* Module stuff */
  1043. /*
  1044. * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
  1045. */
  1046. static int applesmc_dmi_match(const struct dmi_system_id *id)
  1047. {
  1048. int i = 0;
  1049. struct dmi_match_data* dmi_data = id->driver_data;
  1050. printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
  1051. applesmc_accelerometer = dmi_data->accelerometer;
  1052. printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
  1053. applesmc_accelerometer ? "with" : "without");
  1054. applesmc_light = dmi_data->light;
  1055. printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
  1056. applesmc_light ? "with" : "without");
  1057. applesmc_temperature_set = dmi_data->temperature_set;
  1058. while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
  1059. i++;
  1060. printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
  1061. return 1;
  1062. }
  1063. /* Create accelerometer ressources */
  1064. static int applesmc_create_accelerometer(void)
  1065. {
  1066. struct input_dev *idev;
  1067. int ret;
  1068. ret = sysfs_create_group(&pdev->dev.kobj,
  1069. &accelerometer_attributes_group);
  1070. if (ret)
  1071. goto out;
  1072. applesmc_idev = input_allocate_polled_device();
  1073. if (!applesmc_idev) {
  1074. ret = -ENOMEM;
  1075. goto out_sysfs;
  1076. }
  1077. applesmc_idev->poll = applesmc_idev_poll;
  1078. applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
  1079. /* initial calibrate for the input device */
  1080. applesmc_calibrate();
  1081. /* initialize the input device */
  1082. idev = applesmc_idev->input;
  1083. idev->name = "applesmc";
  1084. idev->id.bustype = BUS_HOST;
  1085. idev->dev.parent = &pdev->dev;
  1086. idev->evbit[0] = BIT_MASK(EV_ABS);
  1087. input_set_abs_params(idev, ABS_X,
  1088. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  1089. input_set_abs_params(idev, ABS_Y,
  1090. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  1091. ret = input_register_polled_device(applesmc_idev);
  1092. if (ret)
  1093. goto out_idev;
  1094. return 0;
  1095. out_idev:
  1096. input_free_polled_device(applesmc_idev);
  1097. out_sysfs:
  1098. sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
  1099. out:
  1100. printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
  1101. return ret;
  1102. }
  1103. /* Release all ressources used by the accelerometer */
  1104. static void applesmc_release_accelerometer(void)
  1105. {
  1106. input_unregister_polled_device(applesmc_idev);
  1107. input_free_polled_device(applesmc_idev);
  1108. sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
  1109. }
  1110. static __initdata struct dmi_match_data applesmc_dmi_data[] = {
  1111. /* MacBook Pro: accelerometer, backlight and temperature set 0 */
  1112. { .accelerometer = 1, .light = 1, .temperature_set = 0 },
  1113. /* MacBook2: accelerometer and temperature set 1 */
  1114. { .accelerometer = 1, .light = 0, .temperature_set = 1 },
  1115. /* MacBook: accelerometer and temperature set 2 */
  1116. { .accelerometer = 1, .light = 0, .temperature_set = 2 },
  1117. /* MacMini: temperature set 3 */
  1118. { .accelerometer = 0, .light = 0, .temperature_set = 3 },
  1119. /* MacPro: temperature set 4 */
  1120. { .accelerometer = 0, .light = 0, .temperature_set = 4 },
  1121. /* iMac: temperature set 5 */
  1122. { .accelerometer = 0, .light = 0, .temperature_set = 5 },
  1123. /* MacBook3, MacBook4: accelerometer and temperature set 6 */
  1124. { .accelerometer = 1, .light = 0, .temperature_set = 6 },
  1125. /* MacBook Air: accelerometer, backlight and temperature set 7 */
  1126. { .accelerometer = 1, .light = 1, .temperature_set = 7 },
  1127. /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
  1128. { .accelerometer = 1, .light = 1, .temperature_set = 8 },
  1129. /* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
  1130. { .accelerometer = 1, .light = 1, .temperature_set = 9 },
  1131. /* iMac 5: light sensor only, temperature set 10 */
  1132. { .accelerometer = 0, .light = 0, .temperature_set = 10 },
  1133. /* MacBook 5: accelerometer, backlight and temperature set 11 */
  1134. { .accelerometer = 1, .light = 1, .temperature_set = 11 },
  1135. /* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
  1136. { .accelerometer = 1, .light = 1, .temperature_set = 12 },
  1137. /* iMac 8: light sensor only, temperature set 13 */
  1138. { .accelerometer = 0, .light = 0, .temperature_set = 13 },
  1139. /* iMac 6: light sensor only, temperature set 14 */
  1140. { .accelerometer = 0, .light = 0, .temperature_set = 14 },
  1141. /* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
  1142. { .accelerometer = 1, .light = 1, .temperature_set = 15 },
  1143. /* MacPro3,1: temperature set 16 */
  1144. { .accelerometer = 0, .light = 0, .temperature_set = 16 },
  1145. };
  1146. /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
  1147. * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
  1148. static __initdata struct dmi_system_id applesmc_whitelist[] = {
  1149. { applesmc_dmi_match, "Apple MacBook Air 2", {
  1150. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1151. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
  1152. &applesmc_dmi_data[15]},
  1153. { applesmc_dmi_match, "Apple MacBook Air", {
  1154. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1155. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
  1156. &applesmc_dmi_data[7]},
  1157. { applesmc_dmi_match, "Apple MacBook Pro 5", {
  1158. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1159. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
  1160. &applesmc_dmi_data[12]},
  1161. { applesmc_dmi_match, "Apple MacBook Pro 4", {
  1162. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1163. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
  1164. &applesmc_dmi_data[8]},
  1165. { applesmc_dmi_match, "Apple MacBook Pro 3", {
  1166. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1167. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
  1168. &applesmc_dmi_data[9]},
  1169. { applesmc_dmi_match, "Apple MacBook Pro", {
  1170. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1171. DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
  1172. &applesmc_dmi_data[0]},
  1173. { applesmc_dmi_match, "Apple MacBook (v2)", {
  1174. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1175. DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
  1176. &applesmc_dmi_data[1]},
  1177. { applesmc_dmi_match, "Apple MacBook (v3)", {
  1178. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1179. DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
  1180. &applesmc_dmi_data[6]},
  1181. { applesmc_dmi_match, "Apple MacBook 4", {
  1182. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1183. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
  1184. &applesmc_dmi_data[6]},
  1185. { applesmc_dmi_match, "Apple MacBook 5", {
  1186. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1187. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
  1188. &applesmc_dmi_data[11]},
  1189. { applesmc_dmi_match, "Apple MacBook", {
  1190. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1191. DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
  1192. &applesmc_dmi_data[2]},
  1193. { applesmc_dmi_match, "Apple Macmini", {
  1194. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1195. DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
  1196. &applesmc_dmi_data[3]},
  1197. { applesmc_dmi_match, "Apple MacPro2", {
  1198. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1199. DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
  1200. &applesmc_dmi_data[4]},
  1201. { applesmc_dmi_match, "Apple MacPro3", {
  1202. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1203. DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
  1204. &applesmc_dmi_data[16]},
  1205. { applesmc_dmi_match, "Apple MacPro", {
  1206. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1207. DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
  1208. &applesmc_dmi_data[4]},
  1209. { applesmc_dmi_match, "Apple iMac 8", {
  1210. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1211. DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
  1212. &applesmc_dmi_data[13]},
  1213. { applesmc_dmi_match, "Apple iMac 6", {
  1214. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1215. DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
  1216. &applesmc_dmi_data[14]},
  1217. { applesmc_dmi_match, "Apple iMac 5", {
  1218. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1219. DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
  1220. &applesmc_dmi_data[10]},
  1221. { applesmc_dmi_match, "Apple iMac", {
  1222. DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
  1223. DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
  1224. &applesmc_dmi_data[5]},
  1225. { .ident = NULL }
  1226. };
  1227. static int __init applesmc_init(void)
  1228. {
  1229. int ret;
  1230. int count;
  1231. int i;
  1232. if (!dmi_check_system(applesmc_whitelist)) {
  1233. printk(KERN_WARNING "applesmc: supported laptop not found!\n");
  1234. ret = -ENODEV;
  1235. goto out;
  1236. }
  1237. if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
  1238. "applesmc")) {
  1239. ret = -ENXIO;
  1240. goto out;
  1241. }
  1242. ret = platform_driver_register(&applesmc_driver);
  1243. if (ret)
  1244. goto out_region;
  1245. pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
  1246. NULL, 0);
  1247. if (IS_ERR(pdev)) {
  1248. ret = PTR_ERR(pdev);
  1249. goto out_driver;
  1250. }
  1251. ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
  1252. if (ret)
  1253. goto out_device;
  1254. /* Create key enumeration sysfs files */
  1255. ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
  1256. if (ret)
  1257. goto out_name;
  1258. /* create fan files */
  1259. count = applesmc_get_fan_count();
  1260. if (count < 0) {
  1261. printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
  1262. } else {
  1263. printk(KERN_INFO "applesmc: %d fans found.\n", count);
  1264. switch (count) {
  1265. default:
  1266. printk(KERN_WARNING "applesmc: More than 4 fans found,"
  1267. " but at most 4 fans are supported"
  1268. " by the driver.\n");
  1269. case 4:
  1270. ret = sysfs_create_group(&pdev->dev.kobj,
  1271. &fan_attribute_groups[3]);
  1272. if (ret)
  1273. goto out_key_enumeration;
  1274. case 3:
  1275. ret = sysfs_create_group(&pdev->dev.kobj,
  1276. &fan_attribute_groups[2]);
  1277. if (ret)
  1278. goto out_key_enumeration;
  1279. case 2:
  1280. ret = sysfs_create_group(&pdev->dev.kobj,
  1281. &fan_attribute_groups[1]);
  1282. if (ret)
  1283. goto out_key_enumeration;
  1284. case 1:
  1285. ret = sysfs_create_group(&pdev->dev.kobj,
  1286. &fan_attribute_groups[0]);
  1287. if (ret)
  1288. goto out_fan_1;
  1289. case 0:
  1290. ;
  1291. }
  1292. }
  1293. for (i = 0;
  1294. temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
  1295. i++) {
  1296. if (temperature_attributes[i] == NULL) {
  1297. printk(KERN_ERR "applesmc: More temperature sensors "
  1298. "in temperature_sensors_sets (at least %i)"
  1299. "than available sysfs files in "
  1300. "temperature_attributes (%i), please report "
  1301. "this bug.\n", i, i-1);
  1302. goto out_temperature;
  1303. }
  1304. ret = sysfs_create_file(&pdev->dev.kobj,
  1305. temperature_attributes[i]);
  1306. if (ret)
  1307. goto out_temperature;
  1308. }
  1309. if (applesmc_accelerometer) {
  1310. ret = applesmc_create_accelerometer();
  1311. if (ret)
  1312. goto out_temperature;
  1313. }
  1314. if (applesmc_light) {
  1315. /* Add light sensor file */
  1316. ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1317. if (ret)
  1318. goto out_accelerometer;
  1319. /* Create the workqueue */
  1320. applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
  1321. if (!applesmc_led_wq) {
  1322. ret = -ENOMEM;
  1323. goto out_light_sysfs;
  1324. }
  1325. /* register as a led device */
  1326. ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
  1327. if (ret < 0)
  1328. goto out_light_wq;
  1329. }
  1330. hwmon_dev = hwmon_device_register(&pdev->dev);
  1331. if (IS_ERR(hwmon_dev)) {
  1332. ret = PTR_ERR(hwmon_dev);
  1333. goto out_light_ledclass;
  1334. }
  1335. printk(KERN_INFO "applesmc: driver successfully loaded.\n");
  1336. return 0;
  1337. out_light_ledclass:
  1338. if (applesmc_light)
  1339. led_classdev_unregister(&applesmc_backlight);
  1340. out_light_wq:
  1341. if (applesmc_light)
  1342. destroy_workqueue(applesmc_led_wq);
  1343. out_light_sysfs:
  1344. if (applesmc_light)
  1345. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1346. out_accelerometer:
  1347. if (applesmc_accelerometer)
  1348. applesmc_release_accelerometer();
  1349. out_temperature:
  1350. sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
  1351. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
  1352. out_fan_1:
  1353. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
  1354. out_key_enumeration:
  1355. sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
  1356. out_name:
  1357. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
  1358. out_device:
  1359. platform_device_unregister(pdev);
  1360. out_driver:
  1361. platform_driver_unregister(&applesmc_driver);
  1362. out_region:
  1363. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1364. out:
  1365. printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
  1366. return ret;
  1367. }
  1368. static void __exit applesmc_exit(void)
  1369. {
  1370. hwmon_device_unregister(hwmon_dev);
  1371. if (applesmc_light) {
  1372. led_classdev_unregister(&applesmc_backlight);
  1373. destroy_workqueue(applesmc_led_wq);
  1374. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1375. }
  1376. if (applesmc_accelerometer)
  1377. applesmc_release_accelerometer();
  1378. sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
  1379. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
  1380. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
  1381. sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
  1382. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
  1383. platform_device_unregister(pdev);
  1384. platform_driver_unregister(&applesmc_driver);
  1385. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1386. printk(KERN_INFO "applesmc: driver unloaded.\n");
  1387. }
  1388. module_init(applesmc_init);
  1389. module_exit(applesmc_exit);
  1390. MODULE_AUTHOR("Nicolas Boichat");
  1391. MODULE_DESCRIPTION("Apple SMC");
  1392. MODULE_LICENSE("GPL v2");
  1393. MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);