mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] common: Add autoenable for components
@ 2017-10-27 14:37 Daniel Schultz
  2017-10-27 14:37 ` [PATCH 2/3] ARM: dts: AM335x: Add dummy i2c nodes Daniel Schultz
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Daniel Schultz @ 2017-10-27 14:37 UTC (permalink / raw)
  To: barebox

This patch adds an API to automatically enable either hardware components
with existing device drivers or i2c clients. All functions take a device
tree path to find the hardware and will fix up the node status in the
kernel device tree, if it's accessible.

Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
---
 common/Kconfig       |   9 +++++
 common/Makefile      |   1 +
 common/autoenable.c  | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/autoenable.h |  21 ++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 common/autoenable.c
 create mode 100644 include/autoenable.h

diff --git a/common/Kconfig b/common/Kconfig
index 57418ca..8d2a3e6 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -712,6 +712,15 @@ config CONSOLE_NONE
 
 endchoice
 
+config KERNEL_AUTOENABLE
+	bool
+	prompt "Autoenable of components"
+	help
+	  Say Y to unlock an API for automatically enable either hardware
+	  components with existing device drivers or i2c clients. All functions
+	  take a device tree path to find the hardware and will fix up the node
+	  status in the kernel device tree, if it's accessible.
+
 choice
 	prompt "Console activation strategy"
 	depends on CONSOLE_FULL
diff --git a/common/Makefile b/common/Makefile
index 8cd0ab3..4d7b0f9 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_FLEXIBLE_BOOTARGS)	+= bootargs.o
 obj-$(CONFIG_GLOBALVAR)		+= globalvar.o
 obj-$(CONFIG_GREGORIAN_CALENDER) += date.o
 obj-$(CONFIG_KALLSYMS)		+= kallsyms.o
+obj-$(CONFIG_KERNEL_AUTOENABLE) += autoenable.o
 obj-$(CONFIG_MALLOC_DLMALLOC)	+= dlmalloc.o
 obj-$(CONFIG_MALLOC_TLSF)	+= tlsf_malloc.o tlsf.o
 obj-$(CONFIG_MALLOC_DUMMY)	+= dummy_malloc.o
diff --git a/common/autoenable.c b/common/autoenable.c
new file mode 100644
index 0000000..be76942
--- /dev/null
+++ b/common/autoenable.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2017 PHYTEC Messtechnik GmbH,
+ * Author: Daniel Schultz <d.schultz@phytec.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/err.h>
+#include <of.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <i2c/i2c.h>
+
+/**
+ * autoenable_device_by_path() - Autoenable a device by a device tree path
+ * @param path Device tree path up from the root to the device
+ * @return 0 on success, -enodev on failure. If no device found in the device
+ * tree.
+ *
+ * This function will search for a device and will enable it in the kernel
+ * device tree, if it exists and is loaded.
+ */
+int autoenable_device_by_path(char *path)
+{
+	struct device_d *device;
+	struct device_node *node;
+	int ret;
+
+	node = of_find_node_by_name(NULL, path);
+	if (!node)
+		node = of_find_node_by_path(path);
+
+	if (!node)
+		return -ENODEV;
+
+	device = of_find_device_by_node(node);
+	if (!device)
+		return -ENODEV;
+
+	ret = of_register_set_status_fixup(path, 1);
+	if (!ret)
+		printf("autoenabled %s\n", device->name);
+	return ret;
+}
+
+/**
+ * autoenable_i2c_by_path - Autoenable a i2c client by a device tree path
+ * @param path Device tree path up from the root to the i2c client
+ * @return 0 on success, -enodev on failure. If no i2c client found in the i2c
+ * device tree.
+ *
+ * This function will search for a i2c client, tries to write to the client and
+ * will enable it in the kernel device tree, if it exists and is accessible.
+ */
+int autoenable_i2c_by_path(char *path)
+{
+	struct device_node *node;
+	struct i2c_adapter *i2c_adapter;
+	struct i2c_msg msg;
+	char data[1] = {0x0};
+	int addr;
+	const __be32 *ip;
+	int ret;
+
+	node = of_find_node_by_name(NULL, path);
+	if (!node)
+		node = of_find_node_by_path(path);
+	if (!node)
+		return -ENODEV;
+	if (!node->parent)
+		return -ENODEV;
+
+	ip = of_get_property(node, "reg", NULL);
+	if (!ip)
+		return -ENODEV;
+	addr = be32_to_cpup(ip);
+
+	i2c_adapter = of_find_i2c_adapter_by_node(node->parent);
+	if (!i2c_adapter)
+		return -ENODEV;
+
+	msg.buf = data;
+	msg.addr = addr;
+	msg.len = 1;
+
+	/* Try to communicate with the i2c client */
+	ret = i2c_transfer(i2c_adapter, &msg, 1);
+	if (ret == -EREMOTEIO) {
+		return -ENODEV;
+	}
+	if (ret < 1) {
+		printf("failed to autoenable i2c device on address 0x%x with %i\n",
+								addr, ret);
+		return ret;
+	}
+
+	ret = of_register_set_status_fixup(path, 1);
+	if (!ret)
+		printf("autoenabled i2c device %s\n", node->name);
+
+	return ret;
+}
diff --git a/include/autoenable.h b/include/autoenable.h
new file mode 100644
index 0000000..26811b0
--- /dev/null
+++ b/include/autoenable.h
@@ -0,0 +1,21 @@
+#ifndef __AUTOENABLE_H
+#define __AUTOENABLE_H
+
+#include <linux/err.h>
+
+#ifdef CONFIG_KERNEL_AUTOENABLE
+int autoenable_device_by_path(char *path);
+int autoenable_i2c_by_path(char *path);
+#else
+inline int autoenable_device_by_path(char *path)
+{
+	return -EINVAL;
+}
+
+inline int autoenable_i2c_by_path(char *path)
+{
+	return -EINVAL;
+}
+#endif /* CONFIG_KERNEL_AUTOENABLE */
+
+#endif /* __AUTOENABLE_H */
-- 
2.7.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] ARM: dts: AM335x: Add dummy i2c nodes
  2017-10-27 14:37 [PATCH 1/3] common: Add autoenable for components Daniel Schultz
@ 2017-10-27 14:37 ` Daniel Schultz
  2017-10-27 14:37 ` [PATCH 3/3] ARM: phytec-som-am335x: Add autoenable Daniel Schultz
  2017-11-01  8:44 ` [PATCH 1/3] common: Add autoenable for components Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Schultz @ 2017-10-27 14:37 UTC (permalink / raw)
  To: barebox

