[1/1] mfd: Add I2C based System Configuaration (SYSCON) access

Message ID 20200616172904.373045-1-lee.jones@linaro.org
State New
Headers show
Series
  • [1/1] mfd: Add I2C based System Configuaration (SYSCON) access
Related show

Commit Message

Lee Jones June 16, 2020, 5:29 p.m.
The existing SYSCON implementation only supports MMIO (memory mapped)
accesses, facilitated by Regmap.  This extends support for registers
held behind I2C busses.

Signed-off-by: Lee Jones <lee.jones@linaro.org>

---
 drivers/mfd/Kconfig            |  7 +++
 drivers/mfd/Makefile           |  1 +
 drivers/mfd/syscon-i2c.c       | 89 ++++++++++++++++++++++++++++++++++
 include/linux/mfd/syscon-i2c.h | 26 ++++++++++
 4 files changed, 123 insertions(+)
 create mode 100644 drivers/mfd/syscon-i2c.c
 create mode 100644 include/linux/mfd/syscon-i2c.h

-- 
2.25.1

Comments

Lee Jones June 16, 2020, 6:06 p.m. | #1
On Tue, 16 Jun 2020, Lee Jones wrote:

> The existing SYSCON implementation only supports MMIO (memory mapped)

> accesses, facilitated by Regmap.  This extends support for registers

> held behind I2C busses.

> 

> Signed-off-by: Lee Jones <lee.jones@linaro.org>

> ---

>  drivers/mfd/Kconfig            |  7 +++

>  drivers/mfd/Makefile           |  1 +

>  drivers/mfd/syscon-i2c.c       | 89 ++++++++++++++++++++++++++++++++++

>  include/linux/mfd/syscon-i2c.h | 26 ++++++++++

>  4 files changed, 123 insertions(+)

>  create mode 100644 drivers/mfd/syscon-i2c.c

>  create mode 100644 include/linux/mfd/syscon-i2c.h

> 

> diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig

> index 0a59249198d34..63ae78b92b086 100644

> --- a/drivers/mfd/Kconfig

> +++ b/drivers/mfd/Kconfig

> @@ -1300,6 +1300,13 @@ config MFD_SYSCON

>  	  Select this option to enable accessing system control registers

>  	  via regmap.

>  

> +config MFD_SYSCON_I2C

> +	bool "System Controller Register R/W Based on I2C Regmap"

> +	select REGMAP_I2C

> +	help

> +	  Select this option to enable accessing system control registers

> +	  via I2C using regmap.

> +

>  config MFD_DAVINCI_VOICECODEC

>  	tristate

>  	select MFD_CORE

> diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile

> index f935d10cbf0fc..0aec1f42ac979 100644

> --- a/drivers/mfd/Makefile

> +++ b/drivers/mfd/Makefile

> @@ -219,6 +219,7 @@ obj-$(CONFIG_MFD_RK808)		+= rk808.o

>  obj-$(CONFIG_MFD_RN5T618)	+= rn5t618.o

>  obj-$(CONFIG_MFD_SEC_CORE)	+= sec-core.o sec-irq.o

>  obj-$(CONFIG_MFD_SYSCON)	+= syscon.o

> +obj-$(CONFIG_MFD_SYSCON_I2C)	+= syscon-i2c.o

>  obj-$(CONFIG_MFD_LM3533)	+= lm3533-core.o lm3533-ctrlbank.o

>  obj-$(CONFIG_MFD_VEXPRESS_SYSREG)	+= vexpress-sysreg.o

>  obj-$(CONFIG_MFD_RETU)		+= retu-mfd.o

> diff --git a/drivers/mfd/syscon-i2c.c b/drivers/mfd/syscon-i2c.c

> new file mode 100644

> index 0000000000000..404e595d7646c

> --- /dev/null

> +++ b/drivers/mfd/syscon-i2c.c

> @@ -0,0 +1,89 @@

> +// SPDX-License-Identifier: GPL-2.0-or-later

