drivers/pwm/pwm-mxs.c
Source file repositories/reference/linux-study-clean/drivers/pwm/pwm-mxs.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/pwm/pwm-mxs.c- Extension
.c- Size
- 4564 bytes
- Lines
- 184
- 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/err.hlinux/io.hlinux/kernel.hlinux/module.hlinux/of.hlinux/platform_device.hlinux/pwm.hlinux/slab.hlinux/stmp_device.h
Detected Declarations
struct mxs_pwm_chipfunction mxs_pwm_applyfunction clk_get_ratefunction mxs_pwm_probe
Annotated Snippet
struct mxs_pwm_chip {
struct clk *clk;
void __iomem *base;
};
static inline struct mxs_pwm_chip *to_mxs_pwm_chip(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
int ret, div = 0;
unsigned int period_cycles, duty_cycles;
unsigned long rate;
unsigned long long c;
unsigned int pol_bits;
/*
* If the PWM channel is disabled, make sure to turn on the
* clock before calling clk_get_rate() and writing to the
* registers. Otherwise, just keep it enabled.
*/
if (!pwm_is_enabled(pwm)) {
ret = clk_prepare_enable(mxs->clk);
if (ret)
return ret;
}
if (!state->enabled && pwm_is_enabled(pwm))
writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
rate = clk_get_rate(mxs->clk);
while (1) {
c = rate >> cdiv_shift[div];
c = c * state->period;
do_div(c, 1000000000);
if (c < PERIOD_PERIOD_MAX)
break;
div++;
if (div >= PERIOD_CDIV_MAX)
return -EINVAL;
}
period_cycles = c;
c *= state->duty_cycle;
do_div(c, state->period);
duty_cycles = c;
/*
* The data sheet the says registers must be written to in
* this order (ACTIVEn, then PERIODn). Also, the new settings
* only take effect at the beginning of a new period, avoiding
* glitches.
*/
pol_bits = state->polarity == PWM_POLARITY_NORMAL ?
PERIOD_POLARITY_NORMAL : PERIOD_POLARITY_INVERSE;
writel(duty_cycles << 16,
mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
if (state->enabled) {
if (!pwm_is_enabled(pwm)) {
/*
* The clock was enabled above. Just enable
* the channel in the control register.
*/
writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
}
} else {
clk_disable_unprepare(mxs->clk);
}
return 0;
}
static const struct pwm_ops mxs_pwm_ops = {
.apply = mxs_pwm_apply,
};
static int mxs_pwm_probe(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
struct pwm_chip *chip;
struct mxs_pwm_chip *mxs;
u32 npwm;
int ret;
Annotation
- Immediate include surface: `linux/clk.h`, `linux/err.h`, `linux/io.h`, `linux/kernel.h`, `linux/module.h`, `linux/of.h`, `linux/platform_device.h`, `linux/pwm.h`.
- Detected declarations: `struct mxs_pwm_chip`, `function mxs_pwm_apply`, `function clk_get_rate`, `function mxs_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.