olpc-xo1-sci.c 14 KB

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