pm.c 6.0 KB

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