1 From bb2192123ec70470d6ea33f138846b175403a968 Mon Sep 17 00:00:00 2001
2 From: Brian Norris <computersforpeace@gmail.com>
3 Date: Thu, 4 Jan 2018 08:05:33 +0100
4 Subject: [PATCH] mtd: partitions: add of_match_table parser matching
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Partition parsers can now provide an of_match_table to enable
10 flash<-->parser matching via device tree as documented in the
13 It works by looking for a matching parser for every string in the
14 "compatibility" property (starting with the most specific one).
16 This support is currently limited to built-in parsers as it uses
17 request_module() and friends. This should be sufficient for most cases
18 though as compiling parsers as modules isn't a common choice.
20 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
21 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
22 Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
24 drivers/mtd/mtdpart.c | 59 ++++++++++++++++++++++++++++++++++++++++++
25 include/linux/mtd/partitions.h | 1 +
26 2 files changed, 60 insertions(+)
28 --- a/drivers/mtd/mtdpart.c
29 +++ b/drivers/mtd/mtdpart.c
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/err.h>
34 +#include <linux/of.h>
38 @@ -886,6 +887,45 @@ static int mtd_part_do_parse(struct mtd_
42 + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
44 + * @compat: compatible string describing partitions in a device tree
46 + * MTD parsers can specify supported partitions by providing a table of
47 + * compatibility strings. This function finds a parser that advertises support
48 + * for a passed value of "compatible".
50 +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
52 + struct mtd_part_parser *p, *ret = NULL;
54 + spin_lock(&part_parser_lock);
56 + list_for_each_entry(p, &part_parsers, list) {
57 + const struct of_device_id *matches;
59 + matches = p->of_match_table;
63 + for (; matches->compatible[0]; matches++) {
64 + if (!strcmp(matches->compatible, compat) &&
65 + try_module_get(p->owner)) {
75 + spin_unlock(&part_parser_lock);
81 * parse_mtd_partitions - parse MTD partitions
82 * @master: the master partition (describes whole MTD device)
83 * @types: names of partition parsers to try or %NULL
84 @@ -911,8 +951,27 @@ int parse_mtd_partitions(struct mtd_info
85 struct mtd_part_parser_data *data)
87 struct mtd_part_parser *parser;
88 + struct device_node *np;
89 + struct property *prop;
93 + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
94 + of_property_for_each_string(np, "compatible", prop, compat) {
95 + parser = mtd_part_get_compatible_parser(compat);
98 + ret = mtd_part_do_parse(parser, master, pparts, data);
103 + mtd_part_parser_put(parser);
104 + if (ret < 0 && !err)
110 types = default_mtd_part_types;
112 --- a/include/linux/mtd/partitions.h
113 +++ b/include/linux/mtd/partitions.h
114 @@ -77,6 +77,7 @@ struct mtd_part_parser {
115 struct list_head list;
116 struct module *owner;
118 + const struct of_device_id *of_match_table;
119 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
120 struct mtd_part_parser_data *);
121 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);