public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Ming Yu <a0282524688@gmail.com>
To: tmyu0@nuvoton.com, lee@kernel.org, linus.walleij@linaro.org,
	brgl@bgdev.pl, andi.shyti@kernel.org, mkl@pengutronix.de,
	mailhol.vincent@wanadoo.fr, andrew+netdev@lunn.ch,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, wim@linux-watchdog.org, linux@roeck-us.net,
	jdelvare@suse.com, jic23@kernel.org, lars@metafoo.de,
	ukleinek@kernel.org, alexandre.belloni@bootlin.com
Cc: linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
	linux-i2c@vger.kernel.org, linux-can@vger.kernel.org,
	netdev@vger.kernel.org, linux-watchdog@vger.kernel.org,
	linux-hwmon@vger.kernel.org, linux-iio@vger.kernel.org,
	linux-pwm@vger.kernel.org, linux-rtc@vger.kernel.org
Subject: [PATCH v1 3/9] i2c: Add Nuvoton NCT6694 I2C support
Date: Thu, 24 Oct 2024 16:59:16 +0800	[thread overview]
Message-ID: <20241024085922.133071-4-tmyu0@nuvoton.com> (raw)
In-Reply-To: <20241024085922.133071-1-tmyu0@nuvoton.com>

This driver supports I2C adapter functionality for NCT6694 MFD
device based on USB interface, each I2C controller use default
baudrate(100K).

Signed-off-by: Ming Yu <tmyu0@nuvoton.com>
---
 MAINTAINERS                      |   1 +
 drivers/i2c/busses/Kconfig       |  10 ++
 drivers/i2c/busses/Makefile      |   1 +
 drivers/i2c/busses/i2c-nct6694.c | 166 +++++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-nct6694.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 2c86d5dab3f1..1cc64f9f154a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16439,6 +16439,7 @@ M:	Ming Yu <tmyu0@nuvoton.com>
 L:	linux-kernel@vger.kernel.org
 S:	Supported
 F:	drivers/gpio/gpio-nct6694.c
+F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	include/linux/mfd/nct6694.h
 
diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index 6b3ba7e5723a..01a60de4b8a4 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -1315,6 +1315,16 @@ config I2C_LJCA
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-ljca.
 
+config I2C_NCT6694
+	tristate "Nuvoton NCT6694 I2C adapter support"
+	depends on MFD_NCT6694
+	help
+	  If you say yes to this option, support will be included for Nuvoton
+	  NCT6694, a USB to I2C interface.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-nct6604.
+
 config I2C_CP2615
 	tristate "Silicon Labs CP2615 USB sound card and I2C adapter"
 	depends on USB
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index ecc07c50f2a0..3c4a0ea5a46f 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -135,6 +135,7 @@ obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
 obj-$(CONFIG_I2C_DLN2)		+= i2c-dln2.o
 obj-$(CONFIG_I2C_LJCA)		+= i2c-ljca.o
 obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o
+obj-$(CONFIG_I2C_NCT6694)	+= i2c-nct6694.o
 obj-$(CONFIG_I2C_PARPORT)	+= i2c-parport.o
 obj-$(CONFIG_I2C_PCI1XXXX)	+= i2c-mchp-pci1xxxx.o
 obj-$(CONFIG_I2C_ROBOTFUZZ_OSIF)	+= i2c-robotfuzz-osif.o
