drivers/iio/light/opt4001.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/light/opt4001.c
Extension
.c
Size
12305 bytes
Lines
467
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 opt4001_chip_info {
	int mul;
	int div;
	const char *name;
};

struct opt4001_chip {
	struct regmap *regmap;
	struct i2c_client *client;
	u8 int_time;
	const struct opt4001_chip_info *chip_info;
};

static const struct opt4001_chip_info opt4001_sot_5x3_info = {
	.mul = 4375,
	.div = 10000000,
	.name = "opt4001-sot-5x3"
};

static const struct opt4001_chip_info opt4001_picostar_info = {
	.mul = 3125,
	.div = 10000000,
	.name = "opt4001-picostar"
};

static const int opt4001_int_time_available[][2] = {
	{ 0,    600 },
	{ 0,   1000 },
	{ 0,   1800 },
	{ 0,   3400 },
	{ 0,   6500 },
	{ 0,  12700 },
	{ 0,  25000 },
	{ 0,  50000 },
	{ 0, 100000 },
	{ 0, 200000 },
	{ 0, 400000 },
	{ 0, 800000 },
};

/*
 * Conversion time is integration time + time to set register
 * this is used as integration time.
 */
static const int opt4001_int_time_reg[][2] = {
	{    600,  OPT4001_CTRL_CONVERSION_0_6MS  },
	{   1000,  OPT4001_CTRL_CONVERSION_1MS    },
	{   1800,  OPT4001_CTRL_CONVERSION_1_8MS  },
	{   3400,  OPT4001_CTRL_CONVERSION_3_4MS  },
	{   6500,  OPT4001_CTRL_CONVERSION_6_5MS  },
	{  12700,  OPT4001_CTRL_CONVERSION_12_7MS },
	{  25000,  OPT4001_CTRL_CONVERSION_25MS   },
	{  50000,  OPT4001_CTRL_CONVERSION_50MS   },
	{ 100000,  OPT4001_CTRL_CONVERSION_100MS  },
	{ 200000,  OPT4001_CTRL_CONVERSION_200MS  },
	{ 400000,  OPT4001_CTRL_CONVERSION_400MS  },
	{ 800000,  OPT4001_CTRL_CONVERSION_800MS  },
};

static int opt4001_als_time_to_index(const u32 als_integration_time)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available); i++) {
		if (als_integration_time == opt4001_int_time_available[i][1])
			return i;
	}

	return -EINVAL;
}

static u8 opt4001_calculate_crc(u8 exp, u32 mantissa, u8 count)
{
	u8 crc;

	crc = (hweight32(mantissa) + hweight32(exp) + hweight32(count)) % 2;
	crc |= ((hweight32(mantissa & 0xAAAAA) + hweight32(exp & 0xA)
		 + hweight32(count & 0xA)) % 2) << 1;
	crc |= ((hweight32(mantissa & 0x88888) + hweight32(exp & 0x8)
		 + hweight32(count & 0x8)) % 2) << 2;
	crc |= (hweight32(mantissa & 0x80808) % 2) << 3;

	return crc;
}

static int opt4001_read_lux_value(struct iio_dev *indio_dev,
				  int *val, int *val2)
{
	struct opt4001_chip *chip = iio_priv(indio_dev);
	struct device *dev = &chip->client->dev;

Annotation

Implementation Notes