drivers/leds/rgb/leds-ktd202x.c

Source file repositories/reference/linux-study-clean/drivers/leds/rgb/leds-ktd202x.c

File Facts

System
Linux kernel
Corpus path
drivers/leds/rgb/leds-ktd202x.c
Extension
.c
Size
16521 bytes
Lines
636
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct ktd202x_led {
	struct ktd202x *chip;
	union {
		struct led_classdev cdev;
		struct led_classdev_mc mcdev;
	};
	u32 index;
};

struct ktd202x {
	struct mutex mutex;
	struct regulator_bulk_data regulators[2];
	struct device *dev;
	struct regmap *regmap;
	bool enabled;
	unsigned long num_leds;
	struct ktd202x_led leds[] __counted_by(num_leds);
};

static int ktd202x_chip_disable(struct ktd202x *chip)
{
	int ret;

	if (!chip->enabled)
		return 0;

	regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_ENABLE_CTRL_SLEEP);

	ret = regulator_bulk_disable(ARRAY_SIZE(chip->regulators), chip->regulators);
	if (ret) {
		dev_err(chip->dev, "Failed to disable regulators: %d\n", ret);
		return ret;
	}

	chip->enabled = false;
	return 0;
}

static int ktd202x_chip_enable(struct ktd202x *chip)
{
	int ret;

	if (chip->enabled)
		return 0;

	ret = regulator_bulk_enable(ARRAY_SIZE(chip->regulators), chip->regulators);
	if (ret) {
		dev_err(chip->dev, "Failed to enable regulators: %d\n", ret);
		return ret;
	}
	chip->enabled = true;

	ret = regmap_write(chip->regmap, KTD202X_REG_RESET_CONTROL, KTD202X_ENABLE_CTRL_WAKE);

	if (ret) {
		dev_err(chip->dev, "Failed to enable the chip: %d\n", ret);
		ktd202x_chip_disable(chip);
	}

	return ret;
}

static bool ktd202x_chip_in_use(struct ktd202x *chip)
{
	int i;

	for (i = 0; i < chip->num_leds; i++) {
		if (chip->leds[i].cdev.brightness)
			return true;
	}

	return false;
}

static int ktd202x_brightness_set(struct ktd202x_led *led,
				  struct mc_subled *subleds,
				  unsigned int num_channels)
{
	bool mode_blink = false;
	int channel;
	int state;
	int ret;
	int i;

	if (ktd202x_chip_in_use(led->chip)) {
		ret = ktd202x_chip_enable(led->chip);
		if (ret)
			return ret;
	}

Annotation

Implementation Notes