applesmc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
  4. * sensors, fan control, keyboard backlight control) used in Intel-based Apple
  5. * computers.
  6. *
  7. * Copyright (C) 2007 Nicolas Boichat <[email protected]>
  8. * Copyright (C) 2010 Henrik Rydberg <[email protected]>
  9. *
  10. * Based on hdaps.c driver:
  11. * Copyright (C) 2005 Robert Love <[email protected]>
  12. * Copyright (C) 2005 Jesper Juhl <[email protected]>
  13. *
  14. * Fan control based on smcFanControl:
  15. * Copyright (C) 2006 Hendrik Holtmann <[email protected]>
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/delay.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/input.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/timer.h>
  25. #include <linux/dmi.h>
  26. #include <linux/mutex.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/io.h>
  29. #include <linux/leds.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/err.h>
  33. #include <linux/bits.h>
  34. /* data port used by Apple SMC */
  35. #define APPLESMC_DATA_PORT 0x300
  36. /* command/status port used by Apple SMC */
  37. #define APPLESMC_CMD_PORT 0x304
  38. #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
  39. #define APPLESMC_MAX_DATA_LENGTH 32
  40. /* Apple SMC status bits */
  41. #define SMC_STATUS_AWAITING_DATA BIT(0) /* SMC has data waiting to be read */
  42. #define SMC_STATUS_IB_CLOSED BIT(1) /* Will ignore any input */
  43. #define SMC_STATUS_BUSY BIT(2) /* Command in progress */
  44. /* Initial wait is 8us */
  45. #define APPLESMC_MIN_WAIT 0x0008
  46. #define APPLESMC_READ_CMD 0x10
  47. #define APPLESMC_WRITE_CMD 0x11
  48. #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
  49. #define APPLESMC_GET_KEY_TYPE_CMD 0x13
  50. #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
  51. #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
  52. #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
  53. #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
  54. #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
  55. #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
  56. #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
  57. #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
  58. #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
  59. #define FANS_COUNT "FNum" /* r-o ui8 */
  60. #define FANS_MANUAL "FS! " /* r-w ui16 */
  61. #define FAN_ID_FMT "F%dID" /* r-o char[16] */
  62. #define TEMP_SENSOR_TYPE "sp78"
  63. /* List of keys used to read/write fan speeds */
  64. static const char *const fan_speed_fmt[] = {
  65. "F%dAc", /* actual speed */
  66. "F%dMn", /* minimum speed (rw) */
  67. "F%dMx", /* maximum speed */
  68. "F%dSf", /* safe speed - not all models */
  69. "F%dTg", /* target speed (manual: rw) */
  70. };
  71. #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
  72. #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
  73. #define APPLESMC_POLL_INTERVAL 50 /* msecs */
  74. #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
  75. #define APPLESMC_INPUT_FLAT 4
  76. #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
  77. #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
  78. /* Dynamic device node attributes */
  79. struct applesmc_dev_attr {
  80. struct sensor_device_attribute sda; /* hwmon attributes */
  81. char name[32]; /* room for node file name */
  82. };
  83. /* Dynamic device node group */
  84. struct applesmc_node_group {
  85. char *format; /* format string */
  86. void *show; /* show function */
  87. void *store; /* store function */
  88. int option; /* function argument */
  89. struct applesmc_dev_attr *nodes; /* dynamic node array */
  90. };
  91. /* AppleSMC entry - cached register information */
  92. struct applesmc_entry {
  93. char key[5]; /* four-letter key code */
  94. u8 valid; /* set when entry is successfully read once */
  95. u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
  96. char type[5]; /* four-letter type code */
  97. u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
  98. };
  99. /* Register lookup and registers common to all SMCs */
  100. static struct applesmc_registers {
  101. struct mutex mutex; /* register read/write mutex */
  102. unsigned int key_count; /* number of SMC registers */
  103. unsigned int fan_count; /* number of fans */
  104. unsigned int temp_count; /* number of temperature registers */
  105. unsigned int temp_begin; /* temperature lower index bound */
  106. unsigned int temp_end; /* temperature upper index bound */
  107. unsigned int index_count; /* size of temperature index array */
  108. int num_light_sensors; /* number of light sensors */
  109. bool has_accelerometer; /* has motion sensor */
  110. bool has_key_backlight; /* has keyboard backlight */
  111. bool init_complete; /* true when fully initialized */
  112. struct applesmc_entry *cache; /* cached key entries */
  113. const char **index; /* temperature key index */
  114. } smcreg = {
  115. .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
  116. };
  117. static const int debug;
  118. static struct platform_device *pdev;
  119. static s16 rest_x;
  120. static s16 rest_y;
  121. static u8 backlight_state[2];
  122. static struct device *hwmon_dev;
  123. static struct input_dev *applesmc_idev;
  124. /*
  125. * Last index written to key_at_index sysfs file, and value to use for all other
  126. * key_at_index_* sysfs files.
  127. */
  128. static unsigned int key_at_index;
  129. static struct workqueue_struct *applesmc_led_wq;
  130. /*
  131. * Wait for specific status bits with a mask on the SMC.
  132. * Used before all transactions.
  133. * This does 10 fast loops of 8us then exponentially backs off for a
  134. * minimum total wait of 262ms. Depending on usleep_range this could
  135. * run out past 500ms.
  136. */
  137. static int wait_status(u8 val, u8 mask)
  138. {
  139. u8 status;
  140. int us;
  141. int i;
  142. us = APPLESMC_MIN_WAIT;
  143. for (i = 0; i < 24 ; i++) {
  144. status = inb(APPLESMC_CMD_PORT);
  145. if ((status & mask) == val)
  146. return 0;
  147. usleep_range(us, us * 2);
  148. if (i > 9)
  149. us <<= 1;
  150. }
  151. return -EIO;
  152. }
  153. /* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
  154. static int send_byte(u8 cmd, u16 port)
  155. {
  156. int status;
  157. status = wait_status(0, SMC_STATUS_IB_CLOSED);
  158. if (status)
  159. return status;
  160. /*
  161. * This needs to be a separate read looking for bit 0x04
  162. * after bit 0x02 falls. If consolidated with the wait above
  163. * this extra read may not happen if status returns both
  164. * simultaneously and this would appear to be required.
  165. */
  166. status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
  167. if (status)
  168. return status;
  169. outb(cmd, port);
  170. return 0;
  171. }
  172. /* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
  173. static int send_command(u8 cmd)
  174. {
  175. int ret;
  176. ret = wait_status(0, SMC_STATUS_IB_CLOSED);
  177. if (ret)
  178. return ret;
  179. outb(cmd, APPLESMC_CMD_PORT);
  180. return 0;
  181. }
  182. /*
  183. * Based on logic from the Apple driver. This is issued before any interaction
  184. * If busy is stuck high, issue a read command to reset the SMC state machine.
  185. * If busy is stuck high after the command then the SMC is jammed.
  186. */
  187. static int smc_sane(void)
  188. {
  189. int ret;
  190. ret = wait_status(0, SMC_STATUS_BUSY);
  191. if (!ret)
  192. return ret;
  193. ret = send_command(APPLESMC_READ_CMD);
  194. if (ret)
  195. return ret;
  196. return wait_status(0, SMC_STATUS_BUSY);
  197. }
  198. static int send_argument(const char *key)
  199. {
  200. int i;
  201. for (i = 0; i < 4; i++)
  202. if (send_byte(key[i], APPLESMC_DATA_PORT))
  203. return -EIO;
  204. return 0;
  205. }
  206. static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
  207. {
  208. u8 status, data = 0;
  209. int i;
  210. int ret;
  211. ret = smc_sane();
  212. if (ret)
  213. return ret;
  214. if (send_command(cmd) || send_argument(key)) {
  215. pr_warn("%.4s: read arg fail\n", key);
  216. return -EIO;
  217. }
  218. /* This has no effect on newer (2012) SMCs */
  219. if (send_byte(len, APPLESMC_DATA_PORT)) {
  220. pr_warn("%.4s: read len fail\n", key);
  221. return -EIO;
  222. }
  223. for (i = 0; i < len; i++) {
  224. if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
  225. SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
  226. pr_warn("%.4s: read data[%d] fail\n", key, i);
  227. return -EIO;
  228. }
  229. buffer[i] = inb(APPLESMC_DATA_PORT);
  230. }
  231. /* Read the data port until bit0 is cleared */
  232. for (i = 0; i < 16; i++) {
  233. udelay(APPLESMC_MIN_WAIT);
  234. status = inb(APPLESMC_CMD_PORT);
  235. if (!(status & SMC_STATUS_AWAITING_DATA))
  236. break;
  237. data = inb(APPLESMC_DATA_PORT);
  238. }
  239. if (i)
  240. pr_warn("flushed %d bytes, last value is: %d\n", i, data);
  241. return wait_status(0, SMC_STATUS_BUSY);
  242. }
  243. static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
  244. {
  245. int i;
  246. int ret;
  247. ret = smc_sane();
  248. if (ret)
  249. return ret;
  250. if (send_command(cmd) || send_argument(key)) {
  251. pr_warn("%s: write arg fail\n", key);
  252. return -EIO;
  253. }
  254. if (send_byte(len, APPLESMC_DATA_PORT)) {
  255. pr_warn("%.4s: write len fail\n", key);
  256. return -EIO;
  257. }
  258. for (i = 0; i < len; i++) {
  259. if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
  260. pr_warn("%s: write data fail\n", key);
  261. return -EIO;
  262. }
  263. }
  264. return wait_status(0, SMC_STATUS_BUSY);
  265. }
  266. static int read_register_count(unsigned int *count)
  267. {
  268. __be32 be;
  269. int ret;
  270. ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
  271. if (ret)
  272. return ret;
  273. *count = be32_to_cpu(be);
  274. return 0;
  275. }
  276. /*
  277. * Serialized I/O
  278. *
  279. * Returns zero on success or a negative error on failure.
  280. * All functions below are concurrency safe - callers should NOT hold lock.
  281. */
  282. static int applesmc_read_entry(const struct applesmc_entry *entry,
  283. u8 *buf, u8 len)
  284. {
  285. int ret;
  286. if (entry->len != len)
  287. return -EINVAL;
  288. mutex_lock(&smcreg.mutex);
  289. ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
  290. mutex_unlock(&smcreg.mutex);
  291. return ret;
  292. }
  293. static int applesmc_write_entry(const struct applesmc_entry *entry,
  294. const u8 *buf, u8 len)
  295. {
  296. int ret;
  297. if (entry->len != len)
  298. return -EINVAL;
  299. mutex_lock(&smcreg.mutex);
  300. ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
  301. mutex_unlock(&smcreg.mutex);
  302. return ret;
  303. }
  304. static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
  305. {
  306. struct applesmc_entry *cache = &smcreg.cache[index];
  307. u8 key[4], info[6];
  308. __be32 be;
  309. int ret = 0;
  310. if (cache->valid)
  311. return cache;
  312. mutex_lock(&smcreg.mutex);
  313. if (cache->valid)
  314. goto out;
  315. be = cpu_to_be32(index);
  316. ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
  317. if (ret)
  318. goto out;
  319. ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
  320. if (ret)
  321. goto out;
  322. memcpy(cache->key, key, 4);
  323. cache->len = info[0];
  324. memcpy(cache->type, &info[1], 4);
  325. cache->flags = info[5];
  326. cache->valid = true;
  327. out:
  328. mutex_unlock(&smcreg.mutex);
  329. if (ret)
  330. return ERR_PTR(ret);
  331. return cache;
  332. }
  333. static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
  334. {
  335. int begin = 0, end = smcreg.key_count;
  336. const struct applesmc_entry *entry;
  337. while (begin != end) {
  338. int middle = begin + (end - begin) / 2;
  339. entry = applesmc_get_entry_by_index(middle);
  340. if (IS_ERR(entry)) {
  341. *lo = 0;
  342. return PTR_ERR(entry);
  343. }
  344. if (strcmp(entry->key, key) < 0)
  345. begin = middle + 1;
  346. else
  347. end = middle;
  348. }
  349. *lo = begin;
  350. return 0;
  351. }
  352. static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
  353. {
  354. int begin = 0, end = smcreg.key_count;
  355. const struct applesmc_entry *entry;
  356. while (begin != end) {
  357. int middle = begin + (end - begin) / 2;
  358. entry = applesmc_get_entry_by_index(middle);
  359. if (IS_ERR(entry)) {
  360. *hi = smcreg.key_count;
  361. return PTR_ERR(entry);
  362. }
  363. if (strcmp(key, entry->key) < 0)
  364. end = middle;
  365. else
  366. begin = middle + 1;
  367. }
  368. *hi = begin;
  369. return 0;
  370. }
  371. static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
  372. {
  373. int begin, end;
  374. int ret;
  375. ret = applesmc_get_lower_bound(&begin, key);
  376. if (ret)
  377. return ERR_PTR(ret);
  378. ret = applesmc_get_upper_bound(&end, key);
  379. if (ret)
  380. return ERR_PTR(ret);
  381. if (end - begin != 1)
  382. return ERR_PTR(-EINVAL);
  383. return applesmc_get_entry_by_index(begin);
  384. }
  385. static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
  386. {
  387. const struct applesmc_entry *entry;
  388. entry = applesmc_get_entry_by_key(key);
  389. if (IS_ERR(entry))
  390. return PTR_ERR(entry);
  391. return applesmc_read_entry(entry, buffer, len);
  392. }
  393. static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
  394. {
  395. const struct applesmc_entry *entry;
  396. entry = applesmc_get_entry_by_key(key);
  397. if (IS_ERR(entry))
  398. return PTR_ERR(entry);
  399. return applesmc_write_entry(entry, buffer, len);
  400. }
  401. static int applesmc_has_key(const char *key, bool *value)
  402. {
  403. const struct applesmc_entry *entry;
  404. entry = applesmc_get_entry_by_key(key);
  405. if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
  406. return PTR_ERR(entry);
  407. *value = !IS_ERR(entry);
  408. return 0;
  409. }
  410. /*
  411. * applesmc_read_s16 - Read 16-bit signed big endian register
  412. */
  413. static int applesmc_read_s16(const char *key, s16 *value)
  414. {
  415. u8 buffer[2];
  416. int ret;
  417. ret = applesmc_read_key(key, buffer, 2);
  418. if (ret)
  419. return ret;
  420. *value = ((s16)buffer[0] << 8) | buffer[1];
  421. return 0;
  422. }
  423. /*
  424. * applesmc_device_init - initialize the accelerometer. Can sleep.
  425. */
  426. static void applesmc_device_init(void)
  427. {
  428. int total;
  429. u8 buffer[2];
  430. if (!smcreg.has_accelerometer)
  431. return;
  432. for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
  433. if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
  434. (buffer[0] != 0x00 || buffer[1] != 0x00))
  435. return;
  436. buffer[0] = 0xe0;
  437. buffer[1] = 0x00;
  438. applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
  439. msleep(INIT_WAIT_MSECS);
  440. }
  441. pr_warn("failed to init the device\n");
  442. }
  443. static int applesmc_init_index(struct applesmc_registers *s)
  444. {
  445. const struct applesmc_entry *entry;
  446. unsigned int i;
  447. if (s->index)
  448. return 0;
  449. s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
  450. if (!s->index)
  451. return -ENOMEM;
  452. for (i = s->temp_begin; i < s->temp_end; i++) {
  453. entry = applesmc_get_entry_by_index(i);
  454. if (IS_ERR(entry))
  455. continue;
  456. if (strcmp(entry->type, TEMP_SENSOR_TYPE))
  457. continue;
  458. s->index[s->index_count++] = entry->key;
  459. }
  460. return 0;
  461. }
  462. /*
  463. * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
  464. */
  465. static int applesmc_init_smcreg_try(void)
  466. {
  467. struct applesmc_registers *s = &smcreg;
  468. bool left_light_sensor = false, right_light_sensor = false;
  469. unsigned int count;
  470. u8 tmp[1];
  471. int ret;
  472. if (s->init_complete)
  473. return 0;
  474. ret = read_register_count(&count);
  475. if (ret)
  476. return ret;
  477. if (s->cache && s->key_count != count) {
  478. pr_warn("key count changed from %d to %d\n",
  479. s->key_count, count);
  480. kfree(s->cache);
  481. s->cache = NULL;
  482. }
  483. s->key_count = count;
  484. if (!s->cache)
  485. s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
  486. if (!s->cache)
  487. return -ENOMEM;
  488. ret = applesmc_read_key(FANS_COUNT, tmp, 1);
  489. if (ret)
  490. return ret;
  491. s->fan_count = tmp[0];
  492. if (s->fan_count > 10)
  493. s->fan_count = 10;
  494. ret = applesmc_get_lower_bound(&s->temp_begin, "T");
  495. if (ret)
  496. return ret;
  497. ret = applesmc_get_lower_bound(&s->temp_end, "U");
  498. if (ret)
  499. return ret;
  500. s->temp_count = s->temp_end - s->temp_begin;
  501. ret = applesmc_init_index(s);
  502. if (ret)
  503. return ret;
  504. ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
  505. if (ret)
  506. return ret;
  507. ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
  508. if (ret)
  509. return ret;
  510. ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
  511. if (ret)
  512. return ret;
  513. ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
  514. if (ret)
  515. return ret;
  516. s->num_light_sensors = left_light_sensor + right_light_sensor;
  517. s->init_complete = true;
  518. pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
  519. s->key_count, s->fan_count, s->temp_count, s->index_count,
  520. s->has_accelerometer,
  521. s->num_light_sensors,
  522. s->has_key_backlight);
  523. return 0;
  524. }
  525. static void applesmc_destroy_smcreg(void)
  526. {
  527. kfree(smcreg.index);
  528. smcreg.index = NULL;
  529. kfree(smcreg.cache);
  530. smcreg.cache = NULL;
  531. smcreg.init_complete = false;
  532. }
  533. /*
  534. * applesmc_init_smcreg - Initialize register cache.
  535. *
  536. * Retries until initialization is successful, or the operation times out.
  537. *
  538. */
  539. static int applesmc_init_smcreg(void)
  540. {
  541. int ms, ret;
  542. for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
  543. ret = applesmc_init_smcreg_try();
  544. if (!ret) {
  545. if (ms)
  546. pr_info("init_smcreg() took %d ms\n", ms);
  547. return 0;
  548. }
  549. msleep(INIT_WAIT_MSECS);
  550. }
  551. applesmc_destroy_smcreg();
  552. return ret;
  553. }
  554. /* Device model stuff */
  555. static int applesmc_probe(struct platform_device *dev)
  556. {
  557. int ret;
  558. ret = applesmc_init_smcreg();
  559. if (ret)
  560. return ret;
  561. applesmc_device_init();
  562. return 0;
  563. }
  564. /* Synchronize device with memorized backlight state */
  565. static int applesmc_pm_resume(struct device *dev)
  566. {
  567. if (smcreg.has_key_backlight)
  568. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  569. return 0;
  570. }
  571. /* Reinitialize device on resume from hibernation */
  572. static int applesmc_pm_restore(struct device *dev)
  573. {
  574. applesmc_device_init();
  575. return applesmc_pm_resume(dev);
  576. }
  577. static const struct dev_pm_ops applesmc_pm_ops = {
  578. .resume = applesmc_pm_resume,
  579. .restore = applesmc_pm_restore,
  580. };
  581. static struct platform_driver applesmc_driver = {
  582. .probe = applesmc_probe,
  583. .driver = {
  584. .name = "applesmc",
  585. .pm = &applesmc_pm_ops,
  586. },
  587. };
  588. /*
  589. * applesmc_calibrate - Set our "resting" values. Callers must
  590. * hold applesmc_lock.
  591. */
  592. static void applesmc_calibrate(void)
  593. {
  594. applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
  595. applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
  596. rest_x = -rest_x;
  597. }
  598. static void applesmc_idev_poll(struct input_dev *idev)
  599. {
  600. s16 x, y;
  601. if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
  602. return;
  603. if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
  604. return;
  605. x = -x;
  606. input_report_abs(idev, ABS_X, x - rest_x);
  607. input_report_abs(idev, ABS_Y, y - rest_y);
  608. input_sync(idev);
  609. }
  610. /* Sysfs Files */
  611. static ssize_t applesmc_name_show(struct device *dev,
  612. struct device_attribute *attr, char *buf)
  613. {
  614. return sysfs_emit(buf, "applesmc\n");
  615. }
  616. static ssize_t applesmc_position_show(struct device *dev,
  617. struct device_attribute *attr, char *buf)
  618. {
  619. int ret;
  620. s16 x, y, z;
  621. ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
  622. if (ret)
  623. goto out;
  624. ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
  625. if (ret)
  626. goto out;
  627. ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
  628. if (ret)
  629. goto out;
  630. out:
  631. if (ret)
  632. return ret;
  633. return sysfs_emit(buf, "(%d,%d,%d)\n", x, y, z);
  634. }
  635. static ssize_t applesmc_light_show(struct device *dev,
  636. struct device_attribute *attr, char *sysfsbuf)
  637. {
  638. const struct applesmc_entry *entry;
  639. static int data_length;
  640. int ret;
  641. u8 left = 0, right = 0;
  642. u8 buffer[10];
  643. if (!data_length) {
  644. entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
  645. if (IS_ERR(entry))
  646. return PTR_ERR(entry);
  647. if (entry->len > 10)
  648. return -ENXIO;
  649. data_length = entry->len;
  650. pr_info("light sensor data length set to %d\n", data_length);
  651. }
  652. ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
  653. if (ret)
  654. goto out;
  655. /* newer macbooks report a single 10-bit bigendian value */
  656. if (data_length == 10) {
  657. left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
  658. goto out;
  659. }
  660. left = buffer[2];
  661. ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
  662. if (ret)
  663. goto out;
  664. right = buffer[2];
  665. out:
  666. if (ret)
  667. return ret;
  668. return sysfs_emit(sysfsbuf, "(%d,%d)\n", left, right);
  669. }
  670. /* Displays sensor key as label */
  671. static ssize_t applesmc_show_sensor_label(struct device *dev,
  672. struct device_attribute *devattr, char *sysfsbuf)
  673. {
  674. const char *key = smcreg.index[to_index(devattr)];
  675. return sysfs_emit(sysfsbuf, "%s\n", key);
  676. }
  677. /* Displays degree Celsius * 1000 */
  678. static ssize_t applesmc_show_temperature(struct device *dev,
  679. struct device_attribute *devattr, char *sysfsbuf)
  680. {
  681. const char *key = smcreg.index[to_index(devattr)];
  682. int ret;
  683. s16 value;
  684. int temp;
  685. ret = applesmc_read_s16(key, &value);
  686. if (ret)
  687. return ret;
  688. temp = 250 * (value >> 6);
  689. return sysfs_emit(sysfsbuf, "%d\n", temp);
  690. }
  691. static ssize_t applesmc_show_fan_speed(struct device *dev,
  692. struct device_attribute *attr, char *sysfsbuf)
  693. {
  694. int ret;
  695. unsigned int speed = 0;
  696. char newkey[5];
  697. u8 buffer[2];
  698. scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
  699. to_index(attr));
  700. ret = applesmc_read_key(newkey, buffer, 2);
  701. if (ret)
  702. return ret;
  703. speed = ((buffer[0] << 8 | buffer[1]) >> 2);
  704. return sysfs_emit(sysfsbuf, "%u\n", speed);
  705. }
  706. static ssize_t applesmc_store_fan_speed(struct device *dev,
  707. struct device_attribute *attr,
  708. const char *sysfsbuf, size_t count)
  709. {
  710. int ret;
  711. unsigned long speed;
  712. char newkey[5];
  713. u8 buffer[2];
  714. if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
  715. return -EINVAL; /* Bigger than a 14-bit value */
  716. scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
  717. to_index(attr));
  718. buffer[0] = (speed >> 6) & 0xff;
  719. buffer[1] = (speed << 2) & 0xff;
  720. ret = applesmc_write_key(newkey, buffer, 2);
  721. if (ret)
  722. return ret;
  723. else
  724. return count;
  725. }
  726. static ssize_t applesmc_show_fan_manual(struct device *dev,
  727. struct device_attribute *attr, char *sysfsbuf)
  728. {
  729. int ret;
  730. u16 manual = 0;
  731. u8 buffer[2];
  732. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  733. if (ret)
  734. return ret;
  735. manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
  736. return sysfs_emit(sysfsbuf, "%d\n", manual);
  737. }
  738. static ssize_t applesmc_store_fan_manual(struct device *dev,
  739. struct device_attribute *attr,
  740. const char *sysfsbuf, size_t count)
  741. {
  742. int ret;
  743. u8 buffer[2];
  744. unsigned long input;
  745. u16 val;
  746. if (kstrtoul(sysfsbuf, 10, &input) < 0)
  747. return -EINVAL;
  748. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  749. if (ret)
  750. goto out;
  751. val = (buffer[0] << 8 | buffer[1]);
  752. if (input)
  753. val = val | (0x01 << to_index(attr));
  754. else
  755. val = val & ~(0x01 << to_index(attr));
  756. buffer[0] = (val >> 8) & 0xFF;
  757. buffer[1] = val & 0xFF;
  758. ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
  759. out:
  760. if (ret)
  761. return ret;
  762. else
  763. return count;
  764. }
  765. static ssize_t applesmc_show_fan_position(struct device *dev,
  766. struct device_attribute *attr, char *sysfsbuf)
  767. {
  768. int ret;
  769. char newkey[5];
  770. u8 buffer[17];
  771. scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, to_index(attr));
  772. ret = applesmc_read_key(newkey, buffer, 16);
  773. buffer[16] = 0;
  774. if (ret)
  775. return ret;
  776. return sysfs_emit(sysfsbuf, "%s\n", buffer + 4);
  777. }
  778. static ssize_t applesmc_calibrate_show(struct device *dev,
  779. struct device_attribute *attr, char *sysfsbuf)
  780. {
  781. return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y);
  782. }
  783. static ssize_t applesmc_calibrate_store(struct device *dev,
  784. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  785. {
  786. applesmc_calibrate();
  787. return count;
  788. }
  789. static void applesmc_backlight_set(struct work_struct *work)
  790. {
  791. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  792. }
  793. static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
  794. static void applesmc_brightness_set(struct led_classdev *led_cdev,
  795. enum led_brightness value)
  796. {
  797. int ret;
  798. backlight_state[0] = value;
  799. ret = queue_work(applesmc_led_wq, &backlight_work);
  800. if (debug && (!ret))
  801. dev_dbg(led_cdev->dev, "work was already on the queue.\n");
  802. }
  803. static ssize_t applesmc_key_count_show(struct device *dev,
  804. struct device_attribute *attr, char *sysfsbuf)
  805. {
  806. int ret;
  807. u8 buffer[4];
  808. u32 count;
  809. ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
  810. if (ret)
  811. return ret;
  812. count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
  813. ((u32)buffer[2]<<8) + buffer[3];
  814. return sysfs_emit(sysfsbuf, "%d\n", count);
  815. }
  816. static ssize_t applesmc_key_at_index_read_show(struct device *dev,
  817. struct device_attribute *attr, char *sysfsbuf)
  818. {
  819. const struct applesmc_entry *entry;
  820. int ret;
  821. entry = applesmc_get_entry_by_index(key_at_index);
  822. if (IS_ERR(entry))
  823. return PTR_ERR(entry);
  824. ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
  825. if (ret)
  826. return ret;
  827. return entry->len;
  828. }
  829. static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
  830. struct device_attribute *attr, char *sysfsbuf)
  831. {
  832. const struct applesmc_entry *entry;
  833. entry = applesmc_get_entry_by_index(key_at_index);
  834. if (IS_ERR(entry))
  835. return PTR_ERR(entry);
  836. return sysfs_emit(sysfsbuf, "%d\n", entry->len);
  837. }
  838. static ssize_t applesmc_key_at_index_type_show(struct device *dev,
  839. struct device_attribute *attr, char *sysfsbuf)
  840. {
  841. const struct applesmc_entry *entry;
  842. entry = applesmc_get_entry_by_index(key_at_index);
  843. if (IS_ERR(entry))
  844. return PTR_ERR(entry);
  845. return sysfs_emit(sysfsbuf, "%s\n", entry->type);
  846. }
  847. static ssize_t applesmc_key_at_index_name_show(struct device *dev,
  848. struct device_attribute *attr, char *sysfsbuf)
  849. {
  850. const struct applesmc_entry *entry;
  851. entry = applesmc_get_entry_by_index(key_at_index);
  852. if (IS_ERR(entry))
  853. return PTR_ERR(entry);
  854. return sysfs_emit(sysfsbuf, "%s\n", entry->key);
  855. }
  856. static ssize_t applesmc_key_at_index_show(struct device *dev,
  857. struct device_attribute *attr, char *sysfsbuf)
  858. {
  859. return sysfs_emit(sysfsbuf, "%d\n", key_at_index);
  860. }
  861. static ssize_t applesmc_key_at_index_store(struct device *dev,
  862. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  863. {
  864. unsigned long newkey;
  865. if (kstrtoul(sysfsbuf, 10, &newkey) < 0
  866. || newkey >= smcreg.key_count)
  867. return -EINVAL;
  868. key_at_index = newkey;
  869. return count;
  870. }
  871. static struct led_classdev applesmc_backlight = {
  872. .name = "smc::kbd_backlight",
  873. .default_trigger = "nand-disk",
  874. .brightness_set = applesmc_brightness_set,
  875. };
  876. static struct applesmc_node_group info_group[] = {
  877. { "name", applesmc_name_show },
  878. { "key_count", applesmc_key_count_show },
  879. { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
  880. { "key_at_index_name", applesmc_key_at_index_name_show },
  881. { "key_at_index_type", applesmc_key_at_index_type_show },
  882. { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
  883. { "key_at_index_data", applesmc_key_at_index_read_show },
  884. { }
  885. };
  886. static struct applesmc_node_group accelerometer_group[] = {
  887. { "position", applesmc_position_show },
  888. { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
  889. { }
  890. };
  891. static struct applesmc_node_group light_sensor_group[] = {
  892. { "light", applesmc_light_show },
  893. { }
  894. };
  895. static struct applesmc_node_group fan_group[] = {
  896. { "fan%d_label", applesmc_show_fan_position },
  897. { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
  898. { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
  899. { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
  900. { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
  901. { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
  902. { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
  903. { }
  904. };
  905. static struct applesmc_node_group temp_group[] = {
  906. { "temp%d_label", applesmc_show_sensor_label },
  907. { "temp%d_input", applesmc_show_temperature },
  908. { }
  909. };
  910. /* Module stuff */
  911. /*
  912. * applesmc_destroy_nodes - remove files and free associated memory
  913. */
  914. static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
  915. {
  916. struct applesmc_node_group *grp;
  917. struct applesmc_dev_attr *node;
  918. for (grp = groups; grp->nodes; grp++) {
  919. for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
  920. sysfs_remove_file(&pdev->dev.kobj,
  921. &node->sda.dev_attr.attr);
  922. kfree(grp->nodes);
  923. grp->nodes = NULL;
  924. }
  925. }
  926. /*
  927. * applesmc_create_nodes - create a two-dimensional group of sysfs files
  928. */
  929. static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
  930. {
  931. struct applesmc_node_group *grp;
  932. struct applesmc_dev_attr *node;
  933. struct attribute *attr;
  934. int ret, i;
  935. for (grp = groups; grp->format; grp++) {
  936. grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
  937. if (!grp->nodes) {
  938. ret = -ENOMEM;
  939. goto out;
  940. }
  941. for (i = 0; i < num; i++) {
  942. node = &grp->nodes[i];
  943. scnprintf(node->name, sizeof(node->name), grp->format,
  944. i + 1);
  945. node->sda.index = (grp->option << 16) | (i & 0xffff);
  946. node->sda.dev_attr.show = grp->show;
  947. node->sda.dev_attr.store = grp->store;
  948. attr = &node->sda.dev_attr.attr;
  949. sysfs_attr_init(attr);
  950. attr->name = node->name;
  951. attr->mode = 0444 | (grp->store ? 0200 : 0);
  952. ret = sysfs_create_file(&pdev->dev.kobj, attr);
  953. if (ret) {
  954. attr->name = NULL;
  955. goto out;
  956. }
  957. }
  958. }
  959. return 0;
  960. out:
  961. applesmc_destroy_nodes(groups);
  962. return ret;
  963. }
  964. /* Create accelerometer resources */
  965. static int applesmc_create_accelerometer(void)
  966. {
  967. int ret;
  968. if (!smcreg.has_accelerometer)
  969. return 0;
  970. ret = applesmc_create_nodes(accelerometer_group, 1);
  971. if (ret)
  972. goto out;
  973. applesmc_idev = input_allocate_device();
  974. if (!applesmc_idev) {
  975. ret = -ENOMEM;
  976. goto out_sysfs;
  977. }
  978. /* initial calibrate for the input device */
  979. applesmc_calibrate();
  980. /* initialize the input device */
  981. applesmc_idev->name = "applesmc";
  982. applesmc_idev->id.bustype = BUS_HOST;
  983. applesmc_idev->dev.parent = &pdev->dev;
  984. input_set_abs_params(applesmc_idev, ABS_X,
  985. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  986. input_set_abs_params(applesmc_idev, ABS_Y,
  987. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  988. ret = input_setup_polling(applesmc_idev, applesmc_idev_poll);
  989. if (ret)
  990. goto out_idev;
  991. input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL);
  992. ret = input_register_device(applesmc_idev);
  993. if (ret)
  994. goto out_idev;
  995. return 0;
  996. out_idev:
  997. input_free_device(applesmc_idev);
  998. out_sysfs:
  999. applesmc_destroy_nodes(accelerometer_group);
  1000. out:
  1001. pr_warn("driver init failed (ret=%d)!\n", ret);
  1002. return ret;
  1003. }
  1004. /* Release all resources used by the accelerometer */
  1005. static void applesmc_release_accelerometer(void)
  1006. {
  1007. if (!smcreg.has_accelerometer)
  1008. return;
  1009. input_unregister_device(applesmc_idev);
  1010. applesmc_destroy_nodes(accelerometer_group);
  1011. }
  1012. static int applesmc_create_light_sensor(void)
  1013. {
  1014. if (!smcreg.num_light_sensors)
  1015. return 0;
  1016. return applesmc_create_nodes(light_sensor_group, 1);
  1017. }
  1018. static void applesmc_release_light_sensor(void)
  1019. {
  1020. if (!smcreg.num_light_sensors)
  1021. return;
  1022. applesmc_destroy_nodes(light_sensor_group);
  1023. }
  1024. static int applesmc_create_key_backlight(void)
  1025. {
  1026. if (!smcreg.has_key_backlight)
  1027. return 0;
  1028. applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
  1029. if (!applesmc_led_wq)
  1030. return -ENOMEM;
  1031. return led_classdev_register(&pdev->dev, &applesmc_backlight);
  1032. }
  1033. static void applesmc_release_key_backlight(void)
  1034. {
  1035. if (!smcreg.has_key_backlight)
  1036. return;
  1037. led_classdev_unregister(&applesmc_backlight);
  1038. destroy_workqueue(applesmc_led_wq);
  1039. }
  1040. static int applesmc_dmi_match(const struct dmi_system_id *id)
  1041. {
  1042. return 1;
  1043. }
  1044. /*
  1045. * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
  1046. * So we need to put "Apple MacBook Pro" before "Apple MacBook".
  1047. */
  1048. static const struct dmi_system_id applesmc_whitelist[] __initconst = {
  1049. { applesmc_dmi_match, "Apple MacBook Air", {
  1050. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1051. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
  1052. },
  1053. { applesmc_dmi_match, "Apple MacBook Pro", {
  1054. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1055. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
  1056. },
  1057. { applesmc_dmi_match, "Apple MacBook", {
  1058. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1059. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
  1060. },
  1061. { applesmc_dmi_match, "Apple Macmini", {
  1062. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1063. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
  1064. },
  1065. { applesmc_dmi_match, "Apple MacPro", {
  1066. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1067. DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
  1068. },
  1069. { applesmc_dmi_match, "Apple iMac", {
  1070. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1071. DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
  1072. },
  1073. { applesmc_dmi_match, "Apple Xserve", {
  1074. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1075. DMI_MATCH(DMI_PRODUCT_NAME, "Xserve") },
  1076. },
  1077. { .ident = NULL }
  1078. };
  1079. static int __init applesmc_init(void)
  1080. {
  1081. int ret;
  1082. if (!dmi_check_system(applesmc_whitelist)) {
  1083. pr_warn("supported laptop not found!\n");
  1084. ret = -ENODEV;
  1085. goto out;
  1086. }
  1087. if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
  1088. "applesmc")) {
  1089. ret = -ENXIO;
  1090. goto out;
  1091. }
  1092. ret = platform_driver_register(&applesmc_driver);
  1093. if (ret)
  1094. goto out_region;
  1095. pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
  1096. NULL, 0);
  1097. if (IS_ERR(pdev)) {
  1098. ret = PTR_ERR(pdev);
  1099. goto out_driver;
  1100. }
  1101. /* create register cache */
  1102. ret = applesmc_init_smcreg();
  1103. if (ret)
  1104. goto out_device;
  1105. ret = applesmc_create_nodes(info_group, 1);
  1106. if (ret)
  1107. goto out_smcreg;
  1108. ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
  1109. if (ret)
  1110. goto out_info;
  1111. ret = applesmc_create_nodes(temp_group, smcreg.index_count);
  1112. if (ret)
  1113. goto out_fans;
  1114. ret = applesmc_create_accelerometer();
  1115. if (ret)
  1116. goto out_temperature;
  1117. ret = applesmc_create_light_sensor();
  1118. if (ret)
  1119. goto out_accelerometer;
  1120. ret = applesmc_create_key_backlight();
  1121. if (ret)
  1122. goto out_light_sysfs;
  1123. hwmon_dev = hwmon_device_register(&pdev->dev);
  1124. if (IS_ERR(hwmon_dev)) {
  1125. ret = PTR_ERR(hwmon_dev);
  1126. goto out_light_ledclass;
  1127. }
  1128. return 0;
  1129. out_light_ledclass:
  1130. applesmc_release_key_backlight();
  1131. out_light_sysfs:
  1132. applesmc_release_light_sensor();
  1133. out_accelerometer:
  1134. applesmc_release_accelerometer();
  1135. out_temperature:
  1136. applesmc_destroy_nodes(temp_group);
  1137. out_fans:
  1138. applesmc_destroy_nodes(fan_group);
  1139. out_info:
  1140. applesmc_destroy_nodes(info_group);
  1141. out_smcreg:
  1142. applesmc_destroy_smcreg();
  1143. out_device:
  1144. platform_device_unregister(pdev);
  1145. out_driver:
  1146. platform_driver_unregister(&applesmc_driver);
  1147. out_region:
  1148. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1149. out:
  1150. pr_warn("driver init failed (ret=%d)!\n", ret);
  1151. return ret;
  1152. }
  1153. static void __exit applesmc_exit(void)
  1154. {
  1155. hwmon_device_unregister(hwmon_dev);
  1156. applesmc_release_key_backlight();
  1157. applesmc_release_light_sensor();
  1158. applesmc_release_accelerometer();
  1159. applesmc_destroy_nodes(temp_group);
  1160. applesmc_destroy_nodes(fan_group);
  1161. applesmc_destroy_nodes(info_group);
  1162. applesmc_destroy_smcreg();
  1163. platform_device_unregister(pdev);
  1164. platform_driver_unregister(&applesmc_driver);
  1165. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1166. }
  1167. module_init(applesmc_init);
  1168. module_exit(applesmc_exit);
  1169. MODULE_AUTHOR("Nicolas Boichat");
  1170. MODULE_DESCRIPTION("Apple SMC");
  1171. MODULE_LICENSE("GPL v2");
  1172. MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);