pm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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-au1000.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. alchemy_wrsys(0, AU1000_SYS_WAKEMSK);
  39. alchemy_wrsys(0, AU1000_SYS_WAKESRC);
  40. alchemy_wrsys(db1x_pm_wakemsk, AU1000_SYS_WAKEMSK);
  41. /* setup 1Hz-timer-based wakeup: wait for reg access */
  42. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M20)
  43. asm volatile ("nop");
  44. alchemy_wrsys(alchemy_rdsys(AU1000_SYS_TOYREAD) + db1x_pm_sleep_secs,
  45. AU1000_SYS_TOYMATCH2);
  46. /* wait for value to really hit the register */
  47. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M20)
  48. asm volatile ("nop");
  49. /* ...and now the sandman can come! */
  50. au_sleep();
  51. /* restore CPLD regs */
  52. for (i = BCSR_STATUS; i <= BCSR_SYSTEM; i++)
  53. bcsr_write(i, bcsrs[i]);
  54. /* restore CPLD int registers */
  55. if (hasint) {
  56. bcsr_write(BCSR_INTCLR, 0xffff);
  57. bcsr_write(BCSR_MASKCLR, 0xffff);
  58. bcsr_write(BCSR_INTSTAT, 0xffff);
  59. bcsr_write(BCSR_INTSET, bcsrs[BCSR_INTSET]);
  60. bcsr_write(BCSR_MASKSET, bcsrs[BCSR_MASKSET]);
  61. }
  62. /* light up hexleds */
  63. bcsr_write(BCSR_HEXCLEAR, 0);
  64. return 0;
  65. }
  66. static int db1x_pm_begin(suspend_state_t state)
  67. {
  68. if (!db1x_pm_wakemsk) {
  69. printk(KERN_ERR "db1x: no wakeup source activated!\n");
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static void db1x_pm_end(void)
  75. {
  76. /* read and store wakeup source, the clear the register. To
  77. * be able to clear it, WAKEMSK must be cleared first.
  78. */
  79. db1x_pm_last_wakesrc = alchemy_rdsys(AU1000_SYS_WAKESRC);
  80. alchemy_wrsys(0, AU1000_SYS_WAKEMSK);
  81. alchemy_wrsys(0, AU1000_SYS_WAKESRC);
  82. }
  83. static const struct platform_suspend_ops db1x_pm_ops = {
  84. .valid = suspend_valid_only_mem,
  85. .begin = db1x_pm_begin,
  86. .enter = db1x_pm_enter,
  87. .end = db1x_pm_end,
  88. };
  89. #define ATTRCMP(x) (0 == strcmp(attr->attr.name, #x))
  90. static ssize_t db1x_pmattr_show(struct kobject *kobj,
  91. struct kobj_attribute *attr,
  92. char *buf)
  93. {
  94. int idx;
  95. if (ATTRCMP(timer_timeout))
  96. return sprintf(buf, "%lu\n", db1x_pm_sleep_secs);
  97. else if (ATTRCMP(timer))
  98. return sprintf(buf, "%u\n",
  99. !!(db1x_pm_wakemsk & SYS_WAKEMSK_M2));
  100. else if (ATTRCMP(wakesrc))
  101. return sprintf(buf, "%lu\n", db1x_pm_last_wakesrc);
  102. else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
  103. ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
  104. ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
  105. idx = (attr->attr.name)[4] - '0';
  106. return sprintf(buf, "%d\n",
  107. !!(db1x_pm_wakemsk & SYS_WAKEMSK_GPIO(idx)));
  108. } else if (ATTRCMP(wakemsk)) {
  109. return sprintf(buf, "%08lx\n", db1x_pm_wakemsk);
  110. }
  111. return -ENOENT;
  112. }
  113. static ssize_t db1x_pmattr_store(struct kobject *kobj,
  114. struct kobj_attribute *attr,
  115. const char *instr,
  116. size_t bytes)
  117. {
  118. unsigned long l;
  119. int tmp;
  120. if (ATTRCMP(timer_timeout)) {
  121. tmp = kstrtoul(instr, 0, &l);
  122. if (tmp)
  123. return tmp;
  124. db1x_pm_sleep_secs = l;
  125. } else if (ATTRCMP(timer)) {
  126. if (instr[0] != '0')
  127. db1x_pm_wakemsk |= SYS_WAKEMSK_M2;
  128. else
  129. db1x_pm_wakemsk &= ~SYS_WAKEMSK_M2;
  130. } else if (ATTRCMP(gpio0) || ATTRCMP(gpio1) || ATTRCMP(gpio2) ||
  131. ATTRCMP(gpio3) || ATTRCMP(gpio4) || ATTRCMP(gpio5) ||
  132. ATTRCMP(gpio6) || ATTRCMP(gpio7)) {
  133. tmp = (attr->attr.name)[4] - '0';
  134. if (instr[0] != '0') {
  135. db1x_pm_wakemsk |= SYS_WAKEMSK_GPIO(tmp);
  136. } else {
  137. db1x_pm_wakemsk &= ~SYS_WAKEMSK_GPIO(tmp);
  138. }
  139. } else if (ATTRCMP(wakemsk)) {
  140. tmp = kstrtoul(instr, 0, &l);
  141. if (tmp)
  142. return tmp;
  143. db1x_pm_wakemsk = l & 0x0000003f;
  144. } else
  145. bytes = -ENOENT;
  146. return bytes;
  147. }
  148. #define ATTR(x) \
  149. static struct kobj_attribute x##_attribute = \
  150. __ATTR(x, 0664, db1x_pmattr_show, \
  151. db1x_pmattr_store);
  152. ATTR(gpio0) /* GPIO-based wakeup enable */
  153. ATTR(gpio1)
  154. ATTR(gpio2)
  155. ATTR(gpio3)
  156. ATTR(gpio4)
  157. ATTR(gpio5)
  158. ATTR(gpio6)
  159. ATTR(gpio7)
  160. ATTR(timer) /* TOYMATCH2-based wakeup enable */
  161. ATTR(timer_timeout) /* timer-based wakeup timeout value, in seconds */
  162. ATTR(wakesrc) /* contents of SYS_WAKESRC after last wakeup */
  163. ATTR(wakemsk) /* direct access to SYS_WAKEMSK */
  164. #define ATTR_LIST(x) & x ## _attribute.attr
  165. static struct attribute *db1x_pmattrs[] = {
  166. ATTR_LIST(gpio0),
  167. ATTR_LIST(gpio1),
  168. ATTR_LIST(gpio2),
  169. ATTR_LIST(gpio3),
  170. ATTR_LIST(gpio4),
  171. ATTR_LIST(gpio5),
  172. ATTR_LIST(gpio6),
  173. ATTR_LIST(gpio7),
  174. ATTR_LIST(timer),
  175. ATTR_LIST(timer_timeout),
  176. ATTR_LIST(wakesrc),
  177. ATTR_LIST(wakemsk),
  178. NULL, /* terminator */
  179. };
  180. static struct attribute_group db1x_pmattr_group = {
  181. .name = "db1x",
  182. .attrs = db1x_pmattrs,
  183. };
  184. /*
  185. * Initialize suspend interface
  186. */
  187. static int __init pm_init(void)
  188. {
  189. /* init TOY to tick at 1Hz if not already done. No need to wait
  190. * for confirmation since there's plenty of time from here to
  191. * the next suspend cycle.
  192. */
  193. if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767)
  194. alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
  195. db1x_pm_last_wakesrc = alchemy_rdsys(AU1000_SYS_WAKESRC);
  196. alchemy_wrsys(0, AU1000_SYS_WAKESRC);
  197. alchemy_wrsys(0, AU1000_SYS_WAKEMSK);
  198. suspend_set_ops(&db1x_pm_ops);
  199. return sysfs_create_group(power_kobj, &db1x_pmattr_group);
  200. }
  201. late_initcall(pm_init);