drivers/i2c/muxes/i2c-mux-mlxcpld.c

Source file repositories/reference/linux-study-clean/drivers/i2c/muxes/i2c-mux-mlxcpld.c

File Facts

System
Linux kernel
Corpus path
drivers/i2c/muxes/i2c-mux-mlxcpld.c
Extension
.c
Size
5250 bytes
Lines
194
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 mlxcpld_mux {
	int last_val;
	struct i2c_client *client;
	struct mlxcpld_mux_plat_data pdata;
};

/* MUX logic description.
 * Driver can support different mux control logic, according to CPLD
 * implementation.
 *
 * Connectivity schema.
 *
 * i2c-mlxcpld                                 Digital               Analog
 * driver
 * *--------*                                 * -> mux1 (virt bus2) -> mux -> |
 * | I2CLPC | i2c physical                    * -> mux2 (virt bus3) -> mux -> |
 * | bridge | bus 1                 *---------*                               |
 * | logic  |---------------------> * mux reg *                               |
 * | in CPLD|                       *---------*                               |
 * *--------*   i2c-mux-mlxpcld          ^    * -> muxn (virt busn) -> mux -> |
 *     |        driver                   |                                    |
 *     |        *---------------*        |                              Devices
 *     |        * CPLD (i2c bus)* select |
 *     |        * registers for *--------*
 *     |        * mux selection * deselect
 *     |        *---------------*
 *     |                 |
 * <-------->     <----------->
 * i2c cntrl      Board cntrl reg
 * reg space      space (mux select,
 *                IO, LED, WD, info)
 *
 */

/* Write to mux register. Don't use i2c_transfer() and i2c_smbus_xfer()
 * for this as they will try to lock adapter a second time.
 */
static int mlxcpld_mux_reg_write(struct i2c_adapter *adap,
				 struct mlxcpld_mux *mux, u32 val)
{
	struct i2c_client *client = mux->client;
	union i2c_smbus_data data;
	struct i2c_msg msg;
	u8 buf[3];

	switch (mux->pdata.reg_size) {
	case 1:
		data.byte = val;
		return __i2c_smbus_xfer(adap, client->addr, client->flags,
					I2C_SMBUS_WRITE, mux->pdata.sel_reg_addr,
					I2C_SMBUS_BYTE_DATA, &data);
	case 2:
		buf[0] = mux->pdata.sel_reg_addr >> 8;
		buf[1] = mux->pdata.sel_reg_addr;
		buf[2] = val;
		msg.addr = client->addr;
		msg.buf = buf;
		msg.len = mux->pdata.reg_size + 1;
		msg.flags = 0;
		return __i2c_transfer(adap, &msg, 1);
	default:
		return -EINVAL;
	}
}

static int mlxcpld_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
{
	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);
	u32 regval = chan;
	int err = 0;

	if (mux->pdata.reg_size == 1)
		regval += 1;

	/* Only select the channel if its different from the last channel */
	if (mux->last_val != regval) {
		err = mlxcpld_mux_reg_write(muxc->parent, mux, regval);
		mux->last_val = err < 0 ? -1 : regval;
	}

	return err;
}

static int mlxcpld_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
{
	struct mlxcpld_mux *mux = i2c_mux_priv(muxc);

	/* Deselect active channel */
	mux->last_val = -1;

Annotation

Implementation Notes