drivers/leds/leds-lm3692x.c

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

File Facts

System
Linux kernel
Corpus path
drivers/leds/leds-lm3692x.c
Extension
.c
Size
13380 bytes
Lines
533
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 lm3692x_led {
	struct mutex lock;
	struct i2c_client *client;
	struct led_classdev led_dev;
	struct regmap *regmap;
	struct gpio_desc *enable_gpio;
	struct regulator *regulator;
	int led_enable;
	int model_id;

	u8 boost_ctrl, brightness_ctrl;
	bool enabled;
};

static const struct reg_default lm3692x_reg_defs[] = {
	{LM3692X_EN, 0xf},
	{LM3692X_BRT_CTRL, 0x61},
	{LM3692X_PWM_CTRL, 0x73},
	{LM3692X_BOOST_CTRL, 0x6f},
	{LM3692X_AUTO_FREQ_HI, 0x0},
	{LM3692X_AUTO_FREQ_LO, 0x0},
	{LM3692X_BL_ADJ_THRESH, 0x0},
	{LM3692X_BRT_LSB, 0x7},
	{LM3692X_BRT_MSB, 0xff},
	{LM3692X_FAULT_CTRL, 0x7},
};

static const struct regmap_config lm3692x_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,

	.max_register = LM3692X_FAULT_FLAGS,
	.reg_defaults = lm3692x_reg_defs,
	.num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs),
	.cache_type = REGCACHE_MAPLE,
};

static int lm3692x_fault_check(struct lm3692x_led *led)
{
	int ret;
	unsigned int read_buf;

	ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
	if (ret)
		return ret;

	if (read_buf)
		dev_err(&led->client->dev, "Detected a fault 0x%X\n", read_buf);

	/* The first read may clear the fault.  Check again to see if the fault
	 * still exits and return that value.
	 */
	regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
	if (read_buf)
		dev_err(&led->client->dev, "Second read of fault flags 0x%X\n",
			read_buf);

	return read_buf;
}

static int lm3692x_leds_enable(struct lm3692x_led *led)
{
	int enable_state;
	int ret, reg_ret;

	if (led->enabled)
		return 0;

	if (led->regulator) {
		ret = regulator_enable(led->regulator);
		if (ret) {
			dev_err(&led->client->dev,
				"Failed to enable regulator: %d\n", ret);
			return ret;
		}
	}

	if (led->enable_gpio)
		gpiod_direction_output(led->enable_gpio, 1);

	ret = lm3692x_fault_check(led);
	if (ret) {
		dev_err(&led->client->dev, "Cannot read/clear faults: %d\n",
			ret);
		goto out;
	}

	ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00);
	if (ret)
		goto out;

Annotation

Implementation Notes