MIPS: Fix logic errors in bitops.c
[linux-2.6.git] / drivers / mfd / syscon.c
blob61aea6381cdf37675595dd3a9a7173ea3fc69eb9
1 /*
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>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/of.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;
27 struct syscon {
28 struct device *dev;
29 void __iomem *base;
30 struct regmap *regmap;
33 static int syscon_match(struct device *dev, void *data)
35 struct syscon *syscon = dev_get_drvdata(dev);
36 struct device_node *dn = data;
38 return (syscon->dev->of_node == dn) ? 1 : 0;
41 struct regmap *syscon_node_to_regmap(struct device_node *np)
43 struct syscon *syscon;
44 struct device *dev;
46 dev = driver_find_device(&syscon_driver.driver, NULL, np,
47 syscon_match);
48 if (!dev)
49 return ERR_PTR(-EPROBE_DEFER);
51 syscon = dev_get_drvdata(dev);
53 return syscon->regmap;
55 EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
57 struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
59 struct device_node *syscon_np;
60 struct regmap *regmap;
62 syscon_np = of_find_compatible_node(NULL, NULL, s);
63 if (!syscon_np)
64 return ERR_PTR(-ENODEV);
66 regmap = syscon_node_to_regmap(syscon_np);
67 of_node_put(syscon_np);
69 return regmap;
71 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
73 struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
74 const char *property)
76 struct device_node *syscon_np;
77 struct regmap *regmap;
79 syscon_np = of_parse_phandle(np, property, 0);
80 if (!syscon_np)
81 return ERR_PTR(-ENODEV);
83 regmap = syscon_node_to_regmap(syscon_np);
84 of_node_put(syscon_np);
86 return regmap;
88 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
90 static const struct of_device_id of_syscon_match[] = {
91 { .compatible = "syscon", },
92 { },
95 static struct regmap_config syscon_regmap_config = {
96 .reg_bits = 32,
97 .val_bits = 32,
98 .reg_stride = 4,
101 static int syscon_probe(struct platform_device *pdev)
103 struct device *dev = &pdev->dev;
104 struct device_node *np = dev->of_node;
105 struct syscon *syscon;
106 struct resource res;
107 int ret;
109 if (!np)
110 return -ENOENT;
112 syscon = devm_kzalloc(dev, sizeof(struct syscon),
113 GFP_KERNEL);
114 if (!syscon)
115 return -ENOMEM;
117 syscon->base = of_iomap(np, 0);
118 if (!syscon->base)
119 return -EADDRNOTAVAIL;
121 ret = of_address_to_resource(np, 0, &res);
122 if (ret)
123 return ret;
125 syscon_regmap_config.max_register = res.end - res.start - 3;
126 syscon->regmap = devm_regmap_init_mmio(dev, syscon->base,
127 &syscon_regmap_config);
128 if (IS_ERR(syscon->regmap)) {
129 dev_err(dev, "regmap init failed\n");
130 return PTR_ERR(syscon->regmap);
133 syscon->dev = dev;
134 platform_set_drvdata(pdev, syscon);
136 dev_info(dev, "syscon regmap start 0x%x end 0x%x registered\n",
137 res.start, res.end);
139 return 0;
142 static int syscon_remove(struct platform_device *pdev)
144 struct syscon *syscon;
146 syscon = platform_get_drvdata(pdev);
147 iounmap(syscon->base);
148 platform_set_drvdata(pdev, NULL);
150 return 0;
153 static struct platform_driver syscon_driver = {
154 .driver = {
155 .name = "syscon",
156 .owner = THIS_MODULE,
157 .of_match_table = of_syscon_match,
159 .probe = syscon_probe,
160 .remove = syscon_remove,
163 static int __init syscon_init(void)
165 return platform_driver_register(&syscon_driver);
167 postcore_initcall(syscon_init);
169 static void __exit syscon_exit(void)
171 platform_driver_unregister(&syscon_driver);
173 module_exit(syscon_exit);
175 MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
176 MODULE_DESCRIPTION("System Control driver");
177 MODULE_LICENSE("GPL v2");