drivers/rtc/rtc-sd3078.c
Source file repositories/reference/linux-study-clean/drivers/rtc/rtc-sd3078.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/rtc/rtc-sd3078.c- Extension
.c- Size
- 5582 bytes
- Lines
- 214
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bcd.hlinux/i2c.hlinux/module.hlinux/regmap.hlinux/rtc.hlinux/slab.h
Detected Declarations
function Clockfunction sd3078_disable_reg_writefunction sd3078_rtc_read_timefunction sd3078_rtc_set_timefunction sd3078_probe
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Real Time Clock (RTC) Driver for sd3078
* Copyright (C) 2018 Zoro Li
*/
#include <linux/bcd.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/rtc.h>
#include <linux/slab.h>
#define SD3078_REG_SC 0x00
#define SD3078_REG_MN 0x01
#define SD3078_REG_HR 0x02
#define SD3078_REG_DW 0x03
#define SD3078_REG_DM 0x04
#define SD3078_REG_MO 0x05
#define SD3078_REG_YR 0x06
#define SD3078_REG_CTRL1 0x0f
#define SD3078_REG_CTRL2 0x10
#define SD3078_REG_CTRL3 0x11
#define KEY_WRITE1 0x80
#define KEY_WRITE2 0x04
#define KEY_WRITE3 0x80
#define NUM_TIME_REGS (SD3078_REG_YR - SD3078_REG_SC + 1)
/*
* The sd3078 has write protection
* and we can choose whether or not to use it.
* Write protection is turned off by default.
*/
#define WRITE_PROTECT_EN 0
/*
* In order to prevent arbitrary modification of the time register,
* when modification of the register,
* the "write" bit needs to be written in a certain order.
* 1. set WRITE1 bit
* 2. set WRITE2 bit
* 3. set WRITE3 bit
*/
static void sd3078_enable_reg_write(struct regmap *regmap)
{
regmap_update_bits(regmap, SD3078_REG_CTRL2, KEY_WRITE1, KEY_WRITE1);
regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE2, KEY_WRITE2);
regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE3, KEY_WRITE3);
}
#if WRITE_PROTECT_EN
/*
* In order to prevent arbitrary modification of the time register,
* we should disable the write function.
* when disable write,
* the "write" bit needs to be clear in a certain order.
* 1. clear WRITE2 bit
* 2. clear WRITE3 bit
* 3. clear WRITE1 bit
*/
static void sd3078_disable_reg_write(struct regmap *regmap)
{
regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE2, 0);
regmap_update_bits(regmap, SD3078_REG_CTRL1, KEY_WRITE3, 0);
regmap_update_bits(regmap, SD3078_REG_CTRL2, KEY_WRITE1, 0);
}
#endif
static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
unsigned char hour;
unsigned char rtc_data[NUM_TIME_REGS] = {0};
struct i2c_client *client = to_i2c_client(dev);
struct regmap *regmap = i2c_get_clientdata(client);
int ret;
ret = regmap_bulk_read(regmap, SD3078_REG_SC, rtc_data, NUM_TIME_REGS);
if (ret < 0) {
dev_err(dev, "reading from RTC failed with err:%d\n", ret);
return ret;
}
tm->tm_sec = bcd2bin(rtc_data[SD3078_REG_SC] & 0x7F);
tm->tm_min = bcd2bin(rtc_data[SD3078_REG_MN] & 0x7F);
/*
* The sd3078 supports 12/24 hour mode.
Annotation
- Immediate include surface: `linux/bcd.h`, `linux/i2c.h`, `linux/module.h`, `linux/regmap.h`, `linux/rtc.h`, `linux/slab.h`.
- Detected declarations: `function Clock`, `function sd3078_disable_reg_write`, `function sd3078_rtc_read_time`, `function sd3078_rtc_set_time`, `function sd3078_probe`.
- 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.