Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * System Control Driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Freescale Semiconductor, Inc. |
| 5 | * Copyright (C) 2012 Linaro Ltd. |
| 6 | * |
| 7 | * Author: Dong Aisheng <dong.aisheng@linaro.org> |
| 8 | * |
| 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. |
| 13 | */ |
| 14 | |
| 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> |
Fabio Estevam | 75177de | 2013-02-11 18:48:00 -0200 | [diff] [blame] | 23 | #include <linux/mfd/syscon.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 24 | |
| 25 | static struct platform_driver syscon_driver; |
| 26 | |
| 27 | struct syscon { |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 28 | void __iomem *base; |
| 29 | struct regmap *regmap; |
| 30 | }; |
| 31 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 32 | static int syscon_match_node(struct device *dev, void *data) |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 33 | { |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 34 | struct device_node *dn = data; |
| 35 | |
Alexander Shiyan | ed21465 | 2013-03-13 21:34:19 +0400 | [diff] [blame] | 36 | return (dev->of_node == dn) ? 1 : 0; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | struct regmap *syscon_node_to_regmap(struct device_node *np) |
| 40 | { |
| 41 | struct syscon *syscon; |
| 42 | struct device *dev; |
| 43 | |
| 44 | dev = driver_find_device(&syscon_driver.driver, NULL, np, |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 45 | syscon_match_node); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 46 | if (!dev) |
| 47 | return ERR_PTR(-EPROBE_DEFER); |
| 48 | |
| 49 | syscon = dev_get_drvdata(dev); |
| 50 | |
| 51 | return syscon->regmap; |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(syscon_node_to_regmap); |
| 54 | |
| 55 | struct regmap *syscon_regmap_lookup_by_compatible(const char *s) |
| 56 | { |
| 57 | struct device_node *syscon_np; |
| 58 | struct regmap *regmap; |
| 59 | |
| 60 | syscon_np = of_find_compatible_node(NULL, NULL, s); |
| 61 | if (!syscon_np) |
| 62 | return ERR_PTR(-ENODEV); |
| 63 | |
| 64 | regmap = syscon_node_to_regmap(syscon_np); |
| 65 | of_node_put(syscon_np); |
| 66 | |
| 67 | return regmap; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); |
| 70 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 71 | static int syscon_match_pdevname(struct device *dev, void *data) |
| 72 | { |
| 73 | struct platform_device *pdev = to_platform_device(dev); |
| 74 | const struct platform_device_id *id = platform_get_device_id(pdev); |
| 75 | |
| 76 | if (id) |
| 77 | if (!strcmp(id->name, (const char *)data)) |
| 78 | return 1; |
| 79 | |
| 80 | return !strcmp(dev_name(dev), (const char *)data); |
| 81 | } |
| 82 | |
| 83 | struct regmap *syscon_regmap_lookup_by_pdevname(const char *s) |
| 84 | { |
| 85 | struct device *dev; |
| 86 | struct syscon *syscon; |
| 87 | |
| 88 | dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s, |
| 89 | syscon_match_pdevname); |
| 90 | if (!dev) |
| 91 | return ERR_PTR(-EPROBE_DEFER); |
| 92 | |
| 93 | syscon = dev_get_drvdata(dev); |
| 94 | |
| 95 | return syscon->regmap; |
| 96 | } |
| 97 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname); |
| 98 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 99 | struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, |
| 100 | const char *property) |
| 101 | { |
| 102 | struct device_node *syscon_np; |
| 103 | struct regmap *regmap; |
| 104 | |
| 105 | syscon_np = of_parse_phandle(np, property, 0); |
| 106 | if (!syscon_np) |
| 107 | return ERR_PTR(-ENODEV); |
| 108 | |
| 109 | regmap = syscon_node_to_regmap(syscon_np); |
| 110 | of_node_put(syscon_np); |
| 111 | |
| 112 | return regmap; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); |
| 115 | |
| 116 | static const struct of_device_id of_syscon_match[] = { |
| 117 | { .compatible = "syscon", }, |
| 118 | { }, |
| 119 | }; |
| 120 | |
| 121 | static struct regmap_config syscon_regmap_config = { |
| 122 | .reg_bits = 32, |
| 123 | .val_bits = 32, |
| 124 | .reg_stride = 4, |
| 125 | }; |
| 126 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 127 | static int syscon_probe(struct platform_device *pdev) |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 128 | { |
| 129 | struct device *dev = &pdev->dev; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 130 | struct syscon *syscon; |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 131 | struct resource *res; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 132 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 133 | syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 134 | if (!syscon) |
| 135 | return -ENOMEM; |
| 136 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 137 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 138 | if (!res) |
| 139 | return -ENOENT; |
| 140 | |
| 141 | syscon->base = devm_ioremap(dev, res->start, resource_size(res)); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 142 | if (!syscon->base) |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 143 | return -ENOMEM; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 144 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 145 | syscon_regmap_config.max_register = res->end - res->start - 3; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 146 | syscon->regmap = devm_regmap_init_mmio(dev, syscon->base, |
| 147 | &syscon_regmap_config); |
| 148 | if (IS_ERR(syscon->regmap)) { |
| 149 | dev_err(dev, "regmap init failed\n"); |
| 150 | return PTR_ERR(syscon->regmap); |
| 151 | } |
| 152 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 153 | platform_set_drvdata(pdev, syscon); |
| 154 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 155 | dev_info(dev, "regmap 0x%x-0x%x registered\n", res->start, res->end); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 160 | static const struct platform_device_id syscon_ids[] = { |
| 161 | { "syscon", }, |
| 162 | { } |
| 163 | }; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 164 | |
| 165 | static struct platform_driver syscon_driver = { |
| 166 | .driver = { |
| 167 | .name = "syscon", |
| 168 | .owner = THIS_MODULE, |
| 169 | .of_match_table = of_syscon_match, |
| 170 | }, |
| 171 | .probe = syscon_probe, |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 172 | .id_table = syscon_ids, |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static int __init syscon_init(void) |
| 176 | { |
| 177 | return platform_driver_register(&syscon_driver); |
| 178 | } |
| 179 | postcore_initcall(syscon_init); |
| 180 | |
| 181 | static void __exit syscon_exit(void) |
| 182 | { |
| 183 | platform_driver_unregister(&syscon_driver); |
| 184 | } |
| 185 | module_exit(syscon_exit); |
| 186 | |
| 187 | MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>"); |
| 188 | MODULE_DESCRIPTION("System Control driver"); |
| 189 | MODULE_LICENSE("GPL v2"); |