olpc-xo1-sci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 <linux/olpc-ec.h>
  26. #include <asm/io.h>
  27. #include <asm/msr.h>
  28. #include <asm/olpc.h>
  29. #define DRV_NAME "olpc-xo1-sci"
  30. #define PFX DRV_NAME ": "
  31. static unsigned long acpi_base;
  32. static struct input_dev *power_button_idev;
  33. static struct input_dev *ebook_switch_idev;
  34. static struct input_dev *lid_switch_idev;
  35. static int sci_irq;
  36. static bool lid_open;
  37. static bool lid_inverted;
  38. static int lid_wake_mode;
  39. enum lid_wake_modes {
  40. LID_WAKE_ALWAYS,
  41. LID_WAKE_OPEN,
  42. LID_WAKE_CLOSE,
  43. };
  44. static const char * const lid_wake_mode_names[] = {
  45. [LID_WAKE_ALWAYS] = "always",
  46. [LID_WAKE_OPEN] = "open",
  47. [LID_WAKE_CLOSE] = "close",
  48. };
  49. static void battery_status_changed(void)
  50. {
  51. struct power_supply *psy = power_supply_get_by_name("olpc-battery");
  52. if (psy) {
  53. power_supply_changed(psy);
  54. power_supply_put(psy);
  55. }
  56. }
  57. static void ac_status_changed(void)
  58. {
  59. struct power_supply *psy = power_supply_get_by_name("olpc-ac");
  60. if (psy) {
  61. power_supply_changed(psy);
  62. power_supply_put(psy);
  63. }
  64. }
  65. /* Report current ebook switch state through input layer */
  66. static void send_ebook_state(void)
  67. {
  68. unsigned char state;
  69. if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
  70. pr_err(PFX "failed to get ebook state\n");
  71. return;
  72. }
  73. if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
  74. return; /* Nothing new to report. */
  75. input_report_switch(ebook_switch_idev, SW_TABLET_MODE, state);
  76. input_sync(ebook_switch_idev);
  77. pm_wakeup_event(&ebook_switch_idev->dev, 0);
  78. }
  79. static void flip_lid_inverter(void)
  80. {
  81. /* gpio is high; invert so we'll get l->h event interrupt */
  82. if (lid_inverted)
  83. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  84. else
  85. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  86. lid_inverted = !lid_inverted;
  87. }
  88. static void detect_lid_state(void)
  89. {
  90. /*
  91. * the edge detector hookup on the gpio inputs on the geode is
  92. * odd, to say the least. See http://dev.laptop.org/ticket/5703
  93. * for details, but in a nutshell: we don't use the edge
  94. * detectors. instead, we make use of an anomoly: with the both
  95. * edge detectors turned off, we still get an edge event on a
  96. * positive edge transition. to take advantage of this, we use the
  97. * front-end inverter to ensure that that's the edge we're always
  98. * going to see next.
  99. */
  100. int state;
  101. state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
  102. lid_open = !state ^ !lid_inverted; /* x ^^ y */
  103. if (!state)
  104. return;
  105. flip_lid_inverter();
  106. }
  107. /* Report current lid switch state through input layer */
  108. static void send_lid_state(void)
  109. {
  110. if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
  111. return; /* Nothing new to report. */
  112. input_report_switch(lid_switch_idev, SW_LID, !lid_open);
  113. input_sync(lid_switch_idev);
  114. pm_wakeup_event(&lid_switch_idev->dev, 0);
  115. }
  116. static ssize_t lid_wake_mode_show(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. const char *mode = lid_wake_mode_names[lid_wake_mode];
  120. return sprintf(buf, "%s\n", mode);
  121. }
  122. static ssize_t lid_wake_mode_set(struct device *dev,
  123. struct device_attribute *attr,
  124. const char *buf, size_t count)
  125. {
  126. int i;
  127. for (i = 0; i < ARRAY_SIZE(lid_wake_mode_names); i++) {
  128. const char *mode = lid_wake_mode_names[i];
  129. if (strlen(mode) != count || strncasecmp(mode, buf, count))
  130. continue;
  131. lid_wake_mode = i;
  132. return count;
  133. }
  134. return -EINVAL;
  135. }
  136. static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
  137. lid_wake_mode_set);
  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. r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
  421. if (r) {
  422. dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
  423. goto err_create_attr;
  424. }
  425. return 0;
  426. err_create_attr:
  427. input_unregister_device(lid_switch_idev);
  428. lid_switch_idev = NULL;
  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. }
  438. static int xo1_sci_probe(struct platform_device *pdev)
  439. {
  440. struct resource *res;
  441. int r;
  442. /* don't run on non-XOs */
  443. if (!machine_is_olpc())
  444. return -ENODEV;
  445. r = mfd_cell_enable(pdev);
  446. if (r)
  447. return r;
  448. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  449. if (!res) {
  450. dev_err(&pdev->dev, "can't fetch device resource info\n");
  451. return -EIO;
  452. }
  453. acpi_base = res->start;
  454. r = setup_power_button(pdev);
  455. if (r)
  456. return r;
  457. r = setup_ebook_switch(pdev);
  458. if (r)
  459. goto err_ebook;
  460. r = setup_lid_switch(pdev);
  461. if (r)
  462. goto err_lid;
  463. r = setup_lid_events();
  464. if (r)
  465. goto err_lidevt;
  466. r = setup_ec_sci();
  467. if (r)
  468. goto err_ecsci;
  469. /* Enable PME generation for EC-generated events */
  470. outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
  471. acpi_base + CS5536_PM_GPE0_EN);
  472. /* Clear pending events */
  473. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  474. process_sci_queue(false);
  475. /* Initial sync */
  476. send_ebook_state();
  477. detect_lid_state();
  478. send_lid_state();
  479. r = setup_sci_interrupt(pdev);
  480. if (r)
  481. goto err_sci;
  482. /* Enable all EC events */
  483. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  484. return r;
  485. err_sci:
  486. free_ec_sci();
  487. err_ecsci:
  488. free_lid_events();
  489. err_lidevt:
  490. free_lid_switch();
  491. err_lid:
  492. free_ebook_switch();
  493. err_ebook:
  494. free_power_button();
  495. return r;
  496. }
  497. static int xo1_sci_remove(struct platform_device *pdev)
  498. {
  499. mfd_cell_disable(pdev);
  500. free_irq(sci_irq, pdev);
  501. cancel_work_sync(&sci_work);
  502. free_ec_sci();
  503. free_lid_events();
  504. free_lid_switch();
  505. free_ebook_switch();
  506. free_power_button();
  507. acpi_base = 0;
  508. return 0;
  509. }
  510. static struct platform_driver xo1_sci_driver = {
  511. .driver = {
  512. .name = "olpc-xo1-sci-acpi",
  513. },
  514. .probe = xo1_sci_probe,
  515. .remove = xo1_sci_remove,
  516. .suspend = xo1_sci_suspend,
  517. .resume = xo1_sci_resume,
  518. };
  519. static int __init xo1_sci_init(void)
  520. {
  521. return platform_driver_register(&xo1_sci_driver);
  522. }
  523. arch_initcall(xo1_sci_init);