drivers/rtc/rtc-m48t59.c
Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-m48t59.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/rtc/rtc-m48t59.c- Extension
.c- Size
- 12439 bytes
- Lines
- 473
- 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.
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/module.hlinux/init.hlinux/io.hlinux/device.hlinux/platform_device.hlinux/rtc.hlinux/rtc/m48t59.hlinux/bcd.hlinux/slab.h
Detected Declarations
struct m48t59_privatefunction m48t59_mem_writebfunction m48t59_mem_readbfunction m48t59_rtc_read_timefunction m48t59_rtc_set_timefunction m48t59_rtc_readalarmfunction m48t59_rtc_setalarmfunction m48t59_rtc_alarm_irq_enablefunction m48t59_rtc_procfunction m48t59_rtc_interruptfunction m48t59_nvram_readfunction m48t59_nvram_writefunction m48t59_rtc_probe
Annotated Snippet
struct m48t59_private {
void __iomem *ioaddr;
int irq;
struct rtc_device *rtc;
spinlock_t lock; /* serialize the NVRAM and RTC access */
};
/*
* This is the generic access method when the chip is memory-mapped
*/
static void
m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
{
struct m48t59_private *m48t59 = dev_get_drvdata(dev);
writeb(val, m48t59->ioaddr+ofs);
}
static u8
m48t59_mem_readb(struct device *dev, u32 ofs)
{
struct m48t59_private *m48t59 = dev_get_drvdata(dev);
return readb(m48t59->ioaddr+ofs);
}
/*
* NOTE: M48T59 only uses BCD mode
*/
static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
struct m48t59_plat_data *pdata = dev_get_platdata(dev);
struct m48t59_private *m48t59 = dev_get_drvdata(dev);
unsigned long flags;
u8 val;
spin_lock_irqsave(&m48t59->lock, flags);
/* Issue the READ command */
M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR)) + pdata->yy_offset;
/* tm_mon is 0-11 */
tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
val = M48T59_READ(M48T59_WDAY);
if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
(val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
dev_dbg(dev, "Century bit is enabled\n");
tm->tm_year += 100; /* one century */
}
tm->tm_wday = bcd2bin(val & 0x07);
tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
/* Clear the READ bit */
M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
spin_unlock_irqrestore(&m48t59->lock, flags);
dev_dbg(dev, "RTC read time %ptR\n", tm);
return 0;
}
static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
struct m48t59_plat_data *pdata = dev_get_platdata(dev);
struct m48t59_private *m48t59 = dev_get_drvdata(dev);
unsigned long flags;
u8 val = 0;
int year = tm->tm_year - pdata->yy_offset;
dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
year + 1900, tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (year < 0)
return -EINVAL;
spin_lock_irqsave(&m48t59->lock, flags);
/* Issue the WRITE command */
M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
/* tm_mon is 0-11 */
M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/init.h`, `linux/io.h`, `linux/device.h`, `linux/platform_device.h`, `linux/rtc.h`, `linux/rtc/m48t59.h`.
- Detected declarations: `struct m48t59_private`, `function m48t59_mem_writeb`, `function m48t59_mem_readb`, `function m48t59_rtc_read_time`, `function m48t59_rtc_set_time`, `function m48t59_rtc_readalarm`, `function m48t59_rtc_setalarm`, `function m48t59_rtc_alarm_irq_enable`, `function m48t59_rtc_proc`, `function m48t59_rtc_interrupt`.
- Atlas domain: Driver Families / drivers/rtc.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.