mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Michael Riesch <michael.riesch@wolfvision.net>
To: barebox@lists.infradead.org
Cc: Michael Riesch <michael.riesch@wolfvision.net>
Subject: [PATCH 2/4] common: add wolfvision board code library
Date: Fri, 05 Apr 2024 16:04:43 +0200	[thread overview]
Message-ID: <20240405-feature-wolfvision-pf5-v1-2-e4a29c0e7fe1@wolfvision.net> (raw)
In-Reply-To: <20240405-feature-wolfvision-pf5-v1-0-e4a29c0e7fe1@wolfvision.net>

Add board code library for all WolfVision boards.

Signed-off-by: Michael Riesch <michael.riesch@wolfvision.net>
---
 common/boards/Kconfig              |   3 +
 common/boards/Makefile             |   1 +
 common/boards/wolfvision/Makefile  |   2 +
 common/boards/wolfvision/common.c  | 145 +++++++++++++++++++++++++++++++++++++
 include/boards/wolfvision/common.h |  32 ++++++++
 5 files changed, 183 insertions(+)

diff --git a/common/boards/Kconfig b/common/boards/Kconfig
index f6d4a56f88..a2a51155ea 100644
--- a/common/boards/Kconfig
+++ b/common/boards/Kconfig
@@ -14,3 +14,6 @@ config BOARD_PHYTEC_SOM_IMX8M_DETECTION
 config BOARD_TQ
 	select CRC_ITU_T
 	bool
+
+config BOARD_WOLFVISION
+	bool
diff --git a/common/boards/Makefile b/common/boards/Makefile
index 147c36643d..3f8ac57b2f 100644
--- a/common/boards/Makefile
+++ b/common/boards/Makefile
@@ -3,3 +3,4 @@
 obj-$(CONFIG_BOARD_QEMU_VIRT)	+= qemu-virt/
 obj-$(CONFIG_BOARD_PHYTEC_SOM_DETECTION) += phytec/
 obj-$(CONFIG_BOARD_TQ) += tq/
