From nobody Thu Nov 28 15:46:08 2024 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49F301C1ABE; Tue, 1 Oct 2024 11:54:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727783651; cv=none; b=TSufoJgbublLBokFZXS1mUMmD3RqMV/9JuEHIq8S1zYzfryveJiIIxZZQ+WZKUd6pJmISdaCRJZZPVeOB5O/NcezSfLMOOWtpzHsHVS4RuGG8rwtmbA5hj9qPzZU9ocAAmibjrY8kV6OzgnXVtZdDyv/g4X73xMU+0IIF+AFNO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727783651; c=relaxed/simple; bh=KDqmb3ktuc9m5IAQTHG4aDFNrvdGw74h2nSbelVa4A8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ldUEBvXft8ws/rJ7omva/74U5DYKz28ikMd3BmP+kRZO0m4rk7dxxmiTULsGZ3iwWJn4LFaLwTYwjd+dXA9pX9flnWrWpQYOaSUYDZA6ywCNQkp6hYyVa7Jjm/9fdmWTMdg+fc8sijbPmHOMvu44lRX5pH4oV85taHwpZkMhjDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=LtQeucD+; arc=none smtp.client-ip=217.70.183.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="LtQeucD+" Received: by mail.gandi.net (Postfix) with ESMTPSA id 4FA7D40006; Tue, 1 Oct 2024 11:54:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1727783646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XCSDYGMQrc1XdlAUwurjNtgekrjkqYRs263mZ2xKJM0=; b=LtQeucD+Y8FdwLQi65BzzKt/Vmwb8yxGFEajTQYqOfxTAjG9FTJjfK2w/GCWYc/5gNR6e1 JvZA0EfCCj17cWSw8wHZkbQ+nH/T2nuoH+CfzBVFtIHCpz3j80qWSBPcMYgD6dne1UrtSs REEz4nMjpEYVhhXoH+98NSfDS9gBWmkx6PqoRsCpmuGZuLwrZFmU0xcDHZ2mwowhsi3vHA r3oKfMBcXxNEKrE3RaF1opE0m2NJvLv5scfvbLRTOu92V0Ntyyh5WQyuDMf/EayIq/TMOf odyL3jJhSo3O1SzaJI5TFzd5EOdkgQMvw0yWgNdnjWgtVuE1qwSK8Ba7MuVi1A== From: Thomas Richard Date: Tue, 01 Oct 2024 13:53:29 +0200 Subject: [PATCH v3 3/5] i2c: Congatec Board Controller i2c bus driver Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241001-congatec-board-controller-v3-3-39ceceed5c47@bootlin.com> References: <20241001-congatec-board-controller-v3-0-39ceceed5c47@bootlin.com> In-Reply-To: <20241001-congatec-board-controller-v3-0-39ceceed5c47@bootlin.com> To: Lee Jones , Linus Walleij , Bartosz Golaszewski , Andi Shyti , Wim Van Sebroeck , Guenter Roeck Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org, linux-i2c@vger.kernel.org, linux-watchdog@vger.kernel.org, thomas.petazzoni@bootlin.com, blake.vermeer@keysight.com, Thomas Richard X-Mailer: b4 0.14.1 X-GND-Sasl: thomas.richard@bootlin.com Add i2c support for the Congatec Board Controller. The Board Controller has two i2c busses, a General Purpose bus and a Power Management bus. Acked-by: Andi Shyti Signed-off-by: Thomas Richard --- drivers/i2c/busses/Kconfig | 10 ++ drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-cgbc.c | 406 ++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 417 insertions(+) diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index 6b3ba7e5723a..4977abcd7c46 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig @@ -535,6 +535,16 @@ config I2C_CBUS_GPIO This driver can also be built as a module. If so, the module will be called i2c-cbus-gpio. =20 +config I2C_CGBC + tristate "Congatec I2C Controller" + depends on MFD_CGBC + help + This driver supports the 2 I2C interfaces on the Congatec Board + Controller. + + This driver can also be built as a module. If so, the module will + be called i2c-cgbc.ko. + config I2C_CPM tristate "Freescale CPM1 or CPM2 (MPC8xx/826x)" depends on CPM1 || CPM2 diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index ecc07c50f2a0..a6bcbf2febcf 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile @@ -50,6 +50,7 @@ obj-$(CONFIG_I2C_BCM2835) +=3D i2c-bcm2835.o obj-$(CONFIG_I2C_BCM_IPROC) +=3D i2c-bcm-iproc.o obj-$(CONFIG_I2C_CADENCE) +=3D i2c-cadence.o obj-$(CONFIG_I2C_CBUS_GPIO) +=3D i2c-cbus-gpio.o +obj-$(CONFIG_I2C_CGBC) +=3D i2c-cgbc.o obj-$(CONFIG_I2C_CPM) +=3D i2c-cpm.o obj-$(CONFIG_I2C_DAVINCI) +=3D i2c-davinci.o obj-$(CONFIG_I2C_DESIGNWARE_CORE) +=3D i2c-designware-core.o diff --git a/drivers/i2c/busses/i2c-cgbc.c b/drivers/i2c/busses/i2c-cgbc.c new file mode 100644 index 000000000000..eba0b205de11 --- /dev/null +++ b/drivers/i2c/busses/i2c-cgbc.c @@ -0,0 +1,406 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Congatec Board Controller I2C busses driver + * + * Copyright (C) 2024 Bootlin + * Author: Thomas Richard + */ + +#include +#include +#include +#include +#include + +#define CGBC_I2C_PRIMARY_BUS_ID 0 +#define CGBC_I2C_PM_BUS_ID 4 + +#define CGBC_I2C_CMD_START 0x40 +#define CGBC_I2C_CMD_STAT 0x48 +#define CGBC_I2C_CMD_DATA 0x50 +#define CGBC_I2C_CMD_SPEED 0x58 + +#define CGBC_I2C_STAT_IDL 0x00 +#define CGBC_I2C_STAT_DAT 0x01 +#define CGBC_I2C_STAT_BUSY 0x02 + +#define CGBC_I2C_START 0x80 +#define CGBC_I2C_STOP 0x40 + +#define CGBC_I2C_LAST_ACK 0x80 /* send ACK on last read byte */ + +/* + * Reference code defines 1kHz as min freq and 6.1MHz as max freq. + * But in practice, the board controller limits the frequency to 1MHz, and= the + * 1kHz is not functional (minimal working freq is 50kHz). + * So use these values as limits. + */ +#define CGBC_I2C_FREQ_MIN_HZ 50000 /* 50 kHz */ +#define CGBC_I2C_FREQ_MAX_HZ 1000000 /* 1 MHz */ + +#define CGBC_I2C_FREQ_UNIT_1KHZ 0x40 +#define CGBC_I2C_FREQ_UNIT_10KHZ 0x80 +#define CGBC_I2C_FREQ_UNIT_100KHZ 0xC0 + +#define CGBC_I2C_FREQ_UNIT_MASK 0xC0 +#define CGBC_I2C_FREQ_VALUE_MASK 0x3F + +#define CGBC_I2C_READ_MAX_LEN 31 +#define CGBC_I2C_WRITE_MAX_LEN 32 + +#define CGBC_I2C_CMD_HEADER_SIZE 4 +#define CGBC_I2C_CMD_SIZE (CGBC_I2C_CMD_HEADER_SIZE + CGBC_I2C_WRITE_MAX_= LEN) + +enum cgbc_i2c_state { + CGBC_I2C_STATE_DONE =3D 0, + CGBC_I2C_STATE_INIT, + CGBC_I2C_STATE_START, + CGBC_I2C_STATE_READ, + CGBC_I2C_STATE_WRITE, + CGBC_I2C_STATE_ERROR, +}; + +struct i2c_algo_cgbc_data { + u8 bus_id; + unsigned long read_maxtime_us; +}; + +struct cgbc_i2c_data { + struct device *dev; + struct cgbc_device_data *cgbc; + struct i2c_adapter adap; + struct i2c_msg *msg; + int nmsgs; + int pos; + enum cgbc_i2c_state state; +}; + +struct cgbc_i2c_transfer { + u8 bus_id; + bool start; + bool stop; + bool last_ack; + u8 read; + u8 write; + u8 addr; + u8 data[CGBC_I2C_WRITE_MAX_LEN]; +}; + +static u8 cgbc_i2c_freq_to_reg(unsigned int bus_frequency) +{ + u8 reg; + + if (bus_frequency <=3D 10000) + reg =3D CGBC_I2C_FREQ_UNIT_1KHZ | (bus_frequency / 1000); + else if (bus_frequency <=3D 100000) + reg =3D CGBC_I2C_FREQ_UNIT_10KHZ | (bus_frequency / 10000); + else + reg =3D CGBC_I2C_FREQ_UNIT_100KHZ | (bus_frequency / 100000); + + return reg; +} + +static unsigned int cgbc_i2c_reg_to_freq(u8 reg) +{ + unsigned int freq =3D reg & CGBC_I2C_FREQ_VALUE_MASK; + u8 unit =3D reg & CGBC_I2C_FREQ_UNIT_MASK; + + if (unit =3D=3D CGBC_I2C_FREQ_UNIT_100KHZ) + return freq * 100000; + else if (unit =3D=3D CGBC_I2C_FREQ_UNIT_10KHZ) + return freq * 10000; + else + return freq * 1000; +} + +static int cgbc_i2c_get_status(struct i2c_adapter *adap) +{ + struct i2c_algo_cgbc_data *algo_data =3D adap->algo_data; + struct cgbc_i2c_data *i2c =3D i2c_get_adapdata(adap); + struct cgbc_device_data *cgbc =3D i2c->cgbc; + u8 cmd =3D CGBC_I2C_CMD_STAT | algo_data->bus_id; + u8 status; + int ret; + + ret =3D cgbc_command(cgbc, &cmd, sizeof(cmd), NULL, 0, &status); + if (ret) + return ret; + + return status; +} + +static int cgbc_i2c_set_frequency(struct i2c_adapter *adap, + unsigned int bus_frequency) +{ + struct i2c_algo_cgbc_data *algo_data =3D adap->algo_data; + struct cgbc_i2c_data *i2c =3D i2c_get_adapdata(adap); + struct cgbc_device_data *cgbc =3D i2c->cgbc; + u8 cmd[2], data; + int ret; + + if (bus_frequency > CGBC_I2C_FREQ_MAX_HZ || + bus_frequency < CGBC_I2C_FREQ_MIN_HZ) { + dev_info(i2c->dev, "invalid frequency %u, using default\n", bus_frequenc= y); + bus_frequency =3D I2C_MAX_STANDARD_MODE_FREQ; + } + + cmd[0] =3D CGBC_I2C_CMD_SPEED | algo_data->bus_id; + cmd[1] =3D cgbc_i2c_freq_to_reg(bus_frequency); + + ret =3D cgbc_command(cgbc, &cmd, sizeof(cmd), &data, 1, NULL); + if (ret) + return dev_err_probe(i2c->dev, ret, + "Failed to initialize I2C bus %s", + adap->name); + + cmd[1] =3D 0x00; + + ret =3D cgbc_command(cgbc, &cmd, sizeof(cmd), &data, 1, NULL); + if (ret) + return dev_err_probe(i2c->dev, ret, + "Failed to get I2C bus frequency"); + + bus_frequency =3D cgbc_i2c_reg_to_freq(data); + + dev_dbg(i2c->dev, "%s is running at %d Hz\n", adap->name, bus_frequency); + + /* + * The read_maxtime_us variable represents the maximum time to wait + * for data during a read operation. The maximum amount of data that + * can be read by a command is CGBC_I2C_READ_MAX_LEN. + * Therefore, calculate the max time to properly size the timeout. + */ + algo_data->read_maxtime_us =3D (BITS_PER_BYTE + 1) * CGBC_I2C_READ_MAX_LEN + * USEC_PER_SEC / bus_frequency; + + return 0; +} + +static unsigned int cgbc_i2c_xfer_to_cmd(struct cgbc_i2c_transfer xfer, u8= *cmd) +{ + int i =3D 0; + + cmd[i++] =3D CGBC_I2C_CMD_START | xfer.bus_id; + + cmd[i] =3D (xfer.start) ? CGBC_I2C_START : 0x00; + if (xfer.stop) + cmd[i] |=3D CGBC_I2C_STOP; + cmd[i++] |=3D (xfer.start) ? xfer.write + 1 : xfer.write; + + cmd[i++] =3D (xfer.last_ack) ? (xfer.read | CGBC_I2C_LAST_ACK) : xfer.rea= d; + + if (xfer.start) + cmd[i++] =3D xfer.addr; + + if (xfer.write > 0) + memcpy(&cmd[i], &xfer.data, xfer.write); + + return i + xfer.write; +} + +static int cgbc_i2c_xfer_msg(struct i2c_adapter *adap) +{ + struct i2c_algo_cgbc_data *algo_data =3D adap->algo_data; + struct cgbc_i2c_data *i2c =3D i2c_get_adapdata(adap); + struct cgbc_device_data *cgbc =3D i2c->cgbc; + struct i2c_msg *msg =3D i2c->msg; + u8 cmd[CGBC_I2C_CMD_SIZE]; + int ret, max_len, len, i; + unsigned int cmd_len; + u8 cmd_data; + + struct cgbc_i2c_transfer xfer =3D { + .bus_id =3D algo_data->bus_id, + .addr =3D i2c_8bit_addr_from_msg(msg), + }; + + if (i2c->state =3D=3D CGBC_I2C_STATE_DONE) + return 0; + + ret =3D cgbc_i2c_get_status(adap); + + if (ret =3D=3D CGBC_I2C_STAT_BUSY) + return -EBUSY; + else if (ret < 0) + goto err; + + if (i2c->state =3D=3D CGBC_I2C_STATE_INIT || + (i2c->state =3D=3D CGBC_I2C_STATE_WRITE && msg->flags & I2C_M_RD)) + xfer.start =3D true; + + i2c->state =3D (msg->flags & I2C_M_RD) ? CGBC_I2C_STATE_READ : CGBC_I2C_S= TATE_WRITE; + + max_len =3D (i2c->state =3D=3D CGBC_I2C_STATE_READ) ? + CGBC_I2C_READ_MAX_LEN : CGBC_I2C_WRITE_MAX_LEN; + + if (msg->len - i2c->pos > max_len) { + len =3D max_len; + } else { + len =3D msg->len - i2c->pos; + + if (i2c->nmsgs =3D=3D 1) + xfer.stop =3D true; + } + + if (i2c->state =3D=3D CGBC_I2C_STATE_WRITE) { + xfer.write =3D len; + xfer.read =3D 0; + + for (i =3D 0; i < len; i++) + xfer.data[i] =3D msg->buf[i2c->pos + i]; + + cmd_len =3D cgbc_i2c_xfer_to_cmd(xfer, &cmd[0]); + + ret =3D cgbc_command(cgbc, &cmd, cmd_len, NULL, 0, NULL); + if (ret) + goto err; + } else if (i2c->state =3D=3D CGBC_I2C_STATE_READ) { + xfer.write =3D 0; + xfer.read =3D len; + + if (i2c->nmsgs > 1 || msg->len - i2c->pos > max_len) + xfer.read |=3D CGBC_I2C_LAST_ACK; + + cmd_len =3D cgbc_i2c_xfer_to_cmd(xfer, &cmd[0]); + ret =3D cgbc_command(cgbc, &cmd, cmd_len, NULL, 0, NULL); + if (ret) + goto err; + + ret =3D read_poll_timeout(cgbc_i2c_get_status, ret, + ret !=3D CGBC_I2C_STAT_BUSY, 0, + 2 * algo_data->read_maxtime_us, false, adap); + if (ret < 0) + goto err; + + cmd_data =3D CGBC_I2C_CMD_DATA | algo_data->bus_id; + ret =3D cgbc_command(cgbc, &cmd_data, sizeof(cmd_data), + msg->buf + i2c->pos, len, NULL); + if (ret) + goto err; + } + + if (len =3D=3D (msg->len - i2c->pos)) { + i2c->msg++; + i2c->nmsgs--; + i2c->pos =3D 0; + } else { + i2c->pos +=3D len; + } + + if (i2c->nmsgs =3D=3D 0) + i2c->state =3D CGBC_I2C_STATE_DONE; + + return 0; + +err: + i2c->state =3D CGBC_I2C_STATE_ERROR; + return ret; +} + +static int cgbc_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, + int num) +{ + struct cgbc_i2c_data *i2c =3D i2c_get_adapdata(adap); + unsigned long timeout =3D jiffies + HZ; + int ret; + + i2c->state =3D CGBC_I2C_STATE_INIT; + i2c->msg =3D msgs; + i2c->nmsgs =3D num; + i2c->pos =3D 0; + + while (time_before(jiffies, timeout)) { + ret =3D cgbc_i2c_xfer_msg(adap); + if (i2c->state =3D=3D CGBC_I2C_STATE_DONE) + return num; + + if (i2c->state =3D=3D CGBC_I2C_STATE_ERROR) + return ret; + + if (ret =3D=3D 0) + timeout =3D jiffies + HZ; + } + + i2c->state =3D CGBC_I2C_STATE_ERROR; + return -ETIMEDOUT; +} + +static u32 cgbc_i2c_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~(I2C_FUNC_SMBUS_QUICK)); +} + +static const struct i2c_algorithm cgbc_i2c_algorithm =3D { + .master_xfer =3D cgbc_i2c_xfer, + .functionality =3D cgbc_i2c_func, +}; + +static struct i2c_algo_cgbc_data cgbc_i2c_algo_data[] =3D { + { .bus_id =3D CGBC_I2C_PRIMARY_BUS_ID }, + { .bus_id =3D CGBC_I2C_PM_BUS_ID }, +}; + +static const struct i2c_adapter cgbc_i2c_adapter[] =3D { + { + .owner =3D THIS_MODULE, + .name =3D "Congatec General Purpose I2C adapter", + .class =3D I2C_CLASS_DEPRECATED, + .algo =3D &cgbc_i2c_algorithm, + .algo_data =3D &cgbc_i2c_algo_data[0], + .nr =3D -1, + }, + { + .owner =3D THIS_MODULE, + .name =3D "Congatec Power Management I2C adapter", + .class =3D I2C_CLASS_DEPRECATED, + .algo =3D &cgbc_i2c_algorithm, + .algo_data =3D &cgbc_i2c_algo_data[1], + .nr =3D -1, + }, +}; + +static int cgbc_i2c_probe(struct platform_device *pdev) +{ + struct cgbc_device_data *cgbc =3D dev_get_drvdata(pdev->dev.parent); + struct cgbc_i2c_data *i2c; + int ret; + + i2c =3D devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); + if (!i2c) + return -ENOMEM; + + i2c->cgbc =3D cgbc; + i2c->dev =3D &pdev->dev; + i2c->adap =3D cgbc_i2c_adapter[pdev->id]; + i2c->adap.dev.parent =3D i2c->dev; + i2c_set_adapdata(&i2c->adap, i2c); + platform_set_drvdata(pdev, i2c); + + ret =3D cgbc_i2c_set_frequency(&i2c->adap, I2C_MAX_STANDARD_MODE_FREQ); + if (ret) + return ret; + + return i2c_add_numbered_adapter(&i2c->adap); +} + +static void cgbc_i2c_remove(struct platform_device *pdev) +{ + struct cgbc_i2c_data *i2c =3D platform_get_drvdata(pdev); + + i2c_del_adapter(&i2c->adap); +} + +static struct platform_driver cgbc_i2c_driver =3D { + .driver =3D { + .name =3D "cgbc-i2c", + }, + .probe =3D cgbc_i2c_probe, + .remove_new =3D cgbc_i2c_remove, +}; + +module_platform_driver(cgbc_i2c_driver); + +MODULE_DESCRIPTION("Congatec Board Controller I2C Driver"); +MODULE_AUTHOR("Thomas Richard "); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:cgbc_i2c"); --=20 2.39.5