drivers/rtc/rtc-ntxec.c

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

File Facts

System
Linux kernel
Corpus path
drivers/rtc/rtc-ntxec.c
Extension
.c
Size
4001 bytes
Lines
146
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 ntxec_rtc {
	struct device *dev;
	struct ntxec *ec;
};

#define NTXEC_REG_WRITE_YEAR	0x10
#define NTXEC_REG_WRITE_MONTH	0x11
#define NTXEC_REG_WRITE_DAY	0x12
#define NTXEC_REG_WRITE_HOUR	0x13
#define NTXEC_REG_WRITE_MINUTE	0x14
#define NTXEC_REG_WRITE_SECOND	0x15

#define NTXEC_REG_READ_YEAR_MONTH	0x20
#define NTXEC_REG_READ_MDAY_HOUR	0x21
#define NTXEC_REG_READ_MINUTE_SECOND	0x23

static int ntxec_read_time(struct device *dev, struct rtc_time *tm)
{
	struct ntxec_rtc *rtc = dev_get_drvdata(dev);
	unsigned int value;
	int res;

retry:
	res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value);
	if (res < 0)
		return res;

	tm->tm_min = value >> 8;
	tm->tm_sec = value & 0xff;

	res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MDAY_HOUR, &value);
	if (res < 0)
		return res;

	tm->tm_mday = value >> 8;
	tm->tm_hour = value & 0xff;

	res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_YEAR_MONTH, &value);
	if (res < 0)
		return res;

	tm->tm_year = (value >> 8) + 100;
	tm->tm_mon = (value & 0xff) - 1;

	/*
	 * Read the minutes/seconds field again. If it changed since the first
	 * read, we can't assume that the values read so far are consistent,
	 * and should start from the beginning.
	 */
	res = regmap_read(rtc->ec->regmap, NTXEC_REG_READ_MINUTE_SECOND, &value);
	if (res < 0)
		return res;

	if (tm->tm_min != value >> 8 || tm->tm_sec != (value & 0xff))
		goto retry;

	return 0;
}

static int ntxec_set_time(struct device *dev, struct rtc_time *tm)
{
	struct ntxec_rtc *rtc = dev_get_drvdata(dev);

	/*
	 * To avoid time overflows while we're writing the full date/time,
	 * set the seconds field to zero before doing anything else. For the
	 * next 59 seconds (plus however long it takes until the RTC's next
	 * update of the second field), the seconds field will not overflow
	 * into the other fields.
	 */
	struct reg_sequence regs[] = {
		{ NTXEC_REG_WRITE_SECOND, ntxec_reg8(0) },
		{ NTXEC_REG_WRITE_YEAR, ntxec_reg8(tm->tm_year - 100) },
		{ NTXEC_REG_WRITE_MONTH, ntxec_reg8(tm->tm_mon + 1) },
		{ NTXEC_REG_WRITE_DAY, ntxec_reg8(tm->tm_mday) },
		{ NTXEC_REG_WRITE_HOUR, ntxec_reg8(tm->tm_hour) },
		{ NTXEC_REG_WRITE_MINUTE, ntxec_reg8(tm->tm_min) },
		{ NTXEC_REG_WRITE_SECOND, ntxec_reg8(tm->tm_sec) },
	};

	return regmap_multi_reg_write(rtc->ec->regmap, regs, ARRAY_SIZE(regs));
}

static const struct rtc_class_ops ntxec_rtc_ops = {
	.read_time = ntxec_read_time,
	.set_time = ntxec_set_time,
};

static int ntxec_rtc_probe(struct platform_device *pdev)
{

Annotation

Implementation Notes