drivers/regulator/sgm3804-regulator.c
Source file repositories/reference/linux-study-clean/drivers/regulator/sgm3804-regulator.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/regulator/sgm3804-regulator.c- Extension
.c- Size
- 8286 bytes
- Lines
- 315
- Domain
- Driver Families
- Bucket
- drivers/regulator
- 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.
- 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/i2c.hlinux/module.hlinux/regmap.hlinux/regulator/driver.hlinux/regulator/of_regulator.hlinux/gpio/consumer.h
Detected Declarations
struct sgm3804_datafunction sgm3804_writeable_regfunction sgm3804_readable_regfunction sgm3804_sync_regcache_statefunction sgm3804_get_voltage_selfunction sgm3804_enablefunction sgm3804_disablefunction sgm3804_is_enabledfunction sgm3804_probe
Annotated Snippet
struct sgm3804_data {
struct regmap *regmap;
/* Protects the regcache state update */
struct mutex lock;
struct gpio_desc *gpios[SGM3804_RAIL_COUNT];
};
static const struct linear_range sgm3804_voltages[] = {
REGULATOR_LINEAR_RANGE(2400000, 0x20, 0x2f, 100000),
REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x17, 100000),
};
/*
* The cache is populated with those hardware default values
* so the regmap_update_bits operation will use the cached
* value to build a new register value and write it when GPIOs
* are enabled.
*/
static const struct reg_default sgm3804_reg_defaults[] = {
{ SGM3804_POS_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
{ SGM3804_NEG_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
{ SGM3804_RAIL_DISCHARGE_REG, RAIL_DISCHARGE_REG_DEFAULT },
};
/* Registers are only writable */
static bool sgm3804_writeable_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
case SGM3804_POS_RAIL_VOLTAGE_REG:
case SGM3804_NEG_RAIL_VOLTAGE_REG:
case SGM3804_RAIL_DISCHARGE_REG:
return true;
default:
return false;
}
}
/*
* Since all registers are only writeable, regmap will only read from the cache data.
*/
static bool sgm3804_readable_reg(struct device *dev, unsigned int reg)
{
return false;
}
static const struct regmap_config sgm3804_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = 0x03,
.writeable_reg = sgm3804_writeable_reg,
.readable_reg = sgm3804_readable_reg,
.cache_type = REGCACHE_MAPLE,
.reg_defaults = sgm3804_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(sgm3804_reg_defaults),
};
static int sgm3804_sync_regcache_state(struct sgm3804_data *ctx)
{
guard(mutex)(&ctx->lock);
/* If both GPIOs are down, IC is powered down and I2C writes will fail */
if (!gpiod_get_value_cansleep(ctx->gpios[SGM3804_POS_RAIL]) &&
!gpiod_get_value_cansleep(ctx->gpios[SGM3804_NEG_RAIL])) {
regcache_cache_only(ctx->regmap, true);
regcache_mark_dirty(ctx->regmap);
} else {
int ret;
/* At least a GPIO is up, we can write registers */
regcache_cache_only(ctx->regmap, false);
ret = regcache_sync(ctx->regmap);
if (ret) {
regcache_cache_only(ctx->regmap, true);
return ret;
}
}
return 0;
}
static int sgm3804_get_voltage_sel(struct regulator_dev *rdev)
{
int ret;
ret = regulator_get_voltage_sel_regmap(rdev);
if (ret < 0)
return ret;
/* Force setting a voltage on probe */
if (ret == RAIL_VOLTAGE_INVALID)
Annotation
- Immediate include surface: `linux/err.h`, `linux/i2c.h`, `linux/module.h`, `linux/regmap.h`, `linux/regulator/driver.h`, `linux/regulator/of_regulator.h`, `linux/gpio/consumer.h`.
- Detected declarations: `struct sgm3804_data`, `function sgm3804_writeable_reg`, `function sgm3804_readable_reg`, `function sgm3804_sync_regcache_state`, `function sgm3804_get_voltage_sel`, `function sgm3804_enable`, `function sgm3804_disable`, `function sgm3804_is_enabled`, `function sgm3804_probe`.
- Atlas domain: Driver Families / drivers/regulator.
- Implementation status: source implementation candidate.
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.