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>
18 #include <linux/list.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_data/syscon.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/syscon.h>
26 #include <linux/slab.h>
28 static struct platform_driver syscon_driver
;
30 static DEFINE_SPINLOCK(syscon_list_slock
);
31 static LIST_HEAD(syscon_list
);
34 struct device_node
*np
;
35 struct regmap
*regmap
;
36 struct list_head list
;
39 static struct regmap_config syscon_regmap_config
= {
45 static struct syscon
*of_syscon_register(struct device_node
*np
)
47 struct syscon
*syscon
;
48 struct regmap
*regmap
;
51 struct regmap_config syscon_config
= syscon_regmap_config
;
53 if (!of_device_is_compatible(np
, "syscon"))
54 return ERR_PTR(-EINVAL
);
56 syscon
= kzalloc(sizeof(*syscon
), GFP_KERNEL
);
58 return ERR_PTR(-ENOMEM
);
60 base
= of_iomap(np
, 0);
66 /* Parse the device's DT node for an endianness specification */
67 if (of_property_read_bool(np
, "big-endian"))
68 syscon_config
.val_format_endian
= REGMAP_ENDIAN_BIG
;
69 else if (of_property_read_bool(np
, "little-endian"))
70 syscon_config
.val_format_endian
= REGMAP_ENDIAN_LITTLE
;
72 regmap
= regmap_init_mmio(NULL
, base
, &syscon_config
);
74 pr_err("regmap init failed\n");
75 ret
= PTR_ERR(regmap
);
79 syscon
->regmap
= regmap
;
82 spin_lock(&syscon_list_slock
);
83 list_add_tail(&syscon
->list
, &syscon_list
);
84 spin_unlock(&syscon_list_slock
);
95 struct regmap
*syscon_node_to_regmap(struct device_node
*np
)
97 struct syscon
*entry
, *syscon
= NULL
;
99 spin_lock(&syscon_list_slock
);
101 list_for_each_entry(entry
, &syscon_list
, list
)
102 if (entry
->np
== np
) {
107 spin_unlock(&syscon_list_slock
);
110 syscon
= of_syscon_register(np
);
113 return ERR_CAST(syscon
);
115 return syscon
->regmap
;
117 EXPORT_SYMBOL_GPL(syscon_node_to_regmap
);
119 struct regmap
*syscon_regmap_lookup_by_compatible(const char *s
)
121 struct device_node
*syscon_np
;
122 struct regmap
*regmap
;
124 syscon_np
= of_find_compatible_node(NULL
, NULL
, s
);
126 return ERR_PTR(-ENODEV
);
128 regmap
= syscon_node_to_regmap(syscon_np
);
129 of_node_put(syscon_np
);
133 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible
);
135 static int syscon_match_pdevname(struct device
*dev
, void *data
)
137 return !strcmp(dev_name(dev
), (const char *)data
);
140 struct regmap
*syscon_regmap_lookup_by_pdevname(const char *s
)
143 struct syscon
*syscon
;
145 dev
= driver_find_device(&syscon_driver
.driver
, NULL
, (void *)s
,
146 syscon_match_pdevname
);
148 return ERR_PTR(-EPROBE_DEFER
);
150 syscon
= dev_get_drvdata(dev
);
152 return syscon
->regmap
;
154 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname
);
156 struct regmap
*syscon_regmap_lookup_by_phandle(struct device_node
*np
,
157 const char *property
)
159 struct device_node
*syscon_np
;
160 struct regmap
*regmap
;
163 syscon_np
= of_parse_phandle(np
, property
, 0);
168 return ERR_PTR(-ENODEV
);
170 regmap
= syscon_node_to_regmap(syscon_np
);
171 of_node_put(syscon_np
);
175 EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle
);
177 static int syscon_probe(struct platform_device
*pdev
)
179 struct device
*dev
= &pdev
->dev
;
180 struct syscon_platform_data
*pdata
= dev_get_platdata(dev
);
181 struct syscon
*syscon
;
182 struct resource
*res
;
185 syscon
= devm_kzalloc(dev
, sizeof(*syscon
), GFP_KERNEL
);
189 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
193 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
197 syscon_regmap_config
.max_register
= res
->end
- res
->start
- 3;
199 syscon_regmap_config
.name
= pdata
->label
;
200 syscon
->regmap
= devm_regmap_init_mmio(dev
, base
,
201 &syscon_regmap_config
);
202 if (IS_ERR(syscon
->regmap
)) {
203 dev_err(dev
, "regmap init failed\n");
204 return PTR_ERR(syscon
->regmap
);
207 platform_set_drvdata(pdev
, syscon
);
209 dev_dbg(dev
, "regmap %pR registered\n", res
);
214 static const struct platform_device_id syscon_ids
[] = {
219 static struct platform_driver syscon_driver
= {
223 .probe
= syscon_probe
,
224 .id_table
= syscon_ids
,
227 static int __init
syscon_init(void)
229 return platform_driver_register(&syscon_driver
);
231 postcore_initcall(syscon_init
);
233 static void __exit
syscon_exit(void)
235 platform_driver_unregister(&syscon_driver
);
237 module_exit(syscon_exit
);
239 MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
240 MODULE_DESCRIPTION("System Control driver");
241 MODULE_LICENSE("GPL v2");