#ifndef __COMPAT_LINUX_DELAY_H__
#define __COMPAT_LINUX_DELAY_H__

#include_next <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/time64.h>

#ifndef COMPAT_DETECT_FSLEEP_EXISTS_5_8

#define COMPAT_USLEEP_RANGE_UPPER_BOUND ((TICK_NSEC << max_slack_shift) / NSEC_PER_USEC)
static inline void fsleep(unsigned long usecs)
{
	static const unsigned int max_slack_shift = 2;

	if (usecs <= 10)
		udelay(usecs);
	else if (usecs < COMPAT_USLEEP_RANGE_UPPER_BOUND)
		usleep_range(usecs, usecs + (usecs >> 2));
	else
		msleep(DIV_ROUND_UP(usecs, USEC_PER_MSEC));

#undef COMPAT_USLEEP_RANGE_UPPER_BOUND
}
#endif

#endif /* __COMPAT_LINUX_DELAY_H__ */
