diff --git a/main/dtc/APKBUILD b/main/dtc/APKBUILD
index 7bf4ee1faf9afa55d4b85e03ebf75a636b1899fb..a5ac44f1fe3ac58fd898d11ade6ccd7b999020c3 100644
--- a/main/dtc/APKBUILD
+++ b/main/dtc/APKBUILD
@@ -11,6 +11,7 @@ checkdepends="coreutils"
 subpackages="$pkgname-dev libfdt py3-libfdt:py3_libfdt"
 source="https://kernel.org/pub/software/utils/dtc/dtc-$pkgver.tar.xz
 	install-pylibfdt-to-destdir.patch
+	pylibfdt-fix-with-Python-3.10.patch
 	"
 
 prepare() {
@@ -45,4 +46,5 @@ py3_libfdt() {
 sha512sums="
 26cd351ddca411ab96b93ac3e763f817f9f8a80ca66a8707e1077f771ed8e7e04c01f321ab8ab27b2f9826d9d438483fe3156401493bfd29cef3cc71a1414568  dtc-1.6.1.tar.xz
 1c43dfae8d15861c4f0170f0df9ba63d197ab3be50e39fe5748e547074c52dc7b042ca5e9c64d3d4bb0a8e8cde8df7e71660cde93e9512923405c38debcaa01c  install-pylibfdt-to-destdir.patch
+1c7d2b5f7dcb8aed38a1b685e3be30497988933c2149a169b20fb2df53c44b8584cfdb9bad695a5d357598e33bf85cddb38397477862d46e2de69e46fc53556a  pylibfdt-fix-with-Python-3.10.patch
 "
diff --git a/main/dtc/pylibfdt-fix-with-Python-3.10.patch b/main/dtc/pylibfdt-fix-with-Python-3.10.patch
new file mode 100644
index 0000000000000000000000000000000000000000..c6b9298d84d1f74aaafd316cce87c692c67572f2
--- /dev/null
+++ b/main/dtc/pylibfdt-fix-with-Python-3.10.patch
@@ -0,0 +1,58 @@
+From 383e148b70a47ab15f97a19bb999d54f9c3e810f Mon Sep 17 00:00:00 2001
+From: Ross Burton <ross.burton@arm.com>
+Date: Thu, 11 Nov 2021 16:05:36 +0000
+Subject: pylibfdt: fix with Python 3.10
+
+Since Python 2.5 the argument parsing functions when parsing expressions
+such as s# (string plus length) expect the length to be an int or a
+ssize_t, depending on whether PY_SSIZE_T_CLEAN is defined or not.
+
+Python 3.8 deprecated the use of int, and with Python 3.10 this symbol
+must be defined and ssize_t used[1].
+
+Define the magic symbol when building the extension, and cast the ints
+from the libfdt API to ssize_t as appropriate.
+
+[1] https://docs.python.org/3.10/whatsnew/3.10.html#id2
+
+Signed-off-by: Ross Burton <ross.burton@arm.com>
+Message-Id: <20211111160536.2516573-1-ross.burton@arm.com>
+[dwg: Adjust for new location of setup.py]
+Tested-by: Rob Herring <robh@kernel.org>
+Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
+---
+ pylibfdt/libfdt.i | 4 ++--
+ setup.py          | 1 +
+ 2 files changed, 3 insertions(+), 2 deletions(-)
+
+diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
+index 51ee801..075ef70 100644
+--- a/pylibfdt/libfdt.i
++++ b/pylibfdt/libfdt.i
+@@ -1044,9 +1044,9 @@ typedef uint32_t fdt32_t;
+ 		$result = Py_None;
+ 	else
+         %#if PY_VERSION_HEX >= 0x03000000
+-            $result = Py_BuildValue("y#", $1, *arg4);
++            $result = Py_BuildValue("y#", $1, (Py_ssize_t)*arg4);
+         %#else
+-            $result = Py_BuildValue("s#", $1, *arg4);
++            $result = Py_BuildValue("s#", $1, (Py_ssize_t)*arg4);
+         %#endif
+ }
+ 
+diff --git a/setup.py b/setup.py
+index 4b07be9..0a0daf1 100755
+--- a/pylibfdt/setup.py
++++ b/pylibfdt/setup.py
+@@ -42,6 +42,7 @@ top_builddir = get_top_builddir()
+ libfdt_module = Extension(
+     '_libfdt',
+     sources=[os.path.join(srcdir, 'libfdt.i')],
++    define_macros=[('PY_SSIZE_T_CLEAN', None)],
+     include_dirs=[os.path.join(srcdir, '../libfdt')],
+     libraries=['fdt'],
+     library_dirs=[os.path.join(top_builddir, 'libfdt')],
+-- 
+cgit 1.2.3-1.el7
+