drivers/i2c/busses/i2c-ljca.c

Source file repositories/reference/linux-study-clean/drivers/i2c/busses/i2c-ljca.c

File Facts

System
Linux kernel
Corpus path
drivers/i2c/busses/i2c-ljca.c
Extension
.c
Size
9166 bytes
Lines
344
Domain
Driver Families
Bucket
drivers/i2c
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 ljca_i2c_rw_packet {
	u8 id;
	__le16 len;
	u8 data[] __counted_by(len);
} __packed;

struct ljca_i2c_dev {
	struct ljca_client *ljca;
	struct ljca_i2c_info *i2c_info;
	struct i2c_adapter adap;

	u8 obuf[LJCA_I2C_BUF_SIZE];
	u8 ibuf[LJCA_I2C_BUF_SIZE];
};

static int ljca_i2c_init(struct ljca_i2c_dev *ljca_i2c, u8 id)
{
	struct ljca_i2c_rw_packet *w_packet =
			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
	int ret;

	w_packet->id = id;
	w_packet->len = cpu_to_le16(sizeof(*w_packet->data));
	w_packet->data[0] = LJCA_I2C_INIT_FLAG_FREQ_400K;

	ret = ljca_transfer(ljca_i2c->ljca, LJCA_I2C_INIT, (u8 *)w_packet,
			    struct_size(w_packet, data, 1), NULL, 0);

	return ret < 0 ? ret : 0;
}

static int ljca_i2c_start(struct ljca_i2c_dev *ljca_i2c, u8 target_addr,
			  enum ljca_xfer_type type)
{
	struct ljca_i2c_rw_packet *w_packet =
			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
	struct ljca_i2c_rw_packet *r_packet =
			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
	s16 rp_len;
	int ret;

	w_packet->id = ljca_i2c->i2c_info->id;
	w_packet->len = cpu_to_le16(sizeof(*w_packet->data));
	w_packet->data[0] = (target_addr << 1) | type;

	ret = ljca_transfer(ljca_i2c->ljca, LJCA_I2C_START, (u8 *)w_packet,
			    struct_size(w_packet, data, 1), (u8 *)r_packet,
			    LJCA_I2C_BUF_SIZE);
	if (ret < 0 || ret < sizeof(*r_packet))
		return ret < 0 ? ret : -EIO;

	rp_len = le16_to_cpu(r_packet->len);
	if (rp_len < 0 || r_packet->id != w_packet->id) {
		dev_dbg(&ljca_i2c->adap.dev,
			"i2c start failed len: %d id: %d %d\n",
			rp_len, r_packet->id, w_packet->id);
		return -EIO;
	}

	return 0;
}

static void ljca_i2c_stop(struct ljca_i2c_dev *ljca_i2c)
{
	struct ljca_i2c_rw_packet *w_packet =
			(struct ljca_i2c_rw_packet *)ljca_i2c->obuf;
	struct ljca_i2c_rw_packet *r_packet =
			(struct ljca_i2c_rw_packet *)ljca_i2c->ibuf;
	s16 rp_len;
	int ret;

	w_packet->id = ljca_i2c->i2c_info->id;
	w_packet->len = cpu_to_le16(sizeof(*w_packet->data));
	w_packet->data[0] = 0;

	ret = ljca_transfer(ljca_i2c->ljca, LJCA_I2C_STOP, (u8 *)w_packet,
			    struct_size(w_packet, data, 1), (u8 *)r_packet,
			    LJCA_I2C_BUF_SIZE);
	if (ret < 0 || ret < sizeof(*r_packet)) {
		dev_dbg(&ljca_i2c->adap.dev,
			"i2c stop failed ret: %d id: %d\n",
			ret, w_packet->id);
		return;
	}

	rp_len = le16_to_cpu(r_packet->len);
	if (rp_len < 0 || r_packet->id != w_packet->id)
		dev_dbg(&ljca_i2c->adap.dev,
			"i2c stop failed len: %d id: %d %d\n",
			rp_len, r_packet->id, w_packet->id);

Annotation

Implementation Notes