applesmc.c 34 KB

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