olpc-xo1-sci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 (gpe & CS5536_GPIOM7_PME_FLAG) { /* EC GPIO */
  201. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
  202. schedule_work(&sci_work);
  203. }
  204. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  205. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  206. detect_lid_state();
  207. send_lid_state();
  208. return IRQ_HANDLED;
  209. }
  210. static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
  211. {
  212. if (device_may_wakeup(&power_button_idev->dev))
  213. olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
  214. else
  215. olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
  216. if (device_may_wakeup(&ebook_switch_idev->dev))
  217. olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
  218. else
  219. olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
  220. if (!device_may_wakeup(&lid_switch_idev->dev)) {
  221. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  222. } else if ((lid_open && lid_wake_mode == LID_WAKE_OPEN) ||
  223. (!lid_open && lid_wake_mode == LID_WAKE_CLOSE)) {
  224. flip_lid_inverter();
  225. /* we may have just caused an event */
  226. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  227. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  228. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  229. }
  230. return 0;
  231. }
  232. static int xo1_sci_resume(struct platform_device *pdev)
  233. {
  234. /*
  235. * We don't know what may have happened while we were asleep.
  236. * Reestablish our lid setup so we're sure to catch all transitions.
  237. */
  238. detect_lid_state();
  239. send_lid_state();
  240. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  241. /* Enable all EC events */
  242. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  243. /* Power/battery status might have changed too */
  244. battery_status_changed();
  245. ac_status_changed();
  246. return 0;
  247. }
  248. static int __devinit setup_sci_interrupt(struct platform_device *pdev)
  249. {
  250. u32 lo, hi;
  251. u32 sts;
  252. int r;
  253. rdmsr(0x51400020, lo, hi);
  254. sci_irq = (lo >> 20) & 15;
  255. if (sci_irq) {
  256. dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
  257. } else {
  258. /* Zero means masked */
  259. dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
  260. sci_irq = 3;
  261. lo |= 0x00300000;
  262. wrmsrl(0x51400020, lo);
  263. }
  264. /* Select level triggered in PIC */
  265. if (sci_irq < 8) {
  266. lo = inb(CS5536_PIC_INT_SEL1);
  267. lo |= 1 << sci_irq;
  268. outb(lo, CS5536_PIC_INT_SEL1);
  269. } else {
  270. lo = inb(CS5536_PIC_INT_SEL2);
  271. lo |= 1 << (sci_irq - 8);
  272. outb(lo, CS5536_PIC_INT_SEL2);
  273. }
  274. /* Enable SCI from power button, and clear pending interrupts */
  275. sts = inl(acpi_base + CS5536_PM1_STS);
  276. outl((CS5536_PM_PWRBTN << 16) | 0xffff, acpi_base + CS5536_PM1_STS);
  277. r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
  278. if (r)
  279. dev_err(&pdev->dev, "can't request interrupt\n");
  280. return r;
  281. }
  282. static int __devinit setup_ec_sci(void)
  283. {
  284. int r;
  285. r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
  286. if (r)
  287. return r;
  288. gpio_direction_input(OLPC_GPIO_ECSCI);
  289. /* Clear pending EC SCI events */
  290. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
  291. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);
  292. /*
  293. * Enable EC SCI events, and map them to both a PME and the SCI
  294. * interrupt.
  295. *
  296. * Ordinarily, in addition to functioning as GPIOs, Geode GPIOs can
  297. * be mapped to regular interrupts *or* Geode-specific Power
  298. * Management Events (PMEs) - events that bring the system out of
  299. * suspend. In this case, we want both of those things - the system
  300. * wakeup, *and* the ability to get an interrupt when an event occurs.
  301. *
  302. * To achieve this, we map the GPIO to a PME, and then we use one
  303. * of the many generic knobs on the CS5535 PIC to additionally map the
  304. * PME to the regular SCI interrupt line.
  305. */
  306. cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
  307. /* Set the SCI to cause a PME event on group 7 */
  308. cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
  309. /* And have group 7 also fire the SCI interrupt */
  310. cs5535_pic_unreqz_select_high(7, sci_irq);
  311. return 0;
  312. }
  313. static void free_ec_sci(void)
  314. {
  315. gpio_free(OLPC_GPIO_ECSCI);
  316. }
  317. static int __devinit setup_lid_events(void)
  318. {
  319. int r;
  320. r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
  321. if (r)
  322. return r;
  323. gpio_direction_input(OLPC_GPIO_LID);
  324. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
  325. lid_inverted = 0;
  326. /* Clear edge detection and event enable for now */
  327. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  328. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
  329. cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);
  330. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
  331. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
  332. /* Set the LID to cause an PME event on group 6 */
  333. cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
  334. /* Set PME group 6 to fire the SCI interrupt */
  335. cs5535_gpio_set_irq(6, sci_irq);
  336. /* Enable the event */
  337. cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
  338. return 0;
  339. }
  340. static void free_lid_events(void)
  341. {
  342. gpio_free(OLPC_GPIO_LID);
  343. }
  344. static int __devinit setup_power_button(struct platform_device *pdev)
  345. {
  346. int r;
  347. power_button_idev = input_allocate_device();
  348. if (!power_button_idev)
  349. return -ENOMEM;
  350. power_button_idev->name = "Power Button";
  351. power_button_idev->phys = DRV_NAME "/input0";
  352. set_bit(EV_KEY, power_button_idev->evbit);
  353. set_bit(KEY_POWER, power_button_idev->keybit);
  354. power_button_idev->dev.parent = &pdev->dev;
  355. device_init_wakeup(&power_button_idev->dev, 1);
  356. r = input_register_device(power_button_idev);
  357. if (r) {
  358. dev_err(&pdev->dev, "failed to register power button: %d\n", r);
  359. input_free_device(power_button_idev);
  360. }
  361. return r;
  362. }
  363. static void free_power_button(void)
  364. {
  365. input_unregister_device(power_button_idev);
  366. input_free_device(power_button_idev);
  367. }
  368. static int __devinit setup_ebook_switch(struct platform_device *pdev)
  369. {
  370. int r;
  371. ebook_switch_idev = input_allocate_device();
  372. if (!ebook_switch_idev)
  373. return -ENOMEM;
  374. ebook_switch_idev->name = "EBook Switch";
  375. ebook_switch_idev->phys = DRV_NAME "/input1";
  376. set_bit(EV_SW, ebook_switch_idev->evbit);
  377. set_bit(SW_TABLET_MODE, ebook_switch_idev->swbit);
  378. ebook_switch_idev->dev.parent = &pdev->dev;
  379. device_set_wakeup_capable(&ebook_switch_idev->dev, true);
  380. r = input_register_device(ebook_switch_idev);
  381. if (r) {
  382. dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
  383. input_free_device(ebook_switch_idev);
  384. }
  385. return r;
  386. }
  387. static void free_ebook_switch(void)
  388. {
  389. input_unregister_device(ebook_switch_idev);
  390. input_free_device(ebook_switch_idev);
  391. }
  392. static int __devinit setup_lid_switch(struct platform_device *pdev)
  393. {
  394. int r;
  395. lid_switch_idev = input_allocate_device();
  396. if (!lid_switch_idev)
  397. return -ENOMEM;
  398. lid_switch_idev->name = "Lid Switch";
  399. lid_switch_idev->phys = DRV_NAME "/input2";
  400. set_bit(EV_SW, lid_switch_idev->evbit);
  401. set_bit(SW_LID, lid_switch_idev->swbit);
  402. lid_switch_idev->dev.parent = &pdev->dev;
  403. device_set_wakeup_capable(&lid_switch_idev->dev, true);
  404. r = input_register_device(lid_switch_idev);
  405. if (r) {
  406. dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
  407. goto err_register;
  408. }
  409. r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
  410. if (r) {
  411. dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
  412. goto err_create_attr;
  413. }
  414. return 0;
  415. err_create_attr:
  416. input_unregister_device(lid_switch_idev);
  417. err_register:
  418. input_free_device(lid_switch_idev);
  419. return r;
  420. }
  421. static void free_lid_switch(void)
  422. {
  423. device_remove_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
  424. input_unregister_device(lid_switch_idev);
  425. input_free_device(lid_switch_idev);
  426. }
  427. static int __devinit xo1_sci_probe(struct platform_device *pdev)
  428. {
  429. struct resource *res;
  430. int r;
  431. /* don't run on non-XOs */
  432. if (!machine_is_olpc())
  433. return -ENODEV;
  434. r = mfd_cell_enable(pdev);
  435. if (r)
  436. return r;
  437. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  438. if (!res) {
  439. dev_err(&pdev->dev, "can't fetch device resource info\n");
  440. return -EIO;
  441. }
  442. acpi_base = res->start;
  443. r = setup_power_button(pdev);
  444. if (r)
  445. return r;
  446. r = setup_ebook_switch(pdev);
  447. if (r)
  448. goto err_ebook;
  449. r = setup_lid_switch(pdev);
  450. if (r)
  451. goto err_lid;
  452. r = setup_lid_events();
  453. if (r)
  454. goto err_lidevt;
  455. r = setup_ec_sci();
  456. if (r)
  457. goto err_ecsci;
  458. /* Enable PME generation for EC-generated events */
  459. outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
  460. acpi_base + CS5536_PM_GPE0_EN);
  461. /* Clear pending events */
  462. outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
  463. process_sci_queue(false);
  464. /* Initial sync */
  465. send_ebook_state();
  466. detect_lid_state();
  467. send_lid_state();
  468. r = setup_sci_interrupt(pdev);
  469. if (r)
  470. goto err_sci;
  471. /* Enable all EC events */
  472. olpc_ec_mask_write(EC_SCI_SRC_ALL);
  473. return r;
  474. err_sci:
  475. free_ec_sci();
  476. err_ecsci:
  477. free_lid_events();
  478. err_lidevt:
  479. free_lid_switch();
  480. err_lid:
  481. free_ebook_switch();
  482. err_ebook:
  483. free_power_button();
  484. return r;
  485. }
  486. static int __devexit xo1_sci_remove(struct platform_device *pdev)
  487. {
  488. mfd_cell_disable(pdev);
  489. free_irq(sci_irq, pdev);
  490. cancel_work_sync(&sci_work);
  491. free_ec_sci();
  492. free_lid_events();
  493. free_lid_switch();
  494. free_ebook_switch();
  495. free_power_button();
  496. acpi_base = 0;
  497. return 0;
  498. }
  499. static struct platform_driver xo1_sci_driver = {
  500. .driver = {
  501. .name = "olpc-xo1-sci-acpi",
  502. },
  503. .probe = xo1_sci_probe,
  504. .remove = __devexit_p(xo1_sci_remove),
  505. .suspend = xo1_sci_suspend,
  506. .resume = xo1_sci_resume,
  507. };
  508. static int __init xo1_sci_init(void)
  509. {
  510. return platform_driver_register(&xo1_sci_driver);
  511. }
  512. arch_initcall(xo1_sci_init);