drivers/leds/leds-regulator.c
Source file repositories/reference/linux-study-clean/drivers/leds/leds-regulator.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/leds/leds-regulator.c- Extension
.c- Size
- 4602 bytes
- Lines
- 205
- 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/module.hlinux/mod_devicetable.hlinux/err.hlinux/slab.hlinux/leds.hlinux/leds-regulator.hlinux/platform_device.hlinux/regulator/consumer.h
Detected Declarations
struct regulator_ledfunction led_regulator_get_max_brightnessfunction led_regulator_get_voltagefunction regulator_led_enablefunction regulator_led_disablefunction regulator_led_brightness_setfunction regulator_led_probefunction regulator_led_remove
Annotated Snippet
struct regulator_led {
struct led_classdev cdev;
int enabled;
struct mutex mutex;
struct regulator *vcc;
};
static inline int led_regulator_get_max_brightness(struct regulator *supply)
{
int ret;
int voltage = regulator_list_voltage(supply, 0);
if (voltage <= 0)
return 1;
/* even if regulator can't change voltages,
* we still assume it can change status
* and the LED can be turned on and off.
*/
ret = regulator_set_voltage(supply, voltage, voltage);
if (ret < 0)
return 1;
return regulator_count_voltages(supply);
}
static int led_regulator_get_voltage(struct regulator *supply,
enum led_brightness brightness)
{
if (brightness == 0)
return -EINVAL;
return regulator_list_voltage(supply, brightness - 1);
}
static void regulator_led_enable(struct regulator_led *led)
{
int ret;
if (led->enabled)
return;
ret = regulator_enable(led->vcc);
if (ret != 0) {
dev_err(led->cdev.dev, "Failed to enable vcc: %d\n", ret);
return;
}
led->enabled = 1;
}
static void regulator_led_disable(struct regulator_led *led)
{
int ret;
if (!led->enabled)
return;
ret = regulator_disable(led->vcc);
if (ret != 0) {
dev_err(led->cdev.dev, "Failed to disable vcc: %d\n", ret);
return;
}
led->enabled = 0;
}
static int regulator_led_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct regulator_led *led = to_regulator_led(led_cdev);
int voltage;
int ret = 0;
mutex_lock(&led->mutex);
if (value == LED_OFF) {
regulator_led_disable(led);
goto out;
}
if (led->cdev.max_brightness > 1) {
voltage = led_regulator_get_voltage(led->vcc, value);
dev_dbg(led->cdev.dev, "brightness: %d voltage: %d\n",
value, voltage);
ret = regulator_set_voltage(led->vcc, voltage, voltage);
if (ret != 0)
Annotation
- Immediate include surface: `linux/module.h`, `linux/mod_devicetable.h`, `linux/err.h`, `linux/slab.h`, `linux/leds.h`, `linux/leds-regulator.h`, `linux/platform_device.h`, `linux/regulator/consumer.h`.
- Detected declarations: `struct regulator_led`, `function led_regulator_get_max_brightness`, `function led_regulator_get_voltage`, `function regulator_led_enable`, `function regulator_led_disable`, `function regulator_led_brightness_set`, `function regulator_led_probe`, `function regulator_led_remove`.
- 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.