> +/*

> + * System Control Driver accessed over I2C

> + *

> + * Copyright (C) 2020 Linaro Ltd.

> + *

> + * Author: Lee Jones <lee.jones@linaro.org>

> + */

> +

> +#include <linux/err.h>

> +#include <linux/i2c.h>

> +#include <linux/list.h>

> +#include <linux/mfd/syscon-i2c.h>

> +#include <linux/regmap.h>

> +

> +static DEFINE_SPINLOCK(syscon_i2c_list_slock);

> +static LIST_HEAD(syscon_i2c_list);

> +

> +struct syscon {

> +	struct device_node *np;


Note to self: struct i2c_client *client;

> +	struct regmap *regmap;

> +	struct list_head list;

> +};

> +

> +static const struct regmap_config syscon_i2c_regmap_config = {

> +	.reg_bits = 8,

> +	.val_bits = 8,

> +};

> +

> +static struct syscon *of_syscon_i2c_register(struct i2c_client *client)

> +{

> +	struct regmap_config syscon_config = syscon_i2c_regmap_config;

> +	struct device_node *np = client->dev.of_node;

> +	struct syscon *syscon;

> +	struct regmap *regmap;

> +	int ret;

> +

> +	syscon = devm_kzalloc(&client->dev, sizeof(*syscon), GFP_KERNEL);

> +	if (!syscon)

> +		return ERR_PTR(-ENOMEM);

> +

> +	syscon_config.name = of_node_full_name(np);


Note to self: Use dev_name();

> +	regmap = devm_regmap_init_i2c(client, &syscon_config);

> +	if (IS_ERR(regmap)) {

> +		dev_err(&client->dev, "Failed to initialise Regmap I2C\n");

> +		ret = PTR_ERR(regmap);

> +		return ERR_PTR(ret);

> +	}

> +

> +	syscon->regmap = regmap;

> +	syscon->np = np;

> +

> +	spin_lock(&syscon_i2c_list_slock);

> +	list_add_tail(&syscon->list, &syscon_i2c_list);

> +	spin_unlock(&syscon_i2c_list_slock);

> +

> +	return syscon;

> +}

> +

> +static struct regmap *i2c_device_node_get_regmap(struct i2c_client *client)


Note to self: i2c_client_get_regmap()

> +{

> +	struct device_node *np = client->dev.of_node;

> +	struct syscon *entry, *syscon = NULL;

> +

> +	spin_lock(&syscon_i2c_list_slock);

> +

> +	list_for_each_entry(entry, &syscon_i2c_list, list)

> +		if (entry->np == np) {

> +			syscon = entry;

> +			break;

> +		}

> +

> +	spin_unlock(&syscon_i2c_list_slock);

> +

> +	if (!syscon)

> +		syscon = of_syscon_i2c_register(client);

> +

> +	if (IS_ERR(syscon))

> +		return ERR_CAST(syscon);

> +

> +	return syscon->regmap;

> +}

> +

> +struct regmap *i2c_device_node_to_regmap(struct i2c_client *client)


Note to self: i2c_client_to_regmap()

> +{

> +	return i2c_device_node_get_regmap(client);

> +}

> +EXPORT_SYMBOL_GPL(i2c_device_node_to_regmap);

> diff --git a/include/linux/mfd/syscon-i2c.h b/include/linux/mfd/syscon-i2c.h

> new file mode 100644

> index 0000000000000..854e316fec93e

> --- /dev/null

> +++ b/include/linux/mfd/syscon-i2c.h

> @@ -0,0 +1,26 @@

> +/* SPDX-License-Identifier: GPL-2.0-or-later */

> +/*

> + * System Control Driver accessed via I2C

> + *

> + * Copyright (C) 2020 Linaro Ltd.

> + *

> + * Author: Lee Jones <lee.jones@linaro.org>

> + */

> +

> +#ifndef __LINUX_MFD_SYSCON_I2C_H__

> +#define __LINUX_MFD_SYSCON_I2C_H__

> +

> +#include <linux/err.h>

> +#include <linux/errno.h>

> +#include <linux/i2c.h>

