drivers/rtc/rtc-ds2404.c
Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-ds2404.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/rtc/rtc-ds2404.c- Extension
.c- Size
- 5127 bytes
- Lines
- 226
- 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.
- 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/platform_device.hlinux/module.hlinux/init.hlinux/rtc.hlinux/types.hlinux/bcd.hlinux/delay.hlinux/gpio/consumer.hlinux/slab.hlinux/io.h
Detected Declarations
struct ds2404function ds2404_gpio_mapfunction ds2404_resetfunction ds2404_write_bytefunction ds2404_read_bytefunction ds2404_read_memoryfunction ds2404_write_memoryfunction ds2404_enable_oscfunction ds2404_read_timefunction ds2404_set_timefunction rtc_probe
Annotated Snippet
struct ds2404 {
struct device *dev;
struct gpio_desc *rst_gpiod;
struct gpio_desc *clk_gpiod;
struct gpio_desc *dq_gpiod;
};
static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
/* This will de-assert RESET, declare this GPIO as GPIOD_ACTIVE_LOW */
chip->rst_gpiod = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW);
if (IS_ERR(chip->rst_gpiod))
return PTR_ERR(chip->rst_gpiod);
chip->clk_gpiod = devm_gpiod_get(dev, "clk", GPIOD_OUT_HIGH);
if (IS_ERR(chip->clk_gpiod))
return PTR_ERR(chip->clk_gpiod);
chip->dq_gpiod = devm_gpiod_get(dev, "dq", GPIOD_ASIS);
if (IS_ERR(chip->dq_gpiod))
return PTR_ERR(chip->dq_gpiod);
return 0;
}
static void ds2404_reset(struct ds2404 *chip)
{
gpiod_set_value(chip->rst_gpiod, 1);
udelay(1000);
gpiod_set_value(chip->rst_gpiod, 0);
gpiod_set_value(chip->clk_gpiod, 0);
gpiod_direction_output(chip->dq_gpiod, 0);
udelay(10);
}
static void ds2404_write_byte(struct ds2404 *chip, u8 byte)
{
int i;
gpiod_direction_output(chip->dq_gpiod, 1);
for (i = 0; i < 8; i++) {
gpiod_set_value(chip->dq_gpiod, byte & (1 << i));
udelay(10);
gpiod_set_value(chip->clk_gpiod, 1);
udelay(10);
gpiod_set_value(chip->clk_gpiod, 0);
udelay(10);
}
}
static u8 ds2404_read_byte(struct ds2404 *chip)
{
int i;
u8 ret = 0;
gpiod_direction_input(chip->dq_gpiod);
for (i = 0; i < 8; i++) {
gpiod_set_value(chip->clk_gpiod, 0);
udelay(10);
if (gpiod_get_value(chip->dq_gpiod))
ret |= 1 << i;
gpiod_set_value(chip->clk_gpiod, 1);
udelay(10);
}
return ret;
}
static void ds2404_read_memory(struct ds2404 *chip, u16 offset,
int length, u8 *out)
{
ds2404_reset(chip);
ds2404_write_byte(chip, DS2404_READ_MEMORY_CMD);
ds2404_write_byte(chip, offset & 0xff);
ds2404_write_byte(chip, (offset >> 8) & 0xff);
while (length--)
*out++ = ds2404_read_byte(chip);
}
static void ds2404_write_memory(struct ds2404 *chip, u16 offset,
int length, u8 *out)
{
int i;
u8 ta01, ta02, es;
ds2404_reset(chip);
ds2404_write_byte(chip, DS2404_WRITE_SCRATCHPAD_CMD);
ds2404_write_byte(chip, offset & 0xff);
Annotation
- Immediate include surface: `linux/platform_device.h`, `linux/module.h`, `linux/init.h`, `linux/rtc.h`, `linux/types.h`, `linux/bcd.h`, `linux/delay.h`, `linux/gpio/consumer.h`.
- Detected declarations: `struct ds2404`, `function ds2404_gpio_map`, `function ds2404_reset`, `function ds2404_write_byte`, `function ds2404_read_byte`, `function ds2404_read_memory`, `function ds2404_write_memory`, `function ds2404_enable_osc`, `function ds2404_read_time`, `function ds2404_set_time`.
- Atlas domain: Driver Families / drivers/rtc.
- Implementation status: source implementation candidate.
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.