blob: 6c921820840756967b212c38fc0d50cf19f74d6b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From b0321721be50b80c03a51866a94fde4f94690e18 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3Date: Wed, 15 Jun 2022 21:42:59 +0200
4Subject: [PATCH] mtd: allow getting MTD device associated with a specific DT
5 node
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10MTD subsystem API allows interacting with MTD devices (e.g. reading,
11writing, handling bad blocks). So far a random driver could get MTD
12device only by its name (get_mtd_device_nm()). This change allows
13getting them also by a DT node.
14
15This API is required for drivers handling DT defined MTD partitions in a
16specific way (e.g. U-Boot (sub)partition with environment variables).
17
18Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
19Acked-by: Miquel Raynal <miquel.raynal@bootlin.com>
20Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
21---
22 drivers/mtd/mtdcore.c | 28 ++++++++++++++++++++++++++++
23 include/linux/mtd/mtd.h | 1 +
24 2 files changed, 29 insertions(+)
25
26--- a/drivers/mtd/mtdcore.c
27+++ b/drivers/mtd/mtdcore.c
28@@ -1069,6 +1069,34 @@ int __get_mtd_device(struct mtd_info *mt
29 EXPORT_SYMBOL_GPL(__get_mtd_device);
30
31 /**
32+ * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
33+ *
34+ * @np: device tree node
35+ */
36+struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
37+{
38+ struct mtd_info *mtd = NULL;
39+ struct mtd_info *tmp;
40+ int err;
41+
42+ mutex_lock(&mtd_table_mutex);
43+
44+ err = -EPROBE_DEFER;
45+ mtd_for_each_device(tmp) {
46+ if (mtd_get_of_node(tmp) == np) {
47+ mtd = tmp;
48+ err = __get_mtd_device(mtd);
49+ break;
50+ }
51+ }
52+
53+ mutex_unlock(&mtd_table_mutex);
54+
55+ return err ? ERR_PTR(err) : mtd;
56+}
57+EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
58+
59+/**
60 * get_mtd_device_nm - obtain a validated handle for an MTD device by
61 * device name
62 * @name: MTD device name to open
63--- a/include/linux/mtd/mtd.h
64+++ b/include/linux/mtd/mtd.h
65@@ -567,6 +567,7 @@ extern int mtd_device_unregister(struct
66 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
67 extern int __get_mtd_device(struct mtd_info *mtd);
68 extern void __put_mtd_device(struct mtd_info *mtd);
69+extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np);
70 extern struct mtd_info *get_mtd_device_nm(const char *name);
71 extern void put_mtd_device(struct mtd_info *mtd);
72