1 From b0321721be50b80c03a51866a94fde4f94690e18 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Wed, 15 Jun 2022 21:42:59 +0200
4 Subject: [PATCH] mtd: allow getting MTD device associated with a specific DT
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 MTD subsystem API allows interacting with MTD devices (e.g. reading,
11 writing, handling bad blocks). So far a random driver could get MTD
12 device only by its name (get_mtd_device_nm()). This change allows
13 getting them also by a DT node.
15 This API is required for drivers handling DT defined MTD partitions in a
16 specific way (e.g. U-Boot (sub)partition with environment variables).
18 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
19 Acked-by: Miquel Raynal <miquel.raynal@bootlin.com>
20 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
22 drivers/mtd/mtdcore.c | 28 ++++++++++++++++++++++++++++
23 include/linux/mtd/mtd.h | 1 +
24 2 files changed, 29 insertions(+)
26 --- a/drivers/mtd/mtdcore.c
27 +++ b/drivers/mtd/mtdcore.c
28 @@ -1236,6 +1236,34 @@ int __get_mtd_device(struct mtd_info *mt
29 EXPORT_SYMBOL_GPL(__get_mtd_device);
32 + * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
34 + * @np: device tree node
36 +struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
38 + struct mtd_info *mtd = NULL;
39 + struct mtd_info *tmp;
42 + mutex_lock(&mtd_table_mutex);
44 + err = -EPROBE_DEFER;
45 + mtd_for_each_device(tmp) {
46 + if (mtd_get_of_node(tmp) == np) {
48 + err = __get_mtd_device(mtd);
53 + mutex_unlock(&mtd_table_mutex);
55 + return err ? ERR_PTR(err) : mtd;
57 +EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
60 * get_mtd_device_nm - obtain a validated handle for an MTD device by
62 * @name: MTD device name to open
63 --- a/include/linux/mtd/mtd.h
64 +++ b/include/linux/mtd/mtd.h
65 @@ -682,6 +682,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);