applesmc.c 43 KB

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