applesmc.c 53 KB

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