applesmc.c 43 KB

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