applesmc.c 41 KB

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