drivers/pwm/pwm-twl.c
Source file repositories/reference/linux-study-clean/drivers/pwm/pwm-twl.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/pwm/pwm-twl.c- Extension
.c- Size
- 9620 bytes
- Lines
- 383
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/of.hlinux/platform_device.hlinux/pwm.hlinux/mfd/twl.hlinux/slab.h
Detected Declarations
struct twl_pwm_chipfunction twl_pwm_configfunction twl4030_pwm_enablefunction twl4030_pwm_disablefunction twl4030_pwm_requestfunction twl4030_pwm_freefunction twl6030_pwm_enablefunction twl6030_pwm_disablefunction twl4030_pwm_applyfunction twl6030_pwm_applyfunction twl_pwm_probe
Annotated Snippet
struct twl_pwm_chip {
struct mutex mutex;
u8 twl6030_toggle3;
u8 twl4030_pwm_mux;
};
static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
u64 duty_ns, u64 period_ns)
{
int duty_cycle = DIV64_U64_ROUND_UP(duty_ns * TWL_PWM_MAX, period_ns) + 1;
u8 pwm_config[2] = { 1, 0 };
int base, ret;
/*
* To configure the duty period:
* On-cycle is set to 1 (the minimum allowed value)
* The off time of 0 is not configurable, so the mapping is:
* 0 -> off cycle = 2,
* 1 -> off cycle = 2,
* 2 -> off cycle = 3,
* 126 - > off cycle 127,
* 127 - > off cycle 1
* When on cycle == off cycle the PWM will be always on
*/
if (duty_cycle == 1)
duty_cycle = 2;
else if (duty_cycle > TWL_PWM_MAX)
duty_cycle = 1;
base = pwm->hwpwm * 3;
pwm_config[1] = duty_cycle;
ret = twl_i2c_write(TWL_MODULE_PWM, pwm_config, base, 2);
if (ret < 0)
dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label);
return ret;
}
static int twl4030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
struct twl_pwm_chip *twl = to_twl(chip);
int ret;
u8 val;
mutex_lock(&twl->mutex);
ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
if (ret < 0) {
dev_err(pwmchip_parent(chip), "%s: Failed to read GPBR1\n", pwm->label);
goto out;
}
val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
if (ret < 0)
dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
if (ret < 0)
dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label);
out:
mutex_unlock(&twl->mutex);
return ret;
}
static void twl4030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
{
struct twl_pwm_chip *twl = to_twl(chip);
int ret;
u8 val;
mutex_lock(&twl->mutex);
ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
if (ret < 0) {
dev_err(pwmchip_parent(chip), "%s: Failed to read GPBR1\n", pwm->label);
goto out;
}
val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
Annotation
- Immediate include surface: `linux/module.h`, `linux/of.h`, `linux/platform_device.h`, `linux/pwm.h`, `linux/mfd/twl.h`, `linux/slab.h`.
- Detected declarations: `struct twl_pwm_chip`, `function twl_pwm_config`, `function twl4030_pwm_enable`, `function twl4030_pwm_disable`, `function twl4030_pwm_request`, `function twl4030_pwm_free`, `function twl6030_pwm_enable`, `function twl6030_pwm_disable`, `function twl4030_pwm_apply`, `function twl6030_pwm_apply`.
- Atlas domain: Driver Families / drivers/pwm.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.