applesmc.c 44 KB

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