pm.c 5.2 KB

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