olpc-xo1-sci.c 15 KB

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