> +

> +#ifdef CONFIG_MFD_SYSCON_I2C

> +extern struct regmap *i2c_device_node_to_regmap(struct i2c_client *client);

> +#else

> +static inline struct regmap *i2c_device_node_to_regmap(struct i2c_client *client)

> +{

> +	return ERR_PTR(-ENOTSUPP);

> +}

> +#endif

> +

> +#endif /* __LINUX_MFD_SYSCON_I2C_H__ */


-- 
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog
kernel test robot June 17, 2020, 2:24 a.m. | #2
Hi Lee,

I love your patch! Yet something to improve:

[auto build test ERROR on ljones-mfd/for-mfd-next]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Lee-Jones/mfd-Add-I2C-based-System-Configuaration-SYSCON-access/20200617-013251
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: h8300-allmodconfig (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=h8300 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>, old ones prefixed by <<):

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_byte_reg_read':
>> regmap-i2c.c:(.text+0x167): undefined reference to `i2c_smbus_read_byte_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_byte_reg_write':
>> regmap-i2c.c:(.text+0x1a7): undefined reference to `i2c_smbus_write_byte_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_word_reg_read':
>> regmap-i2c.c:(.text+0x1d3): undefined reference to `i2c_smbus_read_word_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_word_read_swapped':
regmap-i2c.c:(.text+0x211): undefined reference to `i2c_smbus_read_word_data'
h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_word_write_swapped':
>> regmap-i2c.c:(.text+0x25b): undefined reference to `i2c_smbus_write_word_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_smbus_word_reg_write':
regmap-i2c.c:(.text+0x285): undefined reference to `i2c_smbus_write_word_data'
h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_i2c_smbus_i2c_read':
>> regmap-i2c.c:(.text+0x2c3): undefined reference to `i2c_smbus_read_i2c_block_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_i2c_smbus_i2c_write':
>> regmap-i2c.c:(.text+0x30d): undefined reference to `i2c_smbus_write_i2c_block_data'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_i2c_read':
>> regmap-i2c.c:(.text+0x37d): undefined reference to `i2c_transfer'

h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_i2c_gather_write':
regmap-i2c.c:(.text+0x431): undefined reference to `i2c_transfer'
h8300-linux-ld: drivers/base/regmap/regmap-i2c.o: in function `regmap_i2c_write':
>> regmap-i2c.c:(.text+0x485): undefined reference to `i2c_transfer_buffer_flags'


---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

Patch

diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig
index 0a59249198d34..63ae78b92b086 100644
--- a/drivers/mfd/Kconfig
+++ b/drivers/mfd/Kconfig
@@ -1300,6 +1300,13 @@  config MFD_SYSCON
 	  Select this option to enable accessing system control registers
 	  via regmap.
 
+config MFD_SYSCON_I2C
+	bool "System Controller Register R/W Based on I2C Regmap"
+	select REGMAP_I2C
+	help
+	  Select this option to enable accessing system control registers
+	  via I2C using regmap.
+
 config MFD_DAVINCI_VOICECODEC
 	tristate
 	select MFD_CORE
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile
index f935d10cbf0fc..0aec1f42ac979 100644
--- a/drivers/mfd/Makefile
+++ b/drivers/mfd/Makefile
@@ -219,6 +219,7 @@  obj-$(CONFIG_MFD_RK808)		+= rk808.o
 obj-$(CONFIG_MFD_RN5T618)	+= rn5t618.o
 obj-$(CONFIG_MFD_SEC_CORE)	+= sec-core.o sec-irq.o
 obj-$(CONFIG_MFD_SYSCON)	+= syscon.o
+obj-$(CONFIG_MFD_SYSCON_I2C)	+= syscon-i2c.o
 obj-$(CONFIG_MFD_LM3533)	+= lm3533-core.o lm3533-ctrlbank.o
 obj-$(CONFIG_MFD_VEXPRESS_SYSREG)	+= vexpress-sysreg.o
 obj-$(CONFIG_MFD_RETU)		+= retu-mfd.o
