applesmc.c 41 KB

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