applesmc.c 54 KB

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