drivers/iio/light/si1145.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/light/si1145.c
Extension
.c
Size
35269 bytes
Lines
1361
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 si1145_part_info {
	u8 part;
	const struct iio_info *iio_info;
	const struct iio_chan_spec *channels;
	unsigned int num_channels;
	unsigned int num_leds;
	bool uncompressed_meas_rate;
};

/**
 * struct si1145_data - si1145 chip state data
 * @client:	I2C client
 * @lock:	mutex to protect shared state.
 * @cmdlock:	Low-level mutex to protect command execution only
 * @rsp_seq:	Next expected response number or -1 if counter reset required
 * @scan_mask:	Saved scan mask to avoid duplicate set_chlist
 * @autonomous: If automatic measurements are active (for buffer support)
 * @part_info:	Part information
 * @trig:	Pointer to iio trigger
 * @meas_rate:	Value of MEAS_RATE register. Only set in HW in auto mode
 * @buffer:	Used to pack data read from sensor.
 */
struct si1145_data {
	struct i2c_client *client;
	struct mutex lock;
	struct mutex cmdlock;
	int rsp_seq;
	const struct si1145_part_info *part_info;
	unsigned long scan_mask;
	bool autonomous;
	struct iio_trigger *trig;
	int meas_rate;
	/*
	 * Ensure timestamp will be naturally aligned if present.
	 * Maximum buffer size (may be only partly used if not all
	 * channels are enabled):
	 *   6*2 bytes channels data + 4 bytes alignment +
	 *   8 bytes timestamp
	 */
	u8 buffer[24] __aligned(8);
};

/*
 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
 *
 * Does not modify data->rsp_seq
 *
 * Return: 0 on success and -errno on error.
 */
static int __si1145_command_reset(struct si1145_data *data)
{
	struct device *dev = &data->client->dev;
	unsigned long stop_jiffies;
	int ret;

	ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
						      SI1145_CMD_NOP);
	if (ret < 0)
		return ret;
	msleep(SI1145_COMMAND_MINSLEEP_MS);

	stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
	while (true) {
		ret = i2c_smbus_read_byte_data(data->client,
					       SI1145_REG_RESPONSE);
		if (ret <= 0)
			return ret;
		if (time_after(jiffies, stop_jiffies)) {
			dev_warn(dev, "timeout on reset\n");
			return -ETIMEDOUT;
		}
		msleep(SI1145_COMMAND_MINSLEEP_MS);
	}
}

/*
 * si1145_command() - Execute a command and poll the response register
 *
 * All conversion overflows are reported as -EOVERFLOW
 * INVALID_SETTING is reported as -EINVAL
 * Timeouts are reported as -ETIMEDOUT
 *
 * Return: 0 on success or -errno on failure
 */
static int si1145_command(struct si1145_data *data, u8 cmd)
{
	struct device *dev = &data->client->dev;
	unsigned long stop_jiffies;
	int ret;

Annotation

Implementation Notes