2 * System Control Driver
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
5 * Copyright (C) 2012 Linaro Ltd.
7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/err.h>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
25 static struct platform_driver syscon_driver;
28 struct regmap *regmap;
31 static int syscon_match_node(struct device *dev, void *data)
33 struct device_node *dn = data;
35 return (dev->of_node == dn) ? 1 : 0;
38 struct regmap *syscon_node_to_regmap(struct device_node *np)
40 struct syscon *syscon;
43 dev = driver_find_device(&syscon_driver.driver, NULL, np,
46 return ERR_PTR(-EPROBE_DEFER);
48 syscon = dev_get_drvdata(dev);
50 return syscon->regmap;
52 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
54 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
56 struct device_node *syscon_np;
57 struct regmap *regmap;
59 syscon_np = of_find_compatible_node(NULL, NULL, s);
61 return ERR_PTR(-ENODEV);
63 regmap = syscon_node_to_regmap(syscon_np);
64 of_node_put(syscon_np);
68 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
70 static int syscon_match_pdevname(struct device *dev, void *data)
72 return !strcmp(dev_name(dev), (const char *)data);
75 struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
78 struct syscon *syscon;
80 dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
81 syscon_match_pdevname);
83 return ERR_PTR(-EPROBE_DEFER);
85 syscon = dev_get_drvdata(dev);
87 return syscon->regmap;
89 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
91 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
94 struct device_node *syscon_np;
95 struct regmap *regmap;
98 syscon_np = of_parse_phandle(np, property, 0);
103 return ERR_PTR(-ENODEV);
105 regmap = syscon_node_to_regmap(syscon_np);
106 of_node_put(syscon_np);
110 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
112 static const struct of_device_id of_syscon_match[] = {
113 { .compatible = "syscon", },
117 static struct regmap_config syscon_regmap_config = {
123 static int syscon_probe(struct platform_device *pdev)
125 struct device *dev = &pdev->dev;
126 struct syscon *syscon;
127 struct resource *res;
130 syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 base = devm_ioremap(dev, res->start, resource_size(res));
142 syscon_regmap_config.max_register = res->end - res->start - 3;
143 syscon->regmap = devm_regmap_init_mmio(dev, base,
144 &syscon_regmap_config);
145 if (IS_ERR(syscon->regmap)) {
146 dev_err(dev, "regmap init failed\n");
147 return PTR_ERR(syscon->regmap);
150 platform_set_drvdata(pdev, syscon);
152 dev_dbg(dev, "regmap %pR registered\n", res);
157 static const struct platform_device_id syscon_ids[] = {
162 static struct platform_driver syscon_driver = {
165 .owner = THIS_MODULE,
166 .of_match_table = of_syscon_match,
168 .probe = syscon_probe,
169 .id_table = syscon_ids,
172 static int __init syscon_init(void)
174 return platform_driver_register(&syscon_driver);
176 postcore_initcall(syscon_init);
178 static void __exit syscon_exit(void)
180 platform_driver_unregister(&syscon_driver);
182 module_exit(syscon_exit);
184 MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
185 MODULE_DESCRIPTION("System Control driver");
186 MODULE_LICENSE("GPL v2");