applesmc.c 34 KB

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