drivers/leds/rgb/leds-pwm-multicolor.c
Source file repositories/reference/linux-study-clean/drivers/leds/rgb/leds-pwm-multicolor.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/leds/rgb/leds-pwm-multicolor.c- Extension
.c- Size
- 5129 bytes
- Lines
- 199
- Domain
- Driver Families
- Bucket
- drivers/leds
- 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.
- 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/err.hlinux/kernel.hlinux/led-class-multicolor.hlinux/leds.hlinux/mod_devicetable.hlinux/module.hlinux/mutex.hlinux/platform_device.hlinux/property.hlinux/pwm.h
Detected Declarations
struct pwm_ledstruct pwm_mc_ledfunction led_pwm_mc_setfunction iterate_subledsfunction led_pwm_mc_probe
Annotated Snippet
struct pwm_led {
struct pwm_device *pwm;
struct pwm_state state;
bool active_low;
};
struct pwm_mc_led {
struct led_classdev_mc mc_cdev;
struct mutex lock;
struct pwm_led leds[];
};
static int led_pwm_mc_set(struct led_classdev *cdev,
enum led_brightness brightness)
{
struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
struct pwm_mc_led *priv = container_of(mc_cdev, struct pwm_mc_led, mc_cdev);
unsigned long long duty;
int ret = 0;
int i;
led_mc_calc_color_components(mc_cdev, brightness);
mutex_lock(&priv->lock);
for (i = 0; i < mc_cdev->num_colors; i++) {
duty = priv->leds[i].state.period;
duty *= mc_cdev->subled_info[i].brightness;
do_div(duty, cdev->max_brightness);
if (priv->leds[i].active_low)
duty = priv->leds[i].state.period - duty;
priv->leds[i].state.duty_cycle = duty;
/*
* Disabling a PWM doesn't guarantee that it emits the inactive level.
* So keep it on. Only for suspending the PWM should be disabled because
* otherwise it refuses to suspend. The possible downside is that the
* LED might stay (or even go) on.
*/
priv->leds[i].state.enabled = !(cdev->flags & LED_SUSPENDED);
ret = pwm_apply_might_sleep(priv->leds[i].pwm,
&priv->leds[i].state);
if (ret)
break;
}
mutex_unlock(&priv->lock);
return ret;
}
static int iterate_subleds(struct device *dev, struct pwm_mc_led *priv,
struct fwnode_handle *mcnode)
{
struct mc_subled *subled = priv->mc_cdev.subled_info;
struct fwnode_handle *fwnode;
struct pwm_led *pwmled;
u32 color;
int ret;
/* iterate over the nodes inside the multi-led node */
fwnode_for_each_child_node(mcnode, fwnode) {
pwmled = &priv->leds[priv->mc_cdev.num_colors];
pwmled->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
if (IS_ERR(pwmled->pwm)) {
ret = dev_err_probe(dev, PTR_ERR(pwmled->pwm), "unable to request PWM\n");
goto release_fwnode;
}
pwm_init_state(pwmled->pwm, &pwmled->state);
pwmled->active_low = fwnode_property_read_bool(fwnode, "active-low");
ret = fwnode_property_read_u32(fwnode, "color", &color);
if (ret) {
dev_err(dev, "cannot read color: %d\n", ret);
goto release_fwnode;
}
subled[priv->mc_cdev.num_colors].color_index = color;
priv->mc_cdev.num_colors++;
}
return 0;
release_fwnode:
fwnode_handle_put(fwnode);
return ret;
}
static int led_pwm_mc_probe(struct platform_device *pdev)
Annotation
- Immediate include surface: `linux/err.h`, `linux/kernel.h`, `linux/led-class-multicolor.h`, `linux/leds.h`, `linux/mod_devicetable.h`, `linux/module.h`, `linux/mutex.h`, `linux/platform_device.h`.
- Detected declarations: `struct pwm_led`, `struct pwm_mc_led`, `function led_pwm_mc_set`, `function iterate_subleds`, `function led_pwm_mc_probe`.
- Atlas domain: Driver Families / drivers/leds.
- 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.