applesmc.c 35 KB

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