applesmc.c 38 KB

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