applesmc.c 34 KB

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