applesmc.c 46 KB

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