drivers/iio/temperature/mcp9600.c

Source file repositories/reference/linux-study-clean/drivers/iio/temperature/mcp9600.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/temperature/mcp9600.c
Extension
.c
Size
16143 bytes
Lines
581
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 mcp_chip_info {
	u8 chip_id;
	const char *chip_name;
};

struct mcp9600_data {
	struct i2c_client *client;
	u32 thermocouple_type;
};

static int mcp9600_config(struct mcp9600_data *data)
{
	struct i2c_client *client = data->client;
	int ret;
	u8 cfg;

	cfg  = FIELD_PREP(MCP9600_SENSOR_TYPE_MASK,
			  mcp9600_type_map[data->thermocouple_type]);

	ret = i2c_smbus_write_byte_data(client, MCP9600_SENSOR_CFG, cfg);
	if (ret < 0) {
		dev_err(&client->dev, "Failed to set sensor configuration\n");
		return ret;
	}

	return 0;
}

#define MCP9600_CHANNELS(hj_num_ev, hj_ev_spec_off, cj_num_ev, cj_ev_spec_off) \
	{								       \
		{							       \
			.type = IIO_TEMP,				       \
			.address = MCP9600_HOT_JUNCTION,		       \
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	       \
					      BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE) | \
					      BIT(IIO_CHAN_INFO_SCALE),	       \
			.event_spec = &mcp9600_events[hj_ev_spec_off],	       \
			.num_event_specs = hj_num_ev,			       \
		},							       \
		{							       \
			.type = IIO_TEMP,				       \
			.address = MCP9600_COLD_JUNCTION,		       \
			.channel2 = IIO_MOD_TEMP_AMBIENT,		       \
			.modified = 1,					       \
			.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	       \
					      BIT(IIO_CHAN_INFO_SCALE),	       \
			.event_spec = &mcp9600_events[cj_ev_spec_off],	       \
			.num_event_specs = cj_num_ev,			       \
		},							       \
	}

static const struct iio_chan_spec mcp9600_channels[][2] = {
	MCP9600_CHANNELS(0, 0, 0, 0), /* Alerts: - - - - */
	MCP9600_CHANNELS(1, 0, 0, 0), /* Alerts: 1 - - - */
	MCP9600_CHANNELS(1, 1, 0, 0), /* Alerts: - 2 - - */
	MCP9600_CHANNELS(2, 0, 0, 0), /* Alerts: 1 2 - - */
	MCP9600_CHANNELS(0, 0, 1, 0), /* Alerts: - - 3 - */
	MCP9600_CHANNELS(1, 0, 1, 0), /* Alerts: 1 - 3 - */
	MCP9600_CHANNELS(1, 1, 1, 0), /* Alerts: - 2 3 - */
	MCP9600_CHANNELS(2, 0, 1, 0), /* Alerts: 1 2 3 - */
	MCP9600_CHANNELS(0, 0, 1, 1), /* Alerts: - - - 4 */
	MCP9600_CHANNELS(1, 0, 1, 1), /* Alerts: 1 - - 4 */
	MCP9600_CHANNELS(1, 1, 1, 1), /* Alerts: - 2 - 4 */
	MCP9600_CHANNELS(2, 0, 1, 1), /* Alerts: 1 2 - 4 */
	MCP9600_CHANNELS(0, 0, 2, 0), /* Alerts: - - 3 4 */
	MCP9600_CHANNELS(1, 0, 2, 0), /* Alerts: 1 - 3 4 */
	MCP9600_CHANNELS(1, 1, 2, 0), /* Alerts: - 2 3 4 */
	MCP9600_CHANNELS(2, 0, 2, 0), /* Alerts: 1 2 3 4 */
};

static int mcp9600_read(struct mcp9600_data *data,
			struct iio_chan_spec const *chan, int *val)
{
	int ret;

	ret = i2c_smbus_read_word_swapped(data->client, chan->address);

	if (ret < 0)
		return ret;

	*val = sign_extend32(ret, 15);

	return 0;
}

static int mcp9600_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan, int *val,
			    int *val2, long mask)
{
	struct mcp9600_data *data = iio_priv(indio_dev);

Annotation

Implementation Notes