drivers/pwm/pwm-pca9685.c
Source file repositories/reference/linux-study-clean/drivers/pwm/pwm-pca9685.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/pwm/pwm-pca9685.c- Extension
.c- Size
- 15691 bytes
- Lines
- 580
- 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/gpio/driver.hlinux/i2c.hlinux/module.hlinux/mutex.hlinux/platform_device.hlinux/property.hlinux/pwm.hlinux/regmap.hlinux/slab.hlinux/delay.hlinux/pm_runtime.hlinux/bitmap.h
Detected Declarations
struct pca9685struct pca9685_waveformfunction pca9685_prescaler_can_changefunction pca9685_read_regfunction pca9685_write_regfunction pca9685_write_4regfunction pca9685_set_sleep_modefunction pca9685_round_waveform_tohwfunction pca9685_round_waveform_fromhwfunction scoped_guardfunction pca9685_read_waveformfunction pca9685_write_waveformfunction pca9685_pwm_requestfunction pca9685_pwm_freefunction pca9685_readable_regfunction pca9685_writeable_regfunction pca9685_volatile_regfunction pca9685_pwm_probefunction pca9685_pwm_removefunction pca9685_pwm_runtime_suspendfunction pca9685_pwm_runtime_resume
Annotated Snippet
struct pca9685 {
struct regmap *regmap;
struct mutex lock;
DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1);
};
static inline struct pca9685 *to_pca(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
/* This function is supposed to be called with the lock mutex held */
static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel)
{
/* No PWM enabled: Change allowed */
if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1))
return true;
/* More than one PWM enabled: Change not allowed */
if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1)
return false;
/*
* Only one PWM enabled: Change allowed if the PWM about to
* be changed is the one that is already enabled
*/
return test_bit(channel, pca->pwms_enabled);
}
static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val)
{
struct pca9685 *pca = to_pca(chip);
struct device *dev = pwmchip_parent(chip);
int err;
err = regmap_read(pca->regmap, reg, val);
if (err)
dev_err(dev, "regmap_read of register 0x%x failed: %pe\n", reg, ERR_PTR(err));
return err;
}
static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val)
{
struct pca9685 *pca = to_pca(chip);
struct device *dev = pwmchip_parent(chip);
int err;
err = regmap_write(pca->regmap, reg, val);
if (err)
dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err));
return err;
}
static int pca9685_write_4reg(struct pwm_chip *chip, unsigned int reg, u8 val[4])
{
struct pca9685 *pca = to_pca(chip);
struct device *dev = pwmchip_parent(chip);
int err;
err = regmap_bulk_write(pca->regmap, reg, val, 4);
if (err)
dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err));
return err;
}
static int pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable)
{
struct pca9685 *pca = to_pca(chip);
int err;
err = regmap_update_bits(pca->regmap, PCA9685_MODE1,
MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
if (err)
return err;
if (!enable) {
/* Wait 500us for the oscillator to be back up */
udelay(500);
}
return 0;
}
struct pca9685_waveform {
u8 onoff[4];
u8 prescale;
};
static int pca9685_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_waveform *wf, void *_wfhw)
Annotation
- Immediate include surface: `linux/gpio/driver.h`, `linux/i2c.h`, `linux/module.h`, `linux/mutex.h`, `linux/platform_device.h`, `linux/property.h`, `linux/pwm.h`, `linux/regmap.h`.
- Detected declarations: `struct pca9685`, `struct pca9685_waveform`, `function pca9685_prescaler_can_change`, `function pca9685_read_reg`, `function pca9685_write_reg`, `function pca9685_write_4reg`, `function pca9685_set_sleep_mode`, `function pca9685_round_waveform_tohw`, `function pca9685_round_waveform_fromhw`, `function scoped_guard`.
- 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.