applesmc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. if (ret)
  994. goto out_device;
  995. /* Create key enumeration sysfs files */
  996. ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
  997. if (ret)
  998. goto out_name;
  999. /* create fan files */
  1000. count = applesmc_get_fan_count();
  1001. if (count < 0) {
  1002. printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
  1003. } else {
  1004. printk(KERN_INFO "applesmc: %d fans found.\n", count);
  1005. switch (count) {
  1006. default:
  1007. printk(KERN_WARNING "applesmc: More than 2 fans found,"
  1008. " but at most 2 fans are supported"
  1009. " by the driver.\n");
  1010. case 2:
  1011. ret = sysfs_create_group(&pdev->dev.kobj,
  1012. &fan_attribute_groups[1]);
  1013. if (ret)
  1014. goto out_key_enumeration;
  1015. case 1:
  1016. ret = sysfs_create_group(&pdev->dev.kobj,
  1017. &fan_attribute_groups[0]);
  1018. if (ret)
  1019. goto out_fan_1;
  1020. case 0:
  1021. ;
  1022. }
  1023. }
  1024. for (i = 0;
  1025. temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
  1026. i++) {
  1027. if (temperature_attributes[i] == NULL) {
  1028. printk(KERN_ERR "applesmc: More temperature sensors "
  1029. "in temperature_sensors_sets (at least %i)"
  1030. "than available sysfs files in "
  1031. "temperature_attributes (%i), please report "
  1032. "this bug.\n", i, i-1);
  1033. goto out_temperature;
  1034. }
  1035. ret = sysfs_create_file(&pdev->dev.kobj,
  1036. temperature_attributes[i]);
  1037. if (ret)
  1038. goto out_temperature;
  1039. }
  1040. if (applesmc_accelerometer) {
  1041. ret = applesmc_create_accelerometer();
  1042. if (ret)
  1043. goto out_temperature;
  1044. }
  1045. if (applesmc_light) {
  1046. /* Add light sensor file */
  1047. ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1048. if (ret)
  1049. goto out_accelerometer;
  1050. /* Create the workqueue */
  1051. applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
  1052. if (!applesmc_led_wq) {
  1053. ret = -ENOMEM;
  1054. goto out_light_sysfs;
  1055. }
  1056. /* register as a led device */
  1057. ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
  1058. if (ret < 0)
  1059. goto out_light_wq;
  1060. }
  1061. hwmon_class_dev = hwmon_device_register(&pdev->dev);
  1062. if (IS_ERR(hwmon_class_dev)) {
  1063. ret = PTR_ERR(hwmon_class_dev);
  1064. goto out_light_ledclass;
  1065. }
  1066. printk(KERN_INFO "applesmc: driver successfully loaded.\n");
  1067. return 0;
  1068. out_light_ledclass:
  1069. if (applesmc_light)
  1070. led_classdev_unregister(&applesmc_backlight);
  1071. out_light_wq:
  1072. if (applesmc_light)
  1073. destroy_workqueue(applesmc_led_wq);
  1074. out_light_sysfs:
  1075. if (applesmc_light)
  1076. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1077. out_accelerometer:
  1078. if (applesmc_accelerometer)
  1079. applesmc_release_accelerometer();
  1080. out_temperature:
  1081. sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
  1082. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
  1083. out_fan_1:
  1084. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
  1085. out_key_enumeration:
  1086. sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
  1087. out_name:
  1088. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
  1089. out_device:
  1090. platform_device_unregister(pdev);
  1091. out_driver:
  1092. platform_driver_unregister(&applesmc_driver);
  1093. out_region:
  1094. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1095. out:
  1096. printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
  1097. return ret;
  1098. }
  1099. static void __exit applesmc_exit(void)
  1100. {
  1101. hwmon_device_unregister(hwmon_class_dev);
  1102. if (applesmc_light) {
  1103. led_classdev_unregister(&applesmc_backlight);
  1104. destroy_workqueue(applesmc_led_wq);
  1105. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
  1106. }
  1107. if (applesmc_accelerometer)
  1108. applesmc_release_accelerometer();
  1109. sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
  1110. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
  1111. sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
  1112. sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
  1113. sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
  1114. platform_device_unregister(pdev);
  1115. platform_driver_unregister(&applesmc_driver);
  1116. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1117. printk(KERN_INFO "applesmc: driver unloaded.\n");
  1118. }
  1119. module_init(applesmc_init);
  1120. module_exit(applesmc_exit);
  1121. MODULE_AUTHOR("Nicolas Boichat");
  1122. MODULE_DESCRIPTION("Apple SMC");
  1123. MODULE_LICENSE("GPL v2");