applesmc.c 40 KB

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