drivers/iio/light/ltr390.c

Source file repositories/reference/linux-study-clean/drivers/iio/light/ltr390.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/light/ltr390.c
Extension
.c
Size
23411 bytes
Lines
917
Domain
Driver Families
Bucket
drivers/iio
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 ltr390_data {
	struct regmap *regmap;
	struct i2c_client *client;
	/* Protects device from simultaneous reads */
	struct mutex lock;
	enum ltr390_mode mode;
	int gain;
	int int_time_us;
	bool irq_enabled;
};

static const struct regmap_range ltr390_readable_reg_ranges[] = {
	regmap_reg_range(LTR390_MAIN_CTRL, LTR390_MAIN_CTRL),
	regmap_reg_range(LTR390_ALS_UVS_MEAS_RATE, LTR390_MAIN_STATUS),
	regmap_reg_range(LTR390_ALS_DATA_BYTE(0), LTR390_UVS_DATA_BYTE(2)),
	regmap_reg_range(LTR390_INT_CFG, LTR390_INT_PST),
	regmap_reg_range(LTR390_THRESH_UP_BYTE(0), LTR390_THRESH_LOW_BYTE(2)),
};

static const struct regmap_access_table ltr390_readable_reg_table = {
	.yes_ranges = ltr390_readable_reg_ranges,
	.n_yes_ranges = ARRAY_SIZE(ltr390_readable_reg_ranges),
};

static const struct regmap_range ltr390_writeable_reg_ranges[] = {
	regmap_reg_range(LTR390_MAIN_CTRL, LTR390_MAIN_CTRL),
	regmap_reg_range(LTR390_ALS_UVS_MEAS_RATE, LTR390_ALS_UVS_GAIN),
	regmap_reg_range(LTR390_INT_CFG, LTR390_INT_PST),
	regmap_reg_range(LTR390_THRESH_UP_BYTE(0), LTR390_THRESH_LOW_BYTE(2)),
};

static const struct regmap_access_table ltr390_writeable_reg_table = {
	.yes_ranges = ltr390_writeable_reg_ranges,
	.n_yes_ranges = ARRAY_SIZE(ltr390_writeable_reg_ranges),
};

static const struct regmap_config ltr390_regmap_config = {
	.name = "ltr390",
	.reg_bits = 8,
	.reg_stride = 1,
	.val_bits = 8,
	.max_register = LTR390_THRESH_LOW_BYTE(2),
	.rd_table = &ltr390_readable_reg_table,
	.wr_table = &ltr390_writeable_reg_table,
};

/* Sampling frequency is in mili Hz and mili Seconds */
static const int ltr390_samp_freq_table[][2] = {
		[0] = { 40000, 25 },
		[1] = { 20000, 50 },
		[2] = { 10000, 100 },
		[3] = { 5000, 200 },
		[4] = { 2000, 500 },
		[5] = { 1000, 1000 },
		[6] = { 500, 2000 },
		[7] = { 500, 2000 },
};

static int ltr390_register_read(struct ltr390_data *data, u8 register_address)
{
	struct device *dev = &data->client->dev;
	int ret;
	u8 receive_buffer[3];

	ret = regmap_bulk_read(data->regmap, register_address, receive_buffer,
			       sizeof(receive_buffer));
	if (ret) {
		dev_err(dev, "failed to read measurement data");
		return ret;
	}

	return get_unaligned_le24(receive_buffer);
}

static int ltr390_set_mode(struct ltr390_data *data, enum ltr390_mode mode)
{
	int ret;

	if (data->mode == mode)
		return 0;

	switch (mode) {
	case LTR390_SET_ALS_MODE:
		ret = regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_UVS_MODE);
		break;

	case LTR390_SET_UVS_MODE:
		ret = regmap_set_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_UVS_MODE);
		break;
	}

Annotation

Implementation Notes