drivers/rtc/rtc-cros-ec.c

Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-cros-ec.c

File Facts

System
Linux kernel
Corpus path
drivers/rtc/rtc-cros-ec.c
Extension
.c
Size
10700 bytes
Lines
411
Domain
Driver Families
Bucket
drivers/rtc
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct cros_ec_rtc {
	struct cros_ec_device *cros_ec;
	struct rtc_device *rtc;
	struct notifier_block notifier;
	u32 saved_alarm;
};

static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
			   u32 *response)
{
	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
			sizeof(struct ec_response_rtc));
	int ret;

	msg->command = command;
	msg->insize = sizeof(struct ec_response_rtc);

	ret = cros_ec_cmd_xfer_status(cros_ec, msg);
	if (ret < 0)
		return ret;

	*response = ((struct ec_response_rtc *)msg->data)->time;

	return 0;
}

static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
			   u32 param)
{
	DEFINE_RAW_FLEX(struct cros_ec_command, msg, data,
			sizeof(struct ec_response_rtc));
	int ret;

	msg->command = command;
	msg->outsize = sizeof(struct ec_response_rtc);
	((struct ec_response_rtc *)msg->data)->time = param;

	ret = cros_ec_cmd_xfer_status(cros_ec, msg);
	if (ret < 0)
		return ret;
	return 0;
}

/* Read the current time from the EC. */
static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	u32 time;

	ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
	if (ret) {
		dev_err(dev, "error getting time: %d\n", ret);
		return ret;
	}

	rtc_time64_to_tm(time, tm);

	return 0;
}

/* Set the current EC time. */
static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	time64_t time = rtc_tm_to_time64(tm);

	ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
	if (ret < 0) {
		dev_err(dev, "error setting time: %d\n", ret);
		return ret;
	}

	return 0;
}

/* Read alarm time from RTC. */
static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
	struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
	int ret;
	u32 current_time, alarm_offset;

	/*
	 * The EC host command for getting the alarm is relative (i.e. 5
	 * seconds from now) whereas rtc_wkalrm is absolute. Get the current

Annotation

Implementation Notes