pm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Alchemy Development Board example suspend userspace interface.
  3. *
  4. * (c) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kobject.h>
  8. #include <linux/suspend.h>
  9. #include <linux/sysfs.h>
  10. #include <asm/mach-au1x00/au1000.h>
  11. /*
  12. * Generic suspend userspace interface for Alchemy development boards.
  13. * This code exports a few sysfs nodes under /sys/power/db1x/ which
  14. * can be used by userspace to en/disable all au1x-provided wakeup
  15. * sources and configure the timeout after which the the TOYMATCH2 irq
  16. * is to trigger a wakeup.
  17. */
  18. static unsigned long db1x_pm_sleep_secs;
  19. static unsigned long db1x_pm_wakemsk;
  20. static unsigned long db1x_pm_last_wakesrc;
  21. static int db1x_pm_enter(suspend_state_t state)
  22. {
  23. /* enable GPIO based wakeup */
  24. au_writel(1, SYS_PININPUTEN);
  25. /* clear and setup wake cause and source */
  26. au_writel(0, SYS_WAKEMSK);
  27. au_sync();
  28. au_writel(0, SYS_WAKESRC);
  29. au_sync();
  30. au_writel(db1x_pm_wakemsk, SYS_WAKEMSK);
  31. au_sync();
  32. /* setup 1Hz-timer-based wakeup: wait for reg access */
  33. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20)
  34. asm volatile ("nop");
  35. au_writel(au_readl(SYS_TOYREAD) + db1x_pm_sleep_secs, SYS_TOYMATCH2);
  36. au_sync();
  37. /* wait for value to really hit the register */
  38. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20)
  39. asm volatile ("nop");
  40. /* ...and now the sandman can come! */
  41. au_sleep();
  42. return 0;
  43. }
  44. static int db1x_pm_begin(suspend_state_t state)
  45. {
  46. if (!db1x_pm_wakemsk) {
  47. printk(KERN_ERR "db1x: no wakeup source activated!\n");
  48. return -EINVAL;
  49. }
  50. return 0;
  51. }
  52. static void db1x_pm_end(void)
  53. {
  54. /* read and store wakeup source, the clear the register. To
  55. * be able to clear it, WAKEMSK must be cleared first.
  56. */
  57. db1x_pm_last_wakesrc = au_readl(SYS_WAKESRC);
  58. au_writel(0, SYS_WAKEMSK);
  59. au_writel(0, SYS_WAKESRC);
  60. au_sync();
  61. }
  62. static struct platform_suspend_ops db1x_pm_ops = {
  63. .valid = suspend_valid_only_mem,
  64. .begin = db1x_pm_begin,
  65. .enter = db1x_pm_enter,
  66. .end = db1x_pm_end,
  67. };
  68. #define ATTRCMP(x) (0 == strcmp(attr->attr.name, #x))
  69. static ssize_t db1x_pmattr_show(struct kobject *kobj,
  70. struct kobj_attribute *attr,
  71. char *buf)
  72. {
  73. int idx;
  74. if (ATTRCMP(timer_timeout))
  75. return sprintf(buf, "%lu\n", db1x_pm_sleep_secs);
  76. else if (ATTRCMP(timer))
  77. return sprintf(buf, "%u\n",
  78. !!(db1x_pm_wakemsk & SYS_WAKEMSK_M2));
  79. else if (ATTRCMP(wakesrc))
  80. return sprintf(buf, "%lu\n", db1x_pm_last_wakesrc);
  81. else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
  82. ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
  83. ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
  84. idx = (attr->attr.name)[4] - '0';
  85. return sprintf(buf, "%d\n",
  86. !!(db1x_pm_wakemsk & SYS_WAKEMSK_GPIO(idx)));
  87. } else if (ATTRCMP(wakemsk)) {
  88. return sprintf(buf, "%08lx\n", db1x_pm_wakemsk);
  89. }
  90. return -ENOENT;
  91. }
  92. static ssize_t db1x_pmattr_store(struct kobject *kobj,
  93. struct kobj_attribute *attr,
  94. const char *instr,
  95. size_t bytes)
  96. {
  97. unsigned long l;
  98. int tmp;
  99. if (ATTRCMP(timer_timeout)) {
  100. tmp = strict_strtoul(instr, 0, &l);
  101. if (tmp)
  102. return tmp;
  103. db1x_pm_sleep_secs = l;
  104. } else if (ATTRCMP(timer)) {
  105. if (instr[0] != '0')
  106. db1x_pm_wakemsk |= SYS_WAKEMSK_M2;
  107. else
  108. db1x_pm_wakemsk &= ~SYS_WAKEMSK_M2;
  109. } else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
  110. ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
  111. ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
  112. tmp = (attr->attr.name)[4] - '0';
  113. if (instr[0] != '0') {
  114. db1x_pm_wakemsk |= SYS_WAKEMSK_GPIO(tmp);
  115. } else {
  116. db1x_pm_wakemsk &= ~SYS_WAKEMSK_GPIO(tmp);
  117. }
  118. } else if (ATTRCMP(wakemsk)) {
  119. tmp = strict_strtoul(instr, 0, &l);
  120. if (tmp)
  121. return tmp;
  122. db1x_pm_wakemsk = l & 0x0000003f;
  123. } else
  124. bytes = -ENOENT;
  125. return bytes;
  126. }
  127. #define ATTR(x) \
  128. static struct kobj_attribute x##_attribute = \
  129. __ATTR(x, 0664, db1x_pmattr_show, \
  130. db1x_pmattr_store);
  131. ATTR(gpio0) /* GPIO-based wakeup enable */
  132. ATTR(gpio1)
  133. ATTR(gpio2)
  134. ATTR(gpio3)
  135. ATTR(gpio4)
  136. ATTR(gpio5)
  137. ATTR(gpio6)
  138. ATTR(gpio7)
  139. ATTR(timer) /* TOYMATCH2-based wakeup enable */
  140. ATTR(timer_timeout) /* timer-based wakeup timeout value, in seconds */
  141. ATTR(wakesrc) /* contents of SYS_WAKESRC after last wakeup */
  142. ATTR(wakemsk) /* direct access to SYS_WAKEMSK */
  143. #define ATTR_LIST(x) & x ## _attribute.attr
  144. static struct attribute *db1x_pmattrs[] = {
  145. ATTR_LIST(gpio0),
  146. ATTR_LIST(gpio1),
  147. ATTR_LIST(gpio2),
  148. ATTR_LIST(gpio3),
  149. ATTR_LIST(gpio4),
  150. ATTR_LIST(gpio5),
  151. ATTR_LIST(gpio6),
  152. ATTR_LIST(gpio7),
  153. ATTR_LIST(timer),
  154. ATTR_LIST(timer_timeout),
  155. ATTR_LIST(wakesrc),
  156. ATTR_LIST(wakemsk),
  157. NULL, /* terminator */
  158. };
  159. static struct attribute_group db1x_pmattr_group = {
  160. .name = "db1x",
  161. .attrs = db1x_pmattrs,
  162. };
  163. /*
  164. * Initialize suspend interface
  165. */
  166. static int __init pm_init(void)
  167. {
  168. /* init TOY to tick at 1Hz if not already done. No need to wait
  169. * for confirmation since there's plenty of time from here to
  170. * the next suspend cycle.
  171. */
  172. if (au_readl(SYS_TOYTRIM) != 32767) {
  173. au_writel(32767, SYS_TOYTRIM);
  174. au_sync();
  175. }
  176. db1x_pm_last_wakesrc = au_readl(SYS_WAKESRC);
  177. au_writel(0, SYS_WAKESRC);
  178. au_sync();
  179. au_writel(0, SYS_WAKEMSK);
  180. au_sync();
  181. suspend_set_ops(&db1x_pm_ops);
  182. return sysfs_create_group(power_kobj, &db1x_pmattr_group);
  183. }
  184. late_initcall(pm_init);