applesmc.c 44 KB

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