acef46f2b9145d53d69bb600e1bb837099db9ca4
[openwrt/staging/jow.git] /
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
5 node
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
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.
14
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).
17
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>
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 @@ -1229,6 +1229,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 @@ -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);
72