drivers/platform/cznic/turris-omnia-mcu-sys-off-wakeup.c

Source file repositories/reference/linux-study-clean/drivers/platform/cznic/turris-omnia-mcu-sys-off-wakeup.c

File Facts

System
Linux kernel
Corpus path
drivers/platform/cznic/turris-omnia-mcu-sys-off-wakeup.c
Extension
.c
Size
6391 bytes
Lines
261
Domain
Driver Families
Bucket
drivers/platform
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

// SPDX-License-Identifier: GPL-2.0
/*
 * CZ.NIC's Turris Omnia MCU system off and RTC wakeup driver
 *
 * This is not a true RTC driver (in the sense that it does not provide a
 * real-time clock), rather the MCU implements a wakeup from powered off state
 * at a specified time relative to MCU boot, and we expose this feature via RTC
 * alarm, so that it can be used via the rtcwake command, which is the standard
 * Linux command for this.
 *
 * 2024 by Marek BehĂșn <kabel@kernel.org>
 */

#include <linux/crc32.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/kstrtox.h>
#include <linux/reboot.h>
#include <linux/rtc.h>
#include <linux/sysfs.h>
#include <linux/types.h>

#include <linux/turris-omnia-mcu-interface.h>
#include "turris-omnia-mcu.h"

static int omnia_get_uptime_wakeup(const struct i2c_client *client, u32 *uptime,
				   u32 *wakeup)
{
	__le32 reply[2];
	int err;

	err = omnia_cmd_read(client, OMNIA_CMD_GET_UPTIME_AND_WAKEUP, reply,
			     sizeof(reply));
	if (err)
		return err;

	if (uptime)
		*uptime = le32_to_cpu(reply[0]);

	if (wakeup)
		*wakeup = le32_to_cpu(reply[1]);

	return 0;
}

static int omnia_read_time(struct device *dev, struct rtc_time *tm)
{
	u32 uptime;
	int err;

	err = omnia_get_uptime_wakeup(to_i2c_client(dev), &uptime, NULL);
	if (err)
		return err;

	rtc_time64_to_tm(uptime, tm);

	return 0;
}

static int omnia_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct omnia_mcu *mcu = i2c_get_clientdata(client);
	u32 wakeup;
	int err;

	err = omnia_get_uptime_wakeup(client, NULL, &wakeup);
	if (err)
		return err;

	alrm->enabled = !!wakeup;
	rtc_time64_to_tm(wakeup ?: mcu->rtc_alarm, &alrm->time);

	return 0;
}

static int omnia_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct omnia_mcu *mcu = i2c_get_clientdata(client);

	mcu->rtc_alarm = rtc_tm_to_time64(&alrm->time);

	if (alrm->enabled)
		return omnia_cmd_write_u32(client, OMNIA_CMD_SET_WAKEUP,
					   mcu->rtc_alarm);

	return 0;

Annotation

Implementation Notes