diff --git a/drivers/mfd/syscon-i2c.c b/drivers/mfd/syscon-i2c.c
new file mode 100644
index 0000000000000..404e595d7646c
--- /dev/null
+++ b/drivers/mfd/syscon-i2c.c
@@ -0,0 +1,89 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * System Control Driver accessed over I2C
+ *
+ * Copyright (C) 2020 Linaro Ltd.
+ *
+ * Author: Lee Jones <lee.jones@linaro.org>
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/list.h>
+#include <linux/mfd/syscon-i2c.h>
+#include <linux/regmap.h>
+
+static DEFINE_SPINLOCK(syscon_i2c_list_slock);
+static LIST_HEAD(syscon_i2c_list);
+
+struct syscon {
+	struct device_node *np;
+	struct regmap *regmap;
+	struct list_head list;
+};
+
+static const struct regmap_config syscon_i2c_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 8,
+};
+
+static struct syscon *of_syscon_i2c_register(struct i2c_client *client)
+{
+	struct regmap_config syscon_config = syscon_i2c_regmap_config;
+	struct device_node *np = client->dev.of_node;
+	struct syscon *syscon;
+	struct regmap *regmap;
+	int ret;
+
+	syscon = devm_kzalloc(&client->dev, sizeof(*syscon), GFP_KERNEL);
+	if (!syscon)
+		return ERR_PTR(-ENOMEM);
+
+	syscon_config.name = of_node_full_name(np);
+
+	regmap = devm_regmap_init_i2c(client, &syscon_config);
+	if (IS_ERR(regmap)) {
+		dev_err(&client->dev, "Failed to initialise Regmap I2C\n");
+		ret = PTR_ERR(regmap);
+		return ERR_PTR(ret);
+	}
+
+	syscon->regmap = regmap;
+	syscon->np = np;
+
+	spin_lock(&syscon_i2c_list_slock);
+	list_add_tail(&syscon->list, &syscon_i2c_list);
+	spin_unlock(&syscon_i2c_list_slock);
+
+	return syscon;
+}
+
+static struct regmap *i2c_device_node_get_regmap(struct i2c_client *client)
+{
+	struct device_node *np = client->dev.of_node;
+	struct syscon *entry, *syscon = NULL;
+
+	spin_lock(&syscon_i2c_list_slock);
+
+	list_for_each_entry(entry, &syscon_i2c_list, list)
+		if (entry->np == np) {
+			syscon = entry;
+			break;
+		}
+
+	spin_unlock(&syscon_i2c_list_slock);
+
+	if (!syscon)
+		syscon = of_syscon_i2c_register(client);
+
+	if (IS_ERR(syscon))
+		return ERR_CAST(syscon);
+
+	return syscon->regmap;
+}
+
+struct regmap *i2c_device_node_to_regmap(struct i2c_client *client)
+{
+	return i2c_device_node_get_regmap(client);
+}
+EXPORT_SYMBOL_GPL(i2c_device_node_to_regmap);
diff --git a/include/linux/mfd/syscon-i2c.h b/include/linux/mfd/syscon-i2c.h
new file mode 100644
index 0000000000000..854e316fec93e
--- /dev/null
+++ b/include/linux/mfd/syscon-i2c.h
@@ -0,0 +1,26 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * System Control Driver accessed via I2C
+ *
+ * Copyright (C) 2020 Linaro Ltd.
+ *
+ * Author: Lee Jones <lee.jones@linaro.org>
+ */
+
+#ifndef __LINUX_MFD_SYSCON_I2C_H__
+#define __LINUX_MFD_SYSCON_I2C_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
+
+#ifdef CONFIG_MFD_SYSCON_I2C
+extern struct regmap *i2c_device_node_to_regmap(struct i2c_client *client);
+#else
+static inline struct regmap *i2c_device_node_to_regmap(struct i2c_client *client)
+{
+	return ERR_PTR(-ENOTSUPP);
+}
+#endif
+
+#endif /* __LINUX_MFD_SYSCON_I2C_H__ */