summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/syscon.c
blob: 55cc34ff566a4ad2212ce0ffee636d831ae0bc41 (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
112
113
114
115
116
117
118
/* System Control Driver
 *
 * Based on linux driver by:
 *  Copyright (C) 2012 Freescale Semiconductor, Inc.
 *  Copyright (C) 2012 Linaro Ltd.
 *  Author: Dong Aisheng <dong.aisheng@linaro.org>
 *
 * 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.
 */

#include <io.h>
#include <init.h>
#include <common.h>
#include <driver.h>
#include <malloc.h>
#include <xfuncs.h>

#include <linux/err.h>

#include <mfd/syscon.h>

struct syscon {
	void __iomem *base;
};

void __iomem *syscon_base_lookup_by_pdevname(const char *s)
{
	struct syscon *syscon;
	struct device_d *dev;

	for_each_device(dev) {
		if (!strcmp(dev_name(dev), s)) {
			syscon = dev->priv;
			return syscon->base;
		}
	}

	return ERR_PTR(-ENODEV);
}

void __iomem *syscon_base_lookup_by_phandle(struct device_node *np,
					    const char *property)
{
	struct device_node *node;
	struct syscon *syscon;
	struct device_d *dev;

	node = of_parse_phandle(np, property, 0);
	if (!node)
		return ERR_PTR(-ENODEV);

	dev = of_find_device_by_node(node);
	if (!dev)
		return ERR_PTR(-ENODEV);

	syscon = dev->priv;

	return syscon->base;
}

static int syscon_probe(struct device_d *dev)
{
	struct syscon *syscon;
	struct resource *res;

	syscon = xzalloc(sizeof(struct syscon));
	if (!syscon)
		return -ENOMEM;

	res = dev_get_resource(dev, IORESOURCE_MEM, 0);
	if (!res) {
		free(syscon);
		return -ENOENT;
	}

	res = request_iomem_region(dev_name(dev), res->start, res->end);
	if (!res) {
		free(syscon);
		return -EBUSY;
	}

	syscon->base = (void __iomem *)res->start;
	dev->priv = syscon;

	dev_dbg(dev, "map 0x%x-0x%x registered\n", res->start, res->end);

	return 0;
}

static struct platform_device_id syscon_ids[] = {
	{ "syscon", },
	{ }
};

static struct of_device_id of_syscon_match[] = {
	{ .compatible = "syscon" },
	{ },
};

static struct driver_d syscon_driver = {
	.name		= "syscon",
	.probe		= syscon_probe,
	.id_table	= syscon_ids,
	.of_compatible	= DRV_OF_COMPAT(of_syscon_match),
};

static int __init syscon_init(void)
{
	return platform_driver_register(&syscon_driver);
}
core_initcall(syscon_init);

MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
MODULE_DESCRIPTION("System Control driver");
MODULE_LICENSE("GPL v2");