diff --git a/drivers/i2c/busses/i2c-nct6694.c b/drivers/i2c/busses/i2c-nct6694.c
new file mode 100644
index 000000000000..b33d90f26f9f
--- /dev/null
+++ b/drivers/i2c/busses/i2c-nct6694.c
@@ -0,0 +1,166 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 I2C adapter driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+
+/* Host interface */
+#define REQUEST_I2C_MOD		0x03
+
+/* Message Channel*/
+/* Command 00h */
+#define REQUEST_I2C_OFFSET	0x0000	/* OFFSET = SEL|CMD */
+#define REQUEST_I2C_LEN		0x90
+#define I2C_PORT_IDX		0x00
+#define I2C_BR_IDX		0x01
+#define I2C_ADDR_IDX		0x02
+#define I2C_W_CNT_IDX		0x03
+#define I2C_R_CNT_IDX		0x04
+
+#define I2C_RD_IDX		0x50
+#define I2C_WR_IDX		0x10
+
+#define DRVNAME "nct6694-i2c"
+
+enum i2c_baudrate {
+	I2C_BR_25K = 0,
+	I2C_BR_50K,
+	I2C_BR_100K,
+	I2C_BR_200K,
+	I2C_BR_400K,
+	I2C_BR_800K,
+	I2C_BR_1M
+};
+
+struct nct6694_i2c_data {
+	struct nct6694 *nct6694;
+	struct i2c_adapter adapter;
+	unsigned char port;
+	unsigned char br;
+};
+
+static int nct6694_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
+{
+	struct nct6694_i2c_data *data = adap->algo_data;
+	int ret, i;
+
+	for (i = 0; i < num ; i++) {
+		unsigned char buf[REQUEST_I2C_LEN] = {0};
+		struct i2c_msg *msg_temp = &msgs[i];
+
+		if (msg_temp->len > 64)
+			return -EPROTO;
+
+		buf[I2C_PORT_IDX] = data->port;
+		buf[I2C_BR_IDX] = data->br;
+		buf[I2C_ADDR_IDX] = i2c_8bit_addr_from_msg(msg_temp);
+		if (msg_temp->flags & I2C_M_RD) {
+			buf[I2C_R_CNT_IDX] = msg_temp->len;
+			ret = nct6694_write_msg(data->nct6694, REQUEST_I2C_MOD,
+						REQUEST_I2C_OFFSET, REQUEST_I2C_LEN,
+						buf);
+			if (ret < 0)
+				return 0;
+			memcpy(msg_temp->buf, buf + I2C_RD_IDX, msg_temp->len);
+		} else {
+			buf[I2C_W_CNT_IDX] = msg_temp->len;
+			memcpy(buf + I2C_WR_IDX, msg_temp->buf, msg_temp->len);
+			ret = nct6694_write_msg(data->nct6694, REQUEST_I2C_MOD,
+						REQUEST_I2C_OFFSET, REQUEST_I2C_LEN,
+						buf);
+			if (ret < 0)
+				return 0;
+		}
+	}
+
+	return num;
+}
+
+static u32 nct6694_func(struct i2c_adapter *adapter)
+{
+	return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL);
+}
+
+static const struct i2c_algorithm algorithm = {
+	.master_xfer = nct6694_xfer,
+	.functionality = nct6694_func,
+};
+
+static int nct6694_i2c_probe(struct platform_device *pdev)
+{
+	const struct mfd_cell *cell = mfd_get_cell(pdev);
+	struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+	struct nct6694_i2c_data *data;
+	int ret;
+
+	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+
+	data->nct6694 = nct6694;
+	data->port = cell->id;
+	data->br = I2C_BR_100K;
+
+	sprintf(data->adapter.name, "NCT6694 I2C Adapter %d", cell->id);
+	data->adapter.owner = THIS_MODULE;
+	data->adapter.algo = &algorithm;
+	data->adapter.dev.parent = &pdev->dev;
+	data->adapter.algo_data = data;
+
+	platform_set_drvdata(pdev, data);
+
+	ret = i2c_add_adapter(&data->adapter);
+	if (ret) {
+		dev_err(&pdev->dev, "%s: Failed to register I2C Adapter: %pe\n",
+			__func__, ERR_PTR(ret));
+	}
+
+	return ret;
+}
+
+static void nct6694_i2c_remove(struct platform_device *pdev)
+{
+	struct nct6694_i2c_data *data = platform_get_drvdata(pdev);
+
+	i2c_del_adapter(&data->adapter);
+}
+
+static struct platform_driver nct6694_i2c_driver = {
+	.driver = {
+		.name	= DRVNAME,
+	},
+	.probe		= nct6694_i2c_probe,
+	.remove		= nct6694_i2c_remove,
+};
+
+static int __init nct6694_init(void)
+{
+	int err;
+
+	err = platform_driver_register(&nct6694_i2c_driver);
+	if (!err) {
+		if (err)
+			platform_driver_unregister(&nct6694_i2c_driver);
+	}
+
+	return err;
+}
+subsys_initcall(nct6694_init);
+
+static void __exit nct6694_exit(void)
+{
+	platform_driver_unregister(&nct6694_i2c_driver);
+}
+module_exit(nct6694_exit);
+
+MODULE_DESCRIPTION("USB-I2C adapter driver for NCT6694");
+MODULE_AUTHOR("Ming Yu <tmyu0@nuvoton.com>");
+MODULE_LICENSE("GPL");
-- 
2.34.1


  parent reply	other threads:[~2024-10-24  8:59 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-24  8:59 [PATCH v1 0/9] Add Nuvoton NCT6694 MFD devices Ming Yu
2024-10-24  8:59 ` [PATCH v1 1/9] mfd: Add core driver for Nuvoton NCT6694 Ming Yu
2024-10-24  9:03   ` Marc Kleine-Budde
2024-10-25  8:00     ` Ming Yu
2024-10-24  9:57   ` Marc Kleine-Budde
2024-10-25  8:02     ` Ming Yu
2024-10-24 15:20   ` Marc Kleine-Budde
2024-10-24 15:34     ` Marc Kleine-Budde
2024-10-25  8:14       ` Ming Yu
2024-10-25  8:35         ` Marc Kleine-Budde
2024-10-25  9:02           ` Marc Kleine-Budde
2024-10-25 10:22             ` Ming Yu
2024-10-25  8:08     ` Ming Yu
2024-10-25 10:08       ` Marc Kleine-Budde
2024-10-25 11:03         ` Ming Yu
2024-10-25 12:23           ` Marc Kleine-Budde
2024-10-28  7:33             ` Ming Yu
2024-10-28  7:52               ` Marc Kleine-Budde
2024-10-28  8:31                 ` Ming Yu
2024-10-28 14:06                   ` Marc Kleine-Budde
2024-10-29  3:45                     ` Ming Yu
2024-10-29  8:14                       ` Marc Kleine-Budde
2024-11-01  5:35                         ` Ming Yu
2024-10-26 14:58   ` Christophe JAILLET
2024-10-28  7:37     ` Ming Yu
2024-10-24  8:59 ` [PATCH v1 2/9] gpio: Add Nuvoton NCT6694 GPIO support Ming Yu
2024-10-24  9:47   ` Bartosz Golaszewski
     [not found]     ` <CAOoeyxUUOCSaDLK8=ox3hwDVu=Ej-ds4=FsS8F+9GfiE-8HYvg@mail.gmail.com>
2024-10-25  7:12       ` Bartosz Golaszewski
2024-10-25  7:38     ` 游子民
2024-10-25  7:46       ` Bartosz Golaszewski
2024-10-28  8:56         ` Ming Yu
2024-10-30 19:32           ` Bartosz Golaszewski
2024-11-01  6:15             ` Ming Yu
2024-10-24  8:59 ` Ming Yu [this message]
2024-10-24 10:41   ` [PATCH v1 3/9] i2c: Add Nuvoton NCT6694 I2C support Andi Shyti
2024-10-25  7:47     ` 游子民
2024-10-24  8:59 ` [PATCH v1 4/9] can: Add Nuvoton NCT6694 CAN support Ming Yu
2024-10-24 10:03   ` Marc Kleine-Budde
2024-10-24 10:05     ` Marc Kleine-Budde
2024-11-01  1:27     ` Ming Yu
2024-10-24 12:12   ` Marc Kleine-Budde
2024-10-24 15:28     ` Marc Kleine-Budde
2024-11-01  5:32       ` Ming Yu
2024-10-24 14:17   ` Marc Kleine-Budde
2024-10-24 14:20     ` Marc Kleine-Budde
2024-11-01  1:44       ` Ming Yu
2024-11-01  1:37     ` Ming Yu
2024-10-25 11:18   ` kernel test robot
2024-10-25 23:31   ` kernel test robot
2024-10-24  8:59 ` [PATCH v1 5/9] watchdog: Add Nuvoton NCT6694 WDT support Ming Yu
2024-10-24 15:32   ` Guenter Roeck
2024-10-28  9:49     ` Ming Yu
2024-10-24 16:06   ` Guenter Roeck
2024-10-26  9:19   ` kernel test robot
2024-10-24  8:59 ` [PATCH v1 6/9] hwmon: Add Nuvoton NCT6694 HWMON support Ming Yu
2024-10-24  9:20   ` Kalesh Anakkur Purayil
2024-10-24 14:53     ` Guenter Roeck
2024-10-25 15:22       ` Ming Yu
2024-10-25 15:44         ` Guenter Roeck
2024-10-26 14:50           ` Guenter Roeck
2024-10-28  7:58             ` Ming Yu
2024-10-28 18:54               ` Jonathan Cameron
2024-10-30  3:29                 ` Ming Yu
2024-10-30  4:26                   ` Guenter Roeck
2024-11-01  6:11                     ` Ming Yu
2024-10-28  7:42           ` Ming Yu
2024-10-25 15:10     ` Ming Yu
2024-10-24 15:03   ` Guenter Roeck
2024-10-25 15:33     ` Ming Yu
2024-10-24  8:59 ` [PATCH v1 7/9] iio: adc: Add Nuvoton NCT6694 IIO support Ming Yu
2024-10-26 14:41   ` Jonathan Cameron
2024-11-05  6:21     ` Ming Yu
2024-10-24  8:59 ` [PATCH v1 8/9] pwm: Add Nuvoton NCT6694 PWM support Ming Yu
2024-11-22 18:05   ` Uwe Kleine-König
2024-10-24  8:59 ` [PATCH v1 9/9] rtc: Add Nuvoton NCT6694 RTC support Ming Yu
2024-10-25 23:34   ` Nobuhiro Iwamatsu
2024-10-28  8:42     ` Ming Yu
2024-10-24 11:57 ` [PATCH v1 0/9] Add Nuvoton NCT6694 MFD devices Marc Kleine-Budde
2024-10-25  8:22   ` Ming Yu
2024-10-25  8:33     ` Marc Kleine-Budde
2024-10-30  8:30       ` Ming Yu
2024-10-30 10:12         ` Marc Kleine-Budde
2024-10-30 14:21           ` Ming Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241024085922.133071-4-tmyu0@nuvoton.com \
    --to=a0282524688@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andi.shyti@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=brgl@bgdev.pl \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jdelvare@suse.com \
    --cc=jic23@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lars@metafoo.de \
    --cc=lee@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=tmyu0@nuvoton.com \
    --cc=ukleinek@kernel.org \
    --cc=wim@linux-watchdog.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox