drivers/i2c/busses/i2c-bcm2835.c
Source file repositories/reference/linux-study-clean/drivers/i2c/busses/i2c-bcm2835.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/i2c/busses/i2c-bcm2835.c- Extension
.c- Size
- 14179 bytes
- Lines
- 537
- Domain
- Driver Families
- Bucket
- drivers/i2c
- 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.
- 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/clk.hlinux/clkdev.hlinux/clk-provider.hlinux/completion.hlinux/err.hlinux/i2c.hlinux/interrupt.hlinux/io.hlinux/module.hlinux/of.hlinux/platform_device.hlinux/slab.h
Detected Declarations
struct bcm2835_i2c_devstruct clk_bcm2835_i2cfunction bcm2835_i2c_writelfunction bcm2835_i2c_readlfunction clk_bcm2835_i2c_calc_dividerfunction clk_bcm2835_i2c_set_ratefunction clk_bcm2835_i2c_determine_ratefunction clk_bcm2835_i2c_recalc_ratefunction bcm2835_fill_txfifofunction bcm2835_drain_rxfifofunction Conditionfunction bcm2835_i2c_finish_transferfunction bcm2835_i2c_isrfunction bcm2835_i2c_xferfunction bcm2835_i2c_funcfunction bcm2835_i2c_probefunction bcm2835_i2c_remove
Annotated Snippet
struct bcm2835_i2c_dev {
struct device *dev;
void __iomem *regs;
int irq;
struct i2c_adapter adapter;
struct completion completion;
struct i2c_msg *curr_msg;
struct clk *bus_clk;
int num_msgs;
u32 msg_err;
u8 *msg_buf;
size_t msg_buf_remaining;
};
static inline void bcm2835_i2c_writel(struct bcm2835_i2c_dev *i2c_dev,
u32 reg, u32 val)
{
writel(val, i2c_dev->regs + reg);
}
static inline u32 bcm2835_i2c_readl(struct bcm2835_i2c_dev *i2c_dev, u32 reg)
{
return readl(i2c_dev->regs + reg);
}
#define to_clk_bcm2835_i2c(_hw) container_of(_hw, struct clk_bcm2835_i2c, hw)
struct clk_bcm2835_i2c {
struct clk_hw hw;
struct bcm2835_i2c_dev *i2c_dev;
};
static int clk_bcm2835_i2c_calc_divider(unsigned long rate,
unsigned long parent_rate)
{
u32 divider = DIV_ROUND_UP(parent_rate, rate);
/*
* Per the datasheet, the register is always interpreted as an even
* number, by rounding down. In other words, the LSB is ignored. So,
* if the LSB is set, increment the divider to avoid any issue.
*/
if (divider & 1)
divider++;
if ((divider < BCM2835_I2C_CDIV_MIN) ||
(divider > BCM2835_I2C_CDIV_MAX))
return -EINVAL;
return divider;
}
static int clk_bcm2835_i2c_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long parent_rate)
{
struct clk_bcm2835_i2c *div = to_clk_bcm2835_i2c(hw);
u32 redl, fedl;
u32 divider = clk_bcm2835_i2c_calc_divider(rate, parent_rate);
if (divider == -EINVAL)
return -EINVAL;
bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DIV, divider);
/*
* Number of core clocks to wait after falling edge before
* outputting the next data bit. Note that both FEDL and REDL
* can't be greater than CDIV/2.
*/
fedl = max(divider / 16, 1u);
/*
* Number of core clocks to wait after rising edge before
* sampling the next incoming data bit.
*/
redl = max(divider / 4, 1u);
bcm2835_i2c_writel(div->i2c_dev, BCM2835_I2C_DEL,
(fedl << BCM2835_I2C_FEDL_SHIFT) |
(redl << BCM2835_I2C_REDL_SHIFT));
return 0;
}
static int clk_bcm2835_i2c_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
u32 divider = clk_bcm2835_i2c_calc_divider(req->rate, req->best_parent_rate);
req->rate = DIV_ROUND_UP(req->best_parent_rate, divider);
return 0;
}
Annotation
- Immediate include surface: `linux/clk.h`, `linux/clkdev.h`, `linux/clk-provider.h`, `linux/completion.h`, `linux/err.h`, `linux/i2c.h`, `linux/interrupt.h`, `linux/io.h`.
- Detected declarations: `struct bcm2835_i2c_dev`, `struct clk_bcm2835_i2c`, `function bcm2835_i2c_writel`, `function bcm2835_i2c_readl`, `function clk_bcm2835_i2c_calc_divider`, `function clk_bcm2835_i2c_set_rate`, `function clk_bcm2835_i2c_determine_rate`, `function clk_bcm2835_i2c_recalc_rate`, `function bcm2835_fill_txfifo`, `function bcm2835_drain_rxfifo`.
- Atlas domain: Driver Families / drivers/i2c.
- Implementation status: source implementation candidate.
- 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.