drivers/iio/pressure/hp206c.c

Source file repositories/reference/linux-study-clean/drivers/iio/pressure/hp206c.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/pressure/hp206c.c
Extension
.c
Size
10155 bytes
Lines
423
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 hp206c_data {
	struct mutex mutex;
	struct i2c_client *client;
	int temp_osr_index;
	int pres_osr_index;
};

struct hp206c_osr_setting {
	u8 osr_mask;
	unsigned int temp_conv_time_us;
	unsigned int pres_conv_time_us;
};

/* Data from Table 5 in datasheet. */
static const struct hp206c_osr_setting hp206c_osr_settings[] = {
	{ HP206C_CMD_ADC_CVT_OSR_4096,	65600,	131100	},
	{ HP206C_CMD_ADC_CVT_OSR_2048,	32800,	65600	},
	{ HP206C_CMD_ADC_CVT_OSR_1024,	16400,	32800	},
	{ HP206C_CMD_ADC_CVT_OSR_512,	8200,	16400	},
	{ HP206C_CMD_ADC_CVT_OSR_256,	4100,	8200	},
	{ HP206C_CMD_ADC_CVT_OSR_128,	2100,	4100	},
};
static const int hp206c_osr_rates[] = { 4096, 2048, 1024, 512, 256, 128 };
static const char hp206c_osr_rates_str[] = "4096 2048 1024 512 256 128";

static inline int hp206c_read_reg(struct i2c_client *client, u8 reg)
{
	return i2c_smbus_read_byte_data(client, HP206C_CMD_READ_REG | reg);
}

static inline int hp206c_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
	return i2c_smbus_write_byte_data(client,
			HP206C_CMD_WRITE_REG | reg, val);
}

static int hp206c_read_20bit(struct i2c_client *client, u8 cmd)
{
	int ret;
	u8 values[3];

	ret = i2c_smbus_read_i2c_block_data(client, cmd, sizeof(values), values);
	if (ret < 0)
		return ret;
	if (ret != sizeof(values))
		return -EIO;
	return get_unaligned_be24(&values[0]) & GENMASK(19, 0);
}

/* Spin for max 160ms until DEV_RDY is 1, or return error. */
static int hp206c_wait_dev_rdy(struct iio_dev *indio_dev)
{
	int ret;
	int count = 0;
	struct hp206c_data *data = iio_priv(indio_dev);
	struct i2c_client *client = data->client;

	while (++count <= HP206C_MAX_DEV_RDY_WAIT_COUNT) {
		ret = hp206c_read_reg(client, HP206C_REG_INT_SRC);
		if (ret < 0) {
			dev_err(&indio_dev->dev, "Failed READ_REG INT_SRC: %d\n", ret);
			return ret;
		}
		if (ret & HP206C_FLAG_DEV_RDY)
			return 0;
		usleep_range(HP206C_DEV_RDY_WAIT_US, HP206C_DEV_RDY_WAIT_US * 3 / 2);
	}
	return -ETIMEDOUT;
}

static int hp206c_set_compensation(struct i2c_client *client, bool enabled)
{
	int val;

	val = hp206c_read_reg(client, HP206C_REG_PARA);
	if (val < 0)
		return val;
	if (enabled)
		val |= HP206C_FLAG_CMPS_EN;
	else
		val &= ~HP206C_FLAG_CMPS_EN;

	return hp206c_write_reg(client, HP206C_REG_PARA, val);
}

/* Do a soft reset */
static int hp206c_soft_reset(struct iio_dev *indio_dev)
{
	int ret;
	struct hp206c_data *data = iio_priv(indio_dev);

Annotation

Implementation Notes