pfsm-wakeup.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TPS6594 PFSM userspace example
  4. *
  5. * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
  6. *
  7. * This example shows how to use PFSMs from a userspace application,
  8. * on TI j721s2 platform. The PMIC is armed to be triggered by a RTC
  9. * alarm to execute state transition (RETENTION to ACTIVE).
  10. */
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <sys/ioctl.h>
  14. #include <unistd.h>
  15. #include <linux/rtc.h>
  16. #include <linux/tps6594_pfsm.h>
  17. #define ALARM_DELTA_SEC 30
  18. #define RTC_A "/dev/rtc0"
  19. #define PMIC_NB 3
  20. #define PMIC_A "/dev/pfsm-0-0x48"
  21. #define PMIC_B "/dev/pfsm-0-0x4c"
  22. #define PMIC_C "/dev/pfsm-2-0x58"
  23. static const char * const dev_pfsm[] = {PMIC_A, PMIC_B, PMIC_C};
  24. int main(int argc, char *argv[])
  25. {
  26. int i, ret, fd_rtc, fd_pfsm[PMIC_NB] = { 0 };
  27. struct rtc_time rtc_tm;
  28. struct pmic_state_opt pmic_opt = { 0 };
  29. unsigned long data;
  30. fd_rtc = open(RTC_A, O_RDONLY);
  31. if (fd_rtc < 0) {
  32. perror("Failed to open RTC device.");
  33. goto out;
  34. }
  35. for (i = 0 ; i < PMIC_NB ; i++) {
  36. fd_pfsm[i] = open(dev_pfsm[i], O_RDWR);
  37. if (fd_pfsm[i] < 0) {
  38. perror("Failed to open PFSM device.");
  39. goto out;
  40. }
  41. }
  42. /* Read RTC date/time */
  43. ret = ioctl(fd_rtc, RTC_RD_TIME, &rtc_tm);
  44. if (ret < 0) {
  45. perror("Failed to read RTC date/time.");
  46. goto out;
  47. }
  48. printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
  49. rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  50. rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  51. /* Set RTC alarm to ALARM_DELTA_SEC sec in the future, and check for rollover */
  52. rtc_tm.tm_sec += ALARM_DELTA_SEC;
  53. if (rtc_tm.tm_sec >= 60) {
  54. rtc_tm.tm_sec %= 60;
  55. rtc_tm.tm_min++;
  56. }
  57. if (rtc_tm.tm_min == 60) {
  58. rtc_tm.tm_min = 0;
  59. rtc_tm.tm_hour++;
  60. }
  61. if (rtc_tm.tm_hour == 24)
  62. rtc_tm.tm_hour = 0;
  63. ret = ioctl(fd_rtc, RTC_ALM_SET, &rtc_tm);
  64. if (ret < 0) {
  65. perror("Failed to set RTC alarm.");
  66. goto out;
  67. }
  68. /* Enable alarm interrupts */
  69. ret = ioctl(fd_rtc, RTC_AIE_ON, 0);
  70. if (ret < 0) {
  71. perror("Failed to enable alarm interrupts.");
  72. goto out;
  73. }
  74. printf("Waiting %d seconds for alarm...\n", ALARM_DELTA_SEC);
  75. /*
  76. * Set RETENTION state with options for PMIC_C/B/A respectively.
  77. * Since PMIC_A is master, it should be the last one to be configured.
  78. */
  79. pmic_opt.ddr_retention = 1;
  80. for (i = PMIC_NB - 1 ; i >= 0 ; i--) {
  81. printf("Set RETENTION state for PMIC_%d.\n", i);
  82. sleep(1);
  83. ret = ioctl(fd_pfsm[i], PMIC_SET_RETENTION_STATE, &pmic_opt);
  84. if (ret < 0) {
  85. perror("Failed to set RETENTION state.");
  86. goto out_reset;
  87. }
  88. }
  89. /* This blocks until the alarm ring causes an interrupt */
  90. ret = read(fd_rtc, &data, sizeof(unsigned long));
  91. if (ret < 0)
  92. perror("Failed to get RTC alarm.");
  93. else
  94. puts("Alarm rang.\n");
  95. out_reset:
  96. ioctl(fd_rtc, RTC_AIE_OFF, 0);
  97. /* Set ACTIVE state for PMIC_A */
  98. ioctl(fd_pfsm[0], PMIC_SET_ACTIVE_STATE, 0);
  99. out:
  100. for (i = 0 ; i < PMIC_NB ; i++)
  101. if (fd_pfsm[i])
  102. close(fd_pfsm[i]);
  103. if (fd_rtc)
  104. close(fd_rtc);
  105. return 0;
  106. }