applesmc.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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");