summaryrefslogtreecommitdiffstats
path: root/drivers/misc/state.c
blob: 73356b45a0112b4950c7e53f54b0fb2adc20da07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2013 Sascha Hauer <s.hauer@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <common.h>
#include <driver.h>
#include <init.h>
#include <malloc.h>
#include <of.h>
#include <state.h>

#include <linux/err.h>

static int state_probe(struct device_d *dev)
{
	struct device_node *np = dev->device_node;
	struct device_node *partition_node;
	struct state *state;
	const char *alias;
	const char *backend_type = NULL;
	int len, ret;
	const char *of_path;
	char *path;

	if (!np)
		return -EINVAL;

	alias = of_alias_get(np);
	if (!alias)
		alias = np->name;

	state = state_new_from_node(alias, np);
	if (IS_ERR(state))
		return PTR_ERR(state);

	of_path = of_get_property(np, "backend", &len);
	if (!of_path) {
		ret = -ENODEV;
		goto out_release;
	}

	/* guess if of_path is a path, not a phandle */
	if (of_path[0] == '/' && len > 1) {
		ret = of_find_path(np, "backend", &path, 0);
	} else {

		partition_node = of_parse_phandle(np, "backend", 0);
		if (!partition_node)
			return -EINVAL;

		of_path = partition_node->full_name;
		ret = of_find_path_by_node(partition_node, &path, 0);
	}

	if (ret == -ENODEV)
		ret = -EPROBE_DEFER;
	if (ret)
		goto out_release;

	ret = of_property_read_string(np, "backend-type", &backend_type);
	if (ret) {
		goto out_free;
	} else if (!strcmp(backend_type, "raw")) {
		ret = state_backend_raw_file(state, of_path, path, 0, 0);
	} else if (!strcmp(backend_type, "dtb")) {
		ret = state_backend_dtb_file(state, of_path, path);
	} else {
		dev_warn(dev, "invalid backend type: %s\n", backend_type);
		ret = -ENODEV;
		goto out_free;
	}

	if (ret)
		goto out_free;

	dev_info(dev, "backend: %s, path: %s, of_path: %s\n", backend_type, path, of_path);
	free(path);

	return 0;

 out_free:
	free(path);
 out_release:
	state_release(state);
	return ret;
}

static __maybe_unused struct of_device_id state_ids[] = {
	{
		.compatible = "barebox,state",
	}, {
		/* sentinel */
	}
};

static struct driver_d state_driver = {
	.name = "state",
	.probe = state_probe,
	.of_compatible = DRV_OF_COMPAT(state_ids),
};
device_platform_driver(state_driver);