These i2c nodes are needed for autoenable of i2c clients, because
the autoenable API searches for device tree nodes to get the client
address.

Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
---
 arch/arm/dts/am335x-phytec-phycore-som.dtsi | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/arch/arm/dts/am335x-phytec-phycore-som.dtsi b/arch/arm/dts/am335x-phytec-phycore-som.dtsi
index dbc6424..cf2eef3 100644
--- a/arch/arm/dts/am335x-phytec-phycore-som.dtsi
+++ b/arch/arm/dts/am335x-phytec-phycore-som.dtsi
@@ -164,6 +164,20 @@
 		pagesize = <32>;
 		reg = <0x52>;
 	};
+
+	/* The following i2c nodes are for the autoenable */
+	i2c_tmp102: temp@4b {
+		compatible = "ti,tmp102";
+		reg = <0x4b>;
+		status = "disabled";
+	};
+
+	i2c_rtc: rtc@68 {
+		compatible = "rv4162";
+		reg = <0x68>;
+		status = "disabled";
+	};
+
 };
 
 &mmc1 {
-- 
2.7.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] ARM: phytec-som-am335x: Add autoenable
  2017-10-27 14:37 [PATCH 1/3] common: Add autoenable for components Daniel Schultz
  2017-10-27 14:37 ` [PATCH 2/3] ARM: dts: AM335x: Add dummy i2c nodes Daniel Schultz
@ 2017-10-27 14:37 ` Daniel Schultz
  2017-11-01  8:44 ` [PATCH 1/3] common: Add autoenable for components Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Schultz @ 2017-10-27 14:37 UTC (permalink / raw)
  To: barebox

Add autoenable for components, which can be populated on an AM335x
phyCORE SoM.

Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
---
 arch/arm/boards/phytec-som-am335x/board.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/arm/boards/phytec-som-am335x/board.c b/arch/arm/boards/phytec-som-am335x/board.c
index 34d4df1..5d13471 100644
--- a/arch/arm/boards/phytec-som-am335x/board.c
+++ b/arch/arm/boards/phytec-som-am335x/board.c
@@ -26,6 +26,7 @@
 #include <io.h>
 #include <linux/sizes.h>
 #include <envfs.h>
+#include <autoenable.h>
 #include <state.h>
 #include <asm/armlinux.h>
 #include <generated/mach-types.h>
@@ -136,6 +137,17 @@ static int physom_devices_init(void)
 		}
 	}
 
+	/* Enable NAND */
+	autoenable_device_by_path("/ocp/gpmc@50000000");
+	/* Enable eMMC */
+	autoenable_device_by_path("/ocp/mmc@481d8000");
+	/* Enable SPI NOR */
+	autoenable_device_by_path("/ocp/spi@48030000/m25p80@0");
+
+	autoenable_i2c_by_path("/ocp/i2c@44e0b000/temp@4b");
+	autoenable_i2c_by_path("/ocp/i2c@44e0b000/eeprom@52");
+	autoenable_i2c_by_path("/ocp/i2c@44e0b000/rtc@68");
+
 	if (IS_ENABLED(CONFIG_SHELL_NONE))
 		return am33xx_of_register_bootdevice();
 
-- 
2.7.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] common: Add autoenable for components
  2017-10-27 14:37 [PATCH 1/3] common: Add autoenable for components Daniel Schultz
  2017-10-27 14:37 ` [PATCH 2/3] ARM: dts: AM335x: Add dummy i2c nodes Daniel Schultz
  2017-10-27 14:37 ` [PATCH 3/3] ARM: phytec-som-am335x: Add autoenable Daniel Schultz
@ 2017-11-01  8:44 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2017-11-01  8:44 UTC (permalink / raw)
  To: Daniel Schultz; +Cc: barebox

On Fri, Oct 27, 2017 at 04:37:39PM +0200, Daniel Schultz wrote:
> This patch adds an API to automatically enable either hardware components
> with existing device drivers or i2c clients. All functions take a device
> tree path to find the hardware and will fix up the node status in the
> kernel device tree, if it's accessible.
> 
> Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
> ---
>  common/Kconfig       |   9 +++++
>  common/Makefile      |   1 +
>  common/autoenable.c  | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  include/autoenable.h |  21 ++++++++++
>  4 files changed, 140 insertions(+)
>  create mode 100644 common/autoenable.c
>  create mode 100644 include/autoenable.h
> 
> diff --git a/common/Kconfig b/common/Kconfig
> index 57418ca..8d2a3e6 100644
> --- a/common/Kconfig
> +++ b/common/Kconfig
> @@ -712,6 +712,15 @@ config CONSOLE_NONE
>  
>  endchoice
>  
> +config KERNEL_AUTOENABLE
> +	bool
> +	prompt "Autoenable of components"
> +	help
> +	  Say Y to unlock an API for automatically enable either hardware
> +	  components with existing device drivers or i2c clients. All functions
> +	  take a device tree path to find the hardware and will fix up the node
> +	  status in the kernel device tree, if it's accessible.

I don't think we need an extra option for this. Just compile it
unconditionally. The code will be dropped if unused anyway, and
if it's used we probably do not want to fall back to the no-op
inline wrappers.

> +
>  choice
>  	prompt "Console activation strategy"
>  	depends on CONSOLE_FULL
> diff --git a/common/Makefile b/common/Makefile
> index 8cd0ab3..4d7b0f9 100644
> --- a/common/Makefile
> +++ b/common/Makefile
> @@ -31,6 +31,7 @@ obj-$(CONFIG_FLEXIBLE_BOOTARGS)	+= bootargs.o
>  obj-$(CONFIG_GLOBALVAR)		+= globalvar.o
>  obj-$(CONFIG_GREGORIAN_CALENDER) += date.o
>  obj-$(CONFIG_KALLSYMS)		+= kallsyms.o
> +obj-$(CONFIG_KERNEL_AUTOENABLE) += autoenable.o
>  obj-$(CONFIG_MALLOC_DLMALLOC)	+= dlmalloc.o
>  obj-$(CONFIG_MALLOC_TLSF)	+= tlsf_malloc.o tlsf.o
>  obj-$(CONFIG_MALLOC_DUMMY)	+= dummy_malloc.o
> diff --git a/common/autoenable.c b/common/autoenable.c

common/oftree.c would be a good place for the code.

> new file mode 100644
> index 0000000..be76942
> --- /dev/null
> +++ b/common/autoenable.c
> @@ -0,0 +1,109 @@
> +/*
> + * Copyright (C) 2017 PHYTEC Messtechnik GmbH,
> + * Author: Daniel Schultz <d.schultz@phytec.de>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * version 2 as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + */
> +
> +#include <linux/err.h>
> +#include <of.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <i2c/i2c.h>
> +
> +/**
> + * autoenable_device_by_path() - Autoenable a device by a device tree path
> + * @param path Device tree path up from the root to the device
> + * @return 0 on success, -enodev on failure. If no device found in the device
> + * tree.
> + *
> + * This function will search for a device and will enable it in the kernel
> + * device tree, if it exists and is loaded.
> + */
> +int autoenable_device_by_path(char *path)

This function and the other one should get a of_ prefix.

> +{
> +	struct device_d *device;
> +	struct device_node *node;
> +	int ret;
> +
> +	node = of_find_node_by_name(NULL, path);
> +	if (!node)
> +		node = of_find_node_by_path(path);
> +
> +	if (!node)
> +		return -ENODEV;
> +
> +	device = of_find_device_by_node(node);
> +	if (!device)
> +		return -ENODEV;
> +
> +	ret = of_register_set_status_fixup(path, 1);
> +	if (!ret)
> +		printf("autoenabled %s\n", device->name);
> +	return ret;
> +}

I'm not sure this function actually does what you want to archieve. From
the usage of the function in patch 3/3 it seems you want to enable for
example the NAND controller in the kernel device tree if you find a board
with NAND chip equipped, right? To do this you want to have a function
which enables a device in the kernel device tree if it's enabled in the
barebox device tree, but I don't think you want to depend on the driver
actually being enabled in barebox. What if you find a board with NAND
chip equipped but the driver is disabled in barebox to safe some space?


> +
> +/**
> + * autoenable_i2c_by_path - Autoenable a i2c client by a device tree path
> + * @param path Device tree path up from the root to the i2c client
> + * @return 0 on success, -enodev on failure. If no i2c client found in the i2c
> + * device tree.
> + *
> + * This function will search for a i2c client, tries to write to the client and
> + * will enable it in the kernel device tree, if it exists and is accessible.
> + */
> +int autoenable_i2c_by_path(char *path)
> +{

I don't like that this function has the same name pattern as the function
above, but does something different. The above function does something
when it encounters a device which has a driver, but this function
actually checks for physical presence of a device.

> +	struct device_node *node;
> +	struct i2c_adapter *i2c_adapter;
> +	struct i2c_msg msg;
> +	char data[1] = {0x0};
> +	int addr;
> +	const __be32 *ip;
> +	int ret;
> +
> +	node = of_find_node_by_name(NULL, path);
> +	if (!node)
> +		node = of_find_node_by_path(path);
> +	if (!node)
> +		return -ENODEV;
> +	if (!node->parent)
> +		return -ENODEV;
> +
> +	ip = of_get_property(node, "reg", NULL);
> +	if (!ip)
> +		return -ENODEV;
> +	addr = be32_to_cpup(ip);

We have of_property_read_u32() for this.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-11-01  8:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-27 14:37 [PATCH 1/3] common: Add autoenable for components Daniel Schultz
2017-10-27 14:37 ` [PATCH 2/3] ARM: dts: AM335x: Add dummy i2c nodes Daniel Schultz
2017-10-27 14:37 ` [PATCH 3/3] ARM: phytec-som-am335x: Add autoenable Daniel Schultz
2017-11-01  8:44 ` [PATCH 1/3] common: Add autoenable for components Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox