drivers/pwm/pwm-berlin.c
Source file repositories/reference/linux-study-clean/drivers/pwm/pwm-berlin.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/pwm/pwm-berlin.c- Extension
.c- Size
- 7476 bytes
- Lines
- 287
- Domain
- Driver Families
- Bucket
- drivers/pwm
- 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/clk.hlinux/io.hlinux/kernel.hlinux/mod_devicetable.hlinux/module.hlinux/platform_device.hlinux/pwm.hlinux/slab.h
Detected Declarations
struct berlin_pwm_channelstruct berlin_pwm_chipfunction berlin_pwm_readlfunction berlin_pwm_writelfunction berlin_pwm_configfunction berlin_pwm_set_polarityfunction berlin_pwm_enablefunction berlin_pwm_disablefunction berlin_pwm_applyfunction berlin_pwm_probefunction berlin_pwm_suspendfunction berlin_pwm_resume
Annotated Snippet
struct berlin_pwm_channel {
u32 enable;
u32 ctrl;
u32 duty;
u32 tcnt;
};
struct berlin_pwm_chip {
struct clk *clk;
void __iomem *base;
struct berlin_pwm_channel channel[BERLIN_PWM_NUMPWMS];
};
static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc,
unsigned int channel, unsigned long offset)
{
return readl_relaxed(bpc->base + channel * 0x10 + offset);
}
static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
unsigned int channel, u32 value,
unsigned long offset)
{
writel_relaxed(value, bpc->base + channel * 0x10 + offset);
}
static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
u64 duty_ns, u64 period_ns)
{
struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
bool prescale_4096 = false;
u32 value, duty, period;
u64 cycles;
cycles = clk_get_rate(bpc->clk);
cycles *= period_ns;
do_div(cycles, NSEC_PER_SEC);
if (cycles > BERLIN_PWM_MAX_TCNT) {
prescale_4096 = true;
cycles >>= 12; // Prescaled by 4096
if (cycles > BERLIN_PWM_MAX_TCNT)
return -ERANGE;
}
period = cycles;
cycles *= duty_ns;
do_div(cycles, period_ns);
duty = cycles;
value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
if (prescale_4096)
value |= BERLIN_PWM_PRESCALE_4096;
else
value &= ~BERLIN_PWM_PRESCALE_4096;
berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
berlin_pwm_writel(bpc, pwm->hwpwm, duty, BERLIN_PWM_DUTY);
berlin_pwm_writel(bpc, pwm->hwpwm, period, BERLIN_PWM_TCNT);
return 0;
}
static int berlin_pwm_set_polarity(struct pwm_chip *chip,
struct pwm_device *pwm,
enum pwm_polarity polarity)
{
struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
u32 value;
value = berlin_pwm_readl(bpc, pwm->hwpwm, BERLIN_PWM_CONTROL);
if (polarity == PWM_POLARITY_NORMAL)
value &= ~BERLIN_PWM_INVERT_POLARITY;
else
value |= BERLIN_PWM_INVERT_POLARITY;
berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_CONTROL);
return 0;
}
static int berlin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
Annotation
- Immediate include surface: `linux/clk.h`, `linux/io.h`, `linux/kernel.h`, `linux/mod_devicetable.h`, `linux/module.h`, `linux/platform_device.h`, `linux/pwm.h`, `linux/slab.h`.
- Detected declarations: `struct berlin_pwm_channel`, `struct berlin_pwm_chip`, `function berlin_pwm_readl`, `function berlin_pwm_writel`, `function berlin_pwm_config`, `function berlin_pwm_set_polarity`, `function berlin_pwm_enable`, `function berlin_pwm_disable`, `function berlin_pwm_apply`, `function berlin_pwm_probe`.
- Atlas domain: Driver Families / drivers/pwm.
- 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.