samples/pfsm/pfsm-wakeup.c

Source file repositories/reference/linux-study-clean/samples/pfsm/pfsm-wakeup.c

File Facts

System
Linux kernel
Corpus path
samples/pfsm/pfsm-wakeup.c
Extension
.c
Size
3018 bytes
Lines
126
Domain
Support Tooling And Documentation
Bucket
samples
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (fd_pfsm[i] < 0) {
			perror("Failed to open PFSM device.");
			goto out;
		}
	}

	/* Read RTC date/time */
	ret = ioctl(fd_rtc, RTC_RD_TIME, &rtc_tm);
	if (ret < 0) {
		perror("Failed to read RTC date/time.");
		goto out;
	}
	printf("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
	       rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
	       rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

	/* Set RTC alarm to ALARM_DELTA_SEC sec in the future, and check for rollover */
	rtc_tm.tm_sec += ALARM_DELTA_SEC;
	if (rtc_tm.tm_sec >= 60) {
		rtc_tm.tm_sec %= 60;
		rtc_tm.tm_min++;
	}
	if (rtc_tm.tm_min == 60) {
		rtc_tm.tm_min = 0;
		rtc_tm.tm_hour++;
	}
	if (rtc_tm.tm_hour == 24)
		rtc_tm.tm_hour = 0;
	ret = ioctl(fd_rtc, RTC_ALM_SET, &rtc_tm);
	if (ret < 0) {
		perror("Failed to set RTC alarm.");
		goto out;
	}

	/* Enable alarm interrupts */
	ret = ioctl(fd_rtc, RTC_AIE_ON, 0);
	if (ret < 0) {
		perror("Failed to enable alarm interrupts.");
		goto out;
	}
	printf("Waiting %d seconds for alarm...\n", ALARM_DELTA_SEC);

	/*
	 * Set RETENTION state with options for PMIC_C/B/A respectively.
	 * Since PMIC_A is master, it should be the last one to be configured.
	 */
	pmic_opt.ddr_retention = 1;
	for (i = PMIC_NB - 1 ; i >= 0 ; i--) {
		printf("Set RETENTION state for PMIC_%d.\n", i);
		sleep(1);
		ret = ioctl(fd_pfsm[i], PMIC_SET_RETENTION_STATE, &pmic_opt);
		if (ret < 0) {
			perror("Failed to set RETENTION state.");
			goto out_reset;
		}
	}

	/* This blocks until the alarm ring causes an interrupt */
	ret = read(fd_rtc, &data, sizeof(unsigned long));
	if (ret < 0)
		perror("Failed to get RTC alarm.");
	else
		puts("Alarm rang.\n");

out_reset:
	ioctl(fd_rtc, RTC_AIE_OFF, 0);

	/* Set ACTIVE state for PMIC_A */
	ioctl(fd_pfsm[0], PMIC_SET_ACTIVE_STATE, 0);

out:
	for (i = 0 ; i < PMIC_NB ; i++)
		if (fd_pfsm[i])
			close(fd_pfsm[i]);

	if (fd_rtc)
		close(fd_rtc);

	return 0;
}

Annotation

Implementation Notes