+obj-$(CONFIG_BOARD_WOLFVISION) += wolfvision/
diff --git a/common/boards/wolfvision/Makefile b/common/boards/wolfvision/Makefile
new file mode 100644
index 0000000000..b2be4b73f4
--- /dev/null
+++ b/common/boards/wolfvision/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-pbl-y += common.o
diff --git a/common/boards/wolfvision/common.c b/common/boards/wolfvision/common.c
new file mode 100644
index 0000000000..188931c24c
--- /dev/null
+++ b/common/boards/wolfvision/common.c
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Common board code functions WolfVision boards.
+ *
+ * Copyright (C) 2024 WolfVision GmbH.
+ */
+
+#include <common.h>
+#include <aiodev.h>
+#include <net.h>
+#include <state.h>
+
+#include <boards/wolfvision/common.h>
+
+#define WV_RK3568_HWID_TOLERANCE 50
+
+int wolfvision_apply_overlay(const struct wv_overlay *overlay, char **files)
+{
+	int ret;
+
+	if (overlay->filename) {
+		if (*files) {
+			char *old = *files;
+			*files = basprintf("%s %s", old, overlay->filename);
+			free(old);
+		} else {
+			*files = basprintf("%s", overlay->filename);
+		}
+	}
+
+	if (overlay->data) {
+		struct device_node *node =
+			of_unflatten_dtb(overlay->data, INT_MAX);
+
+		if (!node) {
+			pr_err("Cannot unflatten dtbo\n");
+			return -EINVAL;
+		}
+
+		ret = of_overlay_apply_tree(of_get_root_node(), node);
+
+		of_delete_node(node);
+
+		if (ret) {
+			pr_err("Cannot apply overlay: %s\n", strerror(-ret));
+			return ret;
+		}
+
+		of_clk_init();
+		of_probe();
+	}
+
+	return 0;
+}
+
+int wolfvision_register_ethaddr(void)
+{
+	struct device_node *eth0;
+	struct state *state;
+	char mac[ETH_ALEN];
+	int ret;
+
+	ret = of_device_ensure_probed_by_alias("state");
+	if (ret)
+		return ret;
+
+	state = state_by_name("state");
+	if (!state)
+		return -ENOENT;
+
+	ret = state_read_mac(state, "mac-address", mac);
+	if (ret)
+		return ret;
+
+	if (!is_valid_ether_addr(mac))
+		return -EINVAL;
+
+	eth0 = of_find_node_by_alias(of_get_root_node(), "ethernet0");
+	if (eth0)
+		of_eth_register_ethaddr(eth0, mac);
+
+	return 0;
+}
+
+int wolfvision_rk3568_get_hwid(int chan_idx)
+{
+	const int values[WV_RK3568_HWID_MAX] = {
+		0,    112,  225,  337,	450,  562,  675,  787,	900,
+		1012, 1125, 1237, 1350, 1462, 1575, 1687, 1800,
+	};
+	struct aiochannel *chan;
+	int ret, hwid, voltage;
+	char *chan_name;
+
+	chan_name = basprintf("saradc.in_value%d_mV", chan_idx);
+	chan = aiochannel_by_name(chan_name);
+	free(chan_name);
+	if (IS_ERR(chan))
+		return PTR_ERR(chan);
+
+	ret = aiochannel_get_value(chan, &voltage);
+	if (ret)
+		return ret;
+
+	for (hwid = 0; hwid < ARRAY_SIZE(values); hwid++)
+		if (abs(voltage - values[hwid]) < WV_RK3568_HWID_TOLERANCE)
+			return hwid;
+
+	return -EINVAL;
+};
+
+int wolfvision_rk3568_detect_hw(const struct wv_rk3568_extension *extensions,
+				int num_extensions, char **overlays)
+{
+	int i, hwid, ret;
+
+	ret = of_device_ensure_probed_by_alias("saradc");
+	if (ret)
+		return ret;
+
+	for (i = 0; i < num_extensions; i++) {
+		const struct wv_rk3568_extension *extension = &extensions[i];
+		const struct wv_overlay *overlay;
+
+		ret = wolfvision_rk3568_get_hwid(extension->adc_chan);
+		if (ret < 0) {
+			pr_warning("Could not retrieve %s HWID (%d)\n",
+				   extension->name, ret);
+			return ret;
+		}
+
+		hwid = ret;
+		overlay = &extension->overlays[hwid];
+		if (overlay->name) {
+			pr_info("Detected %s %s\n", overlay->name,
+				extension->name);
+			wolfvision_apply_overlay(overlay, overlays);
+		} else {
+			pr_warning("Detected unknown %s HWID %d\n",
+				   extension->name, hwid);
+		}
+	}
+
+	return 0;
+}
diff --git a/include/boards/wolfvision/common.h b/include/boards/wolfvision/common.h
new file mode 100644
index 0000000000..a9bf6acbbb
--- /dev/null
+++ b/include/boards/wolfvision/common.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Common board code functions WolfVision boards.
+ *
+ * Copyright (C) 2024 WolfVision GmbH.
+ */
+
+#ifndef _BOARDS_WOLFVISION_COMMON_H
+#define _BOARDS_WOLFVISION_COMMON_H
+
+#define WV_RK3568_HWID_MAX 17
+
+struct wv_overlay {
+	const char *name;
+	const char *filename;
+	const void *data;
+};
+
+struct wv_rk3568_extension {
+	int adc_chan;
+	const char *name;
+	const struct wv_overlay overlays[WV_RK3568_HWID_MAX];
+};
+
+int wolfvision_apply_overlay(const struct wv_overlay *overlay, char **files);
+
+int wolfvision_register_ethaddr(void);
+
+int wolfvision_rk3568_detect_hw(const struct wv_rk3568_extension *extensions,
+				int num_extensions, char **overlays);
+
+#endif /* _BOARDS_WOLFVISION_COMMON_H */

-- 
2.34.1




  parent reply	other threads:[~2024-04-05 14:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 14:04 [PATCH 0/4] arm: boards: add wolfvision pf5 mainboard Michael Riesch
2024-04-05 14:04 ` [PATCH 1/4] DNM: dts: arm64: rockchip: copy pf5 device tree from mainline Linux Michael Riesch
2024-04-05 17:02   ` Marco Felsch
2024-04-12 13:02     ` Michael Riesch
2024-04-12 13:33       ` Ahmad Fatoum
2024-04-12 15:46         ` Marco Felsch
2024-04-16 13:39         ` Sascha Hauer
2024-04-05 14:04 ` Michael Riesch [this message]
2024-04-08  6:27   ` [PATCH 2/4] common: add wolfvision board code library Sascha Hauer
2024-04-05 14:04 ` [PATCH 3/4] arm: dts: add common state for wolfvision boards Michael Riesch
2024-04-05 14:04 ` [PATCH 4/4] arm: boards: add wolfvision pf5 mainboard Michael Riesch

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=20240405-feature-wolfvision-pf5-v1-2-e4a29c0e7fe1@wolfvision.net \
    --to=michael.riesch@wolfvision.net \
    --cc=barebox@lists.infradead.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