drivers/pwm/core.c
Source file repositories/reference/linux-study-clean/drivers/pwm/core.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/pwm/core.c- Extension
.c- Size
- 68370 bytes
- Lines
- 2760
- Domain
- Driver Families
- Bucket
- drivers/pwm
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern 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 an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/acpi.hlinux/module.hlinux/idr.hlinux/of.hlinux/pwm.hlinux/list.hlinux/mutex.hlinux/err.hlinux/slab.hlinux/device.hlinux/debugfs.hlinux/seq_file.hdt-bindings/pwm/pwm.huapi/linux/pwm.htrace/events/pwm.h
Detected Declarations
struct pwm_exportstruct pwm_cdev_datafunction pwmchip_lockfunction pwmchip_unlockfunction pwm_wf_validfunction pwm_wf2statefunction pwm_state2wffunction pwmwfcmpfunction pwm_check_roundingfunction __pwm_round_waveform_tohwfunction __pwm_round_waveform_fromhwfunction __pwm_read_waveformfunction __pwm_write_waveformfunction pwm_round_waveform_might_sleepfunction pwm_get_state_hwfunction __pwm_set_waveformfunction pwm_set_waveform_might_sleepfunction pwm_apply_debugfunction pwm_state_validfunction __pwm_applyfunction pwm_apply_might_sleepfunction pwm_apply_atomicfunction pwm_get_state_hwfunction pwm_adjust_configfunction pwm_capturefunction idr_for_each_entry_ulfunction pwm_device_requestfunction pwm_request_from_chipfunction of_pwm_xlate_with_flagsfunction numberfunction period_showfunction period_storefunction duty_cycle_showfunction duty_cycle_storefunction enable_showfunction enable_storefunction polarity_showfunction polarity_storefunction capture_showfunction pwm_export_releasefunction pwm_export_childfunction pwm_unexport_matchfunction pwm_unexport_childfunction export_storefunction unexport_storefunction npwm_showfunction pwm_class_apply_statefunction pwm_class_resume_npwm
Annotated Snippet
static const struct file_operations pwm_cdev_fileops = {
.open = pwm_cdev_open,
.release = pwm_cdev_release,
.owner = THIS_MODULE,
.unlocked_ioctl = pwm_cdev_ioctl,
};
static dev_t pwm_devt;
static int pwm_gpio_request(struct gpio_chip *gc, unsigned int offset)
{
struct pwm_chip *chip = gpiochip_get_data(gc);
struct pwm_device *pwm;
pwm = pwm_request_from_chip(chip, offset, "pwm-gpio");
if (IS_ERR(pwm))
return PTR_ERR(pwm);
return 0;
}
static void pwm_gpio_free(struct gpio_chip *gc, unsigned int offset)
{
struct pwm_chip *chip = gpiochip_get_data(gc);
pwm_put(&chip->pwms[offset]);
}
static int pwm_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
{
return GPIO_LINE_DIRECTION_OUT;
}
static int pwm_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
{
struct pwm_chip *chip = gpiochip_get_data(gc);
struct pwm_device *pwm = &chip->pwms[offset];
int ret;
struct pwm_waveform wf = {
.period_length_ns = 1,
};
ret = pwm_round_waveform_might_sleep(pwm, &wf);
if (ret < 0)
return ret;
if (value)
wf.duty_length_ns = wf.period_length_ns;
else
wf.duty_length_ns = 0;
return pwm_set_waveform_might_sleep(pwm, &wf, true);
}
/**
* __pwmchip_add() - register a new PWM chip
* @chip: the PWM chip to add
* @owner: reference to the module providing the chip.
*
* Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
* pwmchip_add wrapper to do this right.
*
* Returns: 0 on success or a negative error code on failure.
*/
int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
{
int ret;
if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
return -EINVAL;
/*
* a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
* otherwise the embedded struct device might disappear too early
* resulting in memory corruption.
* Catch drivers that were not converted appropriately.
*/
if (!chip->uses_pwmchip_alloc)
return -EINVAL;
if (!pwm_ops_check(chip))
return -EINVAL;
chip->owner = owner;
if (chip->atomic)
spin_lock_init(&chip->atomic_lock);
else
mutex_init(&chip->nonatomic_lock);
Annotation
- Immediate include surface: `linux/acpi.h`, `linux/module.h`, `linux/idr.h`, `linux/of.h`, `linux/pwm.h`, `linux/list.h`, `linux/mutex.h`, `linux/err.h`.
- Detected declarations: `struct pwm_export`, `struct pwm_cdev_data`, `function pwmchip_lock`, `function pwmchip_unlock`, `function pwm_wf_valid`, `function pwm_wf2state`, `function pwm_state2wf`, `function pwmwfcmp`, `function pwm_check_rounding`, `function __pwm_round_waveform_tohw`.
- Atlas domain: Driver Families / drivers/pwm.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.