pm.c 6.1 KB

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