olpc-xo1-sci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Support for OLPC XO-1 System Control Interrupts (SCI)
  3. *
  4. * Copyright (C) 2010 One Laptop per Child
  5. * Copyright (C) 2006 Red Hat, Inc.
  6. * Copyright (C) 2006 Advanced Micro Devices, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/cs5535.h>
  14. #include <linux/device.h>
  15. #include <linux/gpio.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_wakeup.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/power_supply.h>
  23. #include <linux/suspend.h>
  24. #include <linux/workqueue.h>
  25. #include <asm/io.h>
  26. #include <asm/msr.h>
  27. #include <asm/olpc.h>
  28. #define DRV_NAME "olpc-xo1-sci"
  29. #define PFX DRV_NAME ": "
  30. static unsigned long acpi_base;
  31. static struct input_dev *power_button_idev;
  32. static struct input_dev *ebook_switch_idev;
  33. static struct input_dev *lid_switch_idev;
  34. static int sci_irq;
  35. static bool lid_open;
  36. static bool lid_inverted;
  37. static int lid_wake_mode;
  38. enum lid_wake_modes {
  39. LID_WAKE_ALWAYS,
  40. LID_WAKE_OPEN,
  41. LID_WAKE_CLOSE,
  42. };
  43. static const char * const lid_wake_mode_names[] = {
  44. [LID_WAKE_ALWAYS] = "always",
  45. [LID_WAKE_OPEN] = "open",
  46. [LID_WAKE_CLOSE] = "close",
  47. };
  48. static void battery_status_changed(void)
  49. {
  50. struct power_supply *psy = power_supply_get_by_name("olpc-battery");
  51. if (psy) {
  52. power_supply_changed(psy);
  53. put_device(psy->dev);
  54. }
  55. }
  56. static void ac_status_changed(void)
  57. {
  58. struct power_supply *psy = power_supply_get_by_name("olpc-ac");
  59. if (psy) {
  60. power_supply_changed(psy);
  61. put_device(psy->dev);
  62. }
  63. }
  64. /* Report current ebook switch state through input layer */
  65. static void send_ebook_state(void)
  66. {
  67. unsigned char state;
  68. if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
  69. pr_err(PFX "failed to get ebook state\n");
  70. return;
  71. }
  72. if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
  73. return; /* Nothing new to report. */
  74. input_report_switch(ebook_switch_idev, SW_TABLET_MODE, state);
  75. input_sync(ebook_switch_idev);
  76. pm_wakeup_event(&ebook_switch_idev->dev, 0);
  77. }
  78. static void flip_lid_inverter(void)
  79. {
  80. /* gpio is high; invert so we'll get l->h event interrupt */
  81. if (lid_inverted)
  82. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  83. else
  84. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  85. lid_inverted = !lid_inverted;
  86. }
  87. static void detect_lid_state(void)
  88. {
  89. /*
  90. * the edge detector hookup on the gpio inputs on the geode is
  91. * odd, to say the least. See http://dev.laptop.org/ticket/5703
  92. * for details, but in a nutshell: we don't use the edge
  93. * detectors. instead, we make use of an anomoly: with the both
  94. * edge detectors turned off, we still get an edge event on a
  95. * positive edge transition. to take advantage of this, we use the
  96. * front-end inverter to ensure that that's the edge we're always
  97. * going to see next.
  98. */
  99. int state;
  100. state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
  101. lid_open = !state ^ !lid_inverted; /* x ^^ y */
  102. if (!state)
  103. return;
  104. flip_lid_inverter();
  105. }
  106. /* Report current lid switch state through input layer */
  107. static void send_lid_state(void)
  108. {
  109. if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
  110. return; /* Nothing new to report. */
  111. input_report_switch(lid_switch_idev, SW_LID, !lid_open);
  112. input_sync(lid_switch_idev);
  113. pm_wakeup_event(&lid_switch_idev->dev, 0);
  114. }
  115. static ssize_t lid_wake_mode_show(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. const char *mode = lid_wake_mode_names[lid_wake_mode];
  119. return sprintf(buf, "%s\n", mode);
  120. }
  121. static ssize_t lid_wake_mode_set(struct device *dev,
  122. struct device_attribute *attr,
  123. const char *buf, size_t count)
  124. {
  125. int i;
  126. for (i = 0; i < ARRAY_SIZE(lid_wake_mode_names); i++) {
  127. const char *mode = lid_wake_mode_names[i];
  128. if (strlen(mode) != count || strncasecmp(mode, buf, count))
  129. continue;
  130. lid_wake_mode = i;
  131. return count;
  132. }
  133. return -EINVAL;
  134. }
  135. static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
  136. lid_wake_mode_set);
  137. /*
  138. * Process all items in the EC's SCI queue.
  139. *
  140. * This is handled in a workqueue because olpc_ec_cmd can be slow (and
  141. * can even timeout).
  142. *
  143. * If propagate_events is false, the queue is drained without events being
  144. * generated for the interrupts.
  145. */
  146. static void process_sci_queue(bool propagate_events)
  147. {
  148. int r;
  149. u16 data;
  150. do {
  151. r = olpc_ec_sci_query(&data);
  152. if (r || !data)
  153. break;
  154. pr_debug(PFX "SCI 0x%x received\n", data);
  155. switch (data) {
  156. case EC_SCI_SRC_BATERR:
  157. case EC_SCI_SRC_BATSOC:
  158. case EC_SCI_SRC_BATTERY:
  159. case EC_SCI_SRC_BATCRIT:
  160. battery_status_changed();
  161. break;
  162. case EC_SCI_SRC_ACPWR:
  163. ac_status_changed();
  164. break;
  165. }
  166. if (data == EC_SCI_SRC_EBOOK && propagate_events)
  167. send_ebook_state();
  168. } while (data);
  169. if (r)
  170. pr_err(PFX "Failed to clear SCI queue");
  171. }
  172. static void process_sci_queue_work(struct work_struct *work)
  173. {
  174. process_sci_queue(true);
  175. }
  176. static DECLARE_WORK(sci_work, process_sci_queue_work);
  177. static irqreturn_t xo1_sci_intr(int irq, void *dev_id)
  178. {
  179. struct platform_device *pdev = dev_id;
  180. u32 sts;
  181. u32 gpe;
  182. sts = inl(acpi_base + CS5536_PM1_STS);
  183. outl(sts | 0xffff, acpi_base + CS5536_PM1_STS);
  184. gpe = inl(acpi_base + CS5536_PM_GPE0_STS);
  185. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  186. dev_dbg(&pdev->dev, "sts %x gpe %x\n", sts, gpe);
  187. if (sts & CS5536_PWRBTN_FLAG) {
  188. if (!(sts & CS5536_WAK_FLAG)) {
  189. /* Only report power button input when it was pressed
  190. * during regular operation (as opposed to when it
  191. * was used to wake the system). */
  192. input_report_key(power_button_idev, KEY_POWER, 1);
  193. input_sync(power_button_idev);
  194. input_report_key(power_button_idev, KEY_POWER, 0);
  195. input_sync(power_button_idev);
  196. }
  197. /* Report the wakeup event in all cases. */
  198. pm_wakeup_event(&power_button_idev->dev, 0);
  199. }
  200. if ((sts & (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) ==
  201. (CS5536_RTC_FLAG | CS5536_WAK_FLAG)) {
  202. /* When the system is woken by the RTC alarm, report the
  203. * event on the rtc device. */
  204. struct device *rtc = bus_find_device_by_name(
  205. &platform_bus_type, NULL, "rtc_cmos");
  206. if (rtc) {
  207. pm_wakeup_event(rtc, 0);
  208. put_device(rtc);
  209. }
  210. }
  211. if (gpe & CS5536_GPIOM7_PME_FLAG) { /* EC GPIO */
  212. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
  213. schedule_work(&sci_work);
  214. }
  215. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  216. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  217. detect_lid_state();
  218. send_lid_state();
  219. return IRQ_HANDLED;
  220. }
  221. static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
  222. {
  223. if (device_may_wakeup(&power_button_idev->dev))
  224. olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
  225. else
  226. olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
  227. if (device_may_wakeup(&ebook_switch_idev->dev))
  228. olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
  229. else
  230. olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
  231. if (!device_may_wakeup(&lid_switch_idev->dev)) {
  232. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  233. } else if ((lid_open && lid_wake_mode == LID_WAKE_OPEN) ||
  234. (!lid_open && lid_wake_mode == LID_WAKE_CLOSE)) {
  235. flip_lid_inverter();
  236. /* we may have just caused an event */
  237. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  238. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  239. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  240. }
  241. return 0;
  242. }
  243. static int xo1_sci_resume(struct platform_device *pdev)
  244. {
  245. /*
  246. * We don't know what may have happened while we were asleep.
  247. * Reestablish our lid setup so we're sure to catch all transitions.
  248. */
  249. detect_lid_state();
  250. send_lid_state();
  251. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  252. /* Enable all EC events */
  253. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  254. /* Power/battery status might have changed too */
  255. battery_status_changed();
  256. ac_status_changed();
  257. return 0;
  258. }
  259. static int __devinit setup_sci_interrupt(struct platform_device *pdev)
  260. {
  261. u32 lo, hi;
  262. u32 sts;
  263. int r;
  264. rdmsr(0x51400020, lo, hi);
  265. sci_irq = (lo >> 20) & 15;
  266. if (sci_irq) {
  267. dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
  268. } else {
  269. /* Zero means masked */
  270. dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
  271. sci_irq = 3;
  272. lo |= 0x00300000;
  273. wrmsrl(0x51400020, lo);
  274. }
  275. /* Select level triggered in PIC */
  276. if (sci_irq < 8) {
  277. lo = inb(CS5536_PIC_INT_SEL1);
  278. lo |= 1 << sci_irq;
  279. outb(lo, CS5536_PIC_INT_SEL1);
  280. } else {
  281. lo = inb(CS5536_PIC_INT_SEL2);
  282. lo |= 1 << (sci_irq - 8);
  283. outb(lo, CS5536_PIC_INT_SEL2);
  284. }
  285. /* Enable interesting SCI events, and clear pending interrupts */
  286. sts = inl(acpi_base + CS5536_PM1_STS);
  287. outl(((CS5536_PM_PWRBTN | CS5536_PM_RTC) << 16) | 0xffff,
  288. acpi_base + CS5536_PM1_STS);
  289. r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
  290. if (r)
  291. dev_err(&pdev->dev, "can't request interrupt\n");
  292. return r;
  293. }
  294. static int __devinit setup_ec_sci(void)
  295. {
  296. int r;
  297. r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
  298. if (r)
  299. return r;
  300. gpio_direction_input(OLPC_GPIO_ECSCI);
  301. /* Clear pending EC SCI events */
  302. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
  303. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);
  304. /*
  305. * Enable EC SCI events, and map them to both a PME and the SCI
  306. * interrupt.
  307. *
  308. * Ordinarily, in addition to functioning as GPIOs, Geode GPIOs can
  309. * be mapped to regular interrupts *or* Geode-specific Power
  310. * Management Events (PMEs) - events that bring the system out of
  311. * suspend. In this case, we want both of those things - the system
  312. * wakeup, *and* the ability to get an interrupt when an event occurs.
  313. *
  314. * To achieve this, we map the GPIO to a PME, and then we use one
  315. * of the many generic knobs on the CS5535 PIC to additionally map the
  316. * PME to the regular SCI interrupt line.
  317. */
  318. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
  319. /* Set the SCI to cause a PME event on group 7 */
  320. cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
  321. /* And have group 7 also fire the SCI interrupt */
  322. cs5535_pic_unreqz_select_high(7, sci_irq);
  323. return 0;
  324. }
  325. static void free_ec_sci(void)
  326. {
  327. gpio_free(OLPC_GPIO_ECSCI);
  328. }
  329. static int __devinit setup_lid_events(void)
  330. {
  331. int r;
  332. r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
  333. if (r)
  334. return r;
  335. gpio_direction_input(OLPC_GPIO_LID);
  336. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  337. lid_inverted = 0;
  338. /* Clear edge detection and event enable for now */
  339. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  340. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
  341. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);
  342. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  343. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  344. /* Set the LID to cause an PME event on group 6 */
  345. cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
  346. /* Set PME group 6 to fire the SCI interrupt */
  347. cs5535_gpio_set_irq(6, sci_irq);
  348. /* Enable the event */
  349. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  350. return 0;
  351. }
  352. static void free_lid_events(void)
  353. {
  354. gpio_free(OLPC_GPIO_LID);
  355. }
  356. static int __devinit setup_power_button(struct platform_device *pdev)
  357. {
  358. int r;
  359. power_button_idev = input_allocate_device();
  360. if (!power_button_idev)
  361. return -ENOMEM;
  362. power_button_idev->name = "Power Button";
  363. power_button_idev->phys = DRV_NAME "/input0";
  364. set_bit(EV_KEY, power_button_idev->evbit);
  365. set_bit(KEY_POWER, power_button_idev->keybit);
  366. power_button_idev->dev.parent = &pdev->dev;
  367. device_init_wakeup(&power_button_idev->dev, 1);
  368. r = input_register_device(power_button_idev);
  369. if (r) {
  370. dev_err(&pdev->dev, "failed to register power button: %d\n", r);
  371. input_free_device(power_button_idev);
  372. }
  373. return r;
  374. }
  375. static void free_power_button(void)
  376. {
  377. input_unregister_device(power_button_idev);
  378. input_free_device(power_button_idev);
  379. }
  380. static int __devinit setup_ebook_switch(struct platform_device *pdev)
  381. {
  382. int r;
  383. ebook_switch_idev = input_allocate_device();
  384. if (!ebook_switch_idev)
  385. return -ENOMEM;
  386. ebook_switch_idev->name = "EBook Switch";
  387. ebook_switch_idev->phys = DRV_NAME "/input1";
  388. set_bit(EV_SW, ebook_switch_idev->evbit);
  389. set_bit(SW_TABLET_MODE, ebook_switch_idev->swbit);
  390. ebook_switch_idev->dev.parent = &pdev->dev;
  391. device_set_wakeup_capable(&ebook_switch_idev->dev, true);
  392. r = input_register_device(ebook_switch_idev);
  393. if (r) {
  394. dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
  395. input_free_device(ebook_switch_idev);
  396. }
  397. return r;
  398. }
  399. static void free_ebook_switch(void)
  400. {
  401. input_unregister_device(ebook_switch_idev);
  402. input_free_device(ebook_switch_idev);
  403. }
  404. static int __devinit setup_lid_switch(struct platform_device *pdev)
  405. {
  406. int r;
  407. lid_switch_idev = input_allocate_device();
  408. if (!lid_switch_idev)
  409. return -ENOMEM;
  410. lid_switch_idev->name = "Lid Switch";
  411. lid_switch_idev->phys = DRV_NAME "/input2";
  412. set_bit(EV_SW, lid_switch_idev->evbit);
  413. set_bit(SW_LID, lid_switch_idev->swbit);
  414. lid_switch_idev->dev.parent = &pdev->dev;
  415. device_set_wakeup_capable(&lid_switch_idev->dev, true);
  416. r = input_register_device(lid_switch_idev);
  417. if (r) {
  418. dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
  419. goto err_register;
  420. }
  421. r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
  422. if (r) {
  423. dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
  424. goto err_create_attr;
  425. }
  426. return 0;
  427. err_create_attr:
  428. input_unregister_device(lid_switch_idev);
  429. err_register:
  430. input_free_device(lid_switch_idev);
  431. return r;
  432. }
  433. static void free_lid_switch(void)
  434. {
  435. device_remove_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
  436. input_unregister_device(lid_switch_idev);
  437. input_free_device(lid_switch_idev);
  438. }
  439. static int __devinit xo1_sci_probe(struct platform_device *pdev)
  440. {
  441. struct resource *res;
  442. int r;
  443. /* don't run on non-XOs */
  444. if (!machine_is_olpc())
  445. return -ENODEV;
  446. r = mfd_cell_enable(pdev);
  447. if (r)
  448. return r;
  449. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  450. if (!res) {
  451. dev_err(&pdev->dev, "can't fetch device resource info\n");
  452. return -EIO;
  453. }
  454. acpi_base = res->start;
  455. r = setup_power_button(pdev);
  456. if (r)
  457. return r;
  458. r = setup_ebook_switch(pdev);
  459. if (r)
  460. goto err_ebook;
  461. r = setup_lid_switch(pdev);
  462. if (r)
  463. goto err_lid;
  464. r = setup_lid_events();
  465. if (r)
  466. goto err_lidevt;
  467. r = setup_ec_sci();
  468. if (r)
  469. goto err_ecsci;
  470. /* Enable PME generation for EC-generated events */
  471. outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
  472. acpi_base + CS5536_PM_GPE0_EN);
  473. /* Clear pending events */
  474. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  475. process_sci_queue(false);
  476. /* Initial sync */
  477. send_ebook_state();
  478. detect_lid_state();
  479. send_lid_state();
  480. r = setup_sci_interrupt(pdev);
  481. if (r)
  482. goto err_sci;
  483. /* Enable all EC events */
  484. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  485. return r;
  486. err_sci:
  487. free_ec_sci();
  488. err_ecsci:
  489. free_lid_events();
  490. err_lidevt:
  491. free_lid_switch();
  492. err_lid:
  493. free_ebook_switch();
  494. err_ebook:
  495. free_power_button();
  496. return r;
  497. }
  498. static int __devexit xo1_sci_remove(struct platform_device *pdev)
  499. {
  500. mfd_cell_disable(pdev);
  501. free_irq(sci_irq, pdev);
  502. cancel_work_sync(&sci_work);
  503. free_ec_sci();
  504. free_lid_events();
  505. free_lid_switch();
  506. free_ebook_switch();
  507. free_power_button();
  508. acpi_base = 0;
  509. return 0;
  510. }
  511. static struct platform_driver xo1_sci_driver = {
  512. .driver = {
  513. .name = "olpc-xo1-sci-acpi",
  514. },
  515. .probe = xo1_sci_probe,
  516. .remove = __devexit_p(xo1_sci_remove),
  517. .suspend = xo1_sci_suspend,
  518. .resume = xo1_sci_resume,
  519. };
  520. static int __init xo1_sci_init(void)
  521. {
  522. return platform_driver_register(&xo1_sci_driver);
  523. }
  524. arch_initcall(xo1_sci_init);