applesmc.c 54 KB

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