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 | |
Fabrice Gasnier | a00406b | 2018-12-12 09:48:14 +0100 | [diff] [blame^] | 15 | #include <linux/clk.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Baolin Wang | 3bafc09 | 2017-12-25 14:37:10 +0800 | [diff] [blame] | 17 | #include <linux/hwspinlock.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 18 | #include <linux/io.h> |
Paul Gortmaker | 1345da7 | 2019-01-13 13:36:40 -0500 | [diff] [blame] | 19 | #include <linux/init.h> |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 20 | #include <linux/list.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/of_address.h> |
| 23 | #include <linux/of_platform.h> |
Pawel Moll | 29f9b6c | 2014-02-12 10:47:10 +0000 | [diff] [blame] | 24 | #include <linux/platform_data/syscon.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/regmap.h> |
Fabio Estevam | 75177de | 2013-02-11 18:48:00 -0200 | [diff] [blame] | 27 | #include <linux/mfd/syscon.h> |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 28 | #include <linux/slab.h> |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 29 | |
| 30 | static struct platform_driver syscon_driver; |
| 31 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 32 | static DEFINE_SPINLOCK(syscon_list_slock); |
| 33 | static LIST_HEAD(syscon_list); |
| 34 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 35 | struct syscon { |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 36 | struct device_node *np; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 37 | struct regmap *regmap; |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 38 | struct list_head list; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
Philipp Zabel | c131045 | 2016-01-29 10:30:16 +0100 | [diff] [blame] | 41 | static const struct regmap_config syscon_regmap_config = { |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 42 | .reg_bits = 32, |
| 43 | .val_bits = 32, |
| 44 | .reg_stride = 4, |
| 45 | }; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 46 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 47 | static struct syscon *of_syscon_register(struct device_node *np) |
| 48 | { |
Fabrice Gasnier | a00406b | 2018-12-12 09:48:14 +0100 | [diff] [blame^] | 49 | struct clk *clk; |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 50 | struct syscon *syscon; |
| 51 | struct regmap *regmap; |
| 52 | void __iomem *base; |
Damien Riegel | db2fb60 | 2015-11-30 10:59:47 -0500 | [diff] [blame] | 53 | u32 reg_io_width; |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 54 | int ret; |
| 55 | struct regmap_config syscon_config = syscon_regmap_config; |
Philipp Zabel | ca668f0 | 2016-01-29 10:35:51 +0100 | [diff] [blame] | 56 | struct resource res; |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 57 | |
| 58 | if (!of_device_is_compatible(np, "syscon")) |
| 59 | return ERR_PTR(-EINVAL); |
| 60 | |
| 61 | syscon = kzalloc(sizeof(*syscon), GFP_KERNEL); |
| 62 | if (!syscon) |
| 63 | return ERR_PTR(-ENOMEM); |
| 64 | |
Philipp Zabel | ca668f0 | 2016-01-29 10:35:51 +0100 | [diff] [blame] | 65 | if (of_address_to_resource(np, 0, &res)) { |
| 66 | ret = -ENOMEM; |
| 67 | goto err_map; |
| 68 | } |
| 69 | |
| 70 | base = ioremap(res.start, resource_size(&res)); |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 71 | if (!base) { |
| 72 | ret = -ENOMEM; |
| 73 | goto err_map; |
| 74 | } |
| 75 | |
| 76 | /* Parse the device's DT node for an endianness specification */ |
| 77 | if (of_property_read_bool(np, "big-endian")) |
| 78 | syscon_config.val_format_endian = REGMAP_ENDIAN_BIG; |
Paul Burton | d29ccdb | 2016-10-14 10:17:31 +0100 | [diff] [blame] | 79 | else if (of_property_read_bool(np, "little-endian")) |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 80 | syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE; |
Paul Burton | d29ccdb | 2016-10-14 10:17:31 +0100 | [diff] [blame] | 81 | else if (of_property_read_bool(np, "native-endian")) |
| 82 | syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE; |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 83 | |
Damien Riegel | db2fb60 | 2015-11-30 10:59:47 -0500 | [diff] [blame] | 84 | /* |
| 85 | * search for reg-io-width property in DT. If it is not provided, |
| 86 | * default to 4 bytes. regmap_init_mmio will return an error if values |
| 87 | * are invalid so there is no need to check them here. |
| 88 | */ |
| 89 | ret = of_property_read_u32(np, "reg-io-width", ®_io_width); |
| 90 | if (ret) |
| 91 | reg_io_width = 4; |
| 92 | |
Baolin Wang | 3bafc09 | 2017-12-25 14:37:10 +0800 | [diff] [blame] | 93 | ret = of_hwspin_lock_get_id(np, 0); |
| 94 | if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) { |
| 95 | syscon_config.use_hwlock = true; |
| 96 | syscon_config.hwlock_id = ret; |
| 97 | syscon_config.hwlock_mode = HWLOCK_IRQSTATE; |
| 98 | } else if (ret < 0) { |
| 99 | switch (ret) { |
| 100 | case -ENOENT: |
| 101 | /* Ignore missing hwlock, it's optional. */ |
| 102 | break; |
| 103 | default: |
| 104 | pr_err("Failed to retrieve valid hwlock: %d\n", ret); |
| 105 | /* fall-through */ |
| 106 | case -EPROBE_DEFER: |
| 107 | goto err_regmap; |
| 108 | } |
| 109 | } |
| 110 | |
David Lechner | 408d1d5 | 2018-01-23 16:57:34 -0600 | [diff] [blame] | 111 | syscon_config.name = of_node_full_name(np); |
Damien Riegel | db2fb60 | 2015-11-30 10:59:47 -0500 | [diff] [blame] | 112 | syscon_config.reg_stride = reg_io_width; |
| 113 | syscon_config.val_bits = reg_io_width * 8; |
Philipp Zabel | ca668f0 | 2016-01-29 10:35:51 +0100 | [diff] [blame] | 114 | syscon_config.max_register = resource_size(&res) - reg_io_width; |
Jeffy Chen | 500f9ff | 2018-03-12 17:51:39 +0800 | [diff] [blame] | 115 | syscon_config.name = of_node_full_name(np); |
Damien Riegel | db2fb60 | 2015-11-30 10:59:47 -0500 | [diff] [blame] | 116 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 117 | regmap = regmap_init_mmio(NULL, base, &syscon_config); |
| 118 | if (IS_ERR(regmap)) { |
| 119 | pr_err("regmap init failed\n"); |
| 120 | ret = PTR_ERR(regmap); |
| 121 | goto err_regmap; |
| 122 | } |
| 123 | |
Fabrice Gasnier | a00406b | 2018-12-12 09:48:14 +0100 | [diff] [blame^] | 124 | clk = of_clk_get(np, 0); |
| 125 | if (IS_ERR(clk)) { |
| 126 | ret = PTR_ERR(clk); |
| 127 | /* clock is optional */ |
| 128 | if (ret != -ENOENT) |
| 129 | goto err_clk; |
| 130 | } else { |
| 131 | ret = regmap_mmio_attach_clk(regmap, clk); |
| 132 | if (ret) |
| 133 | goto err_attach; |
| 134 | } |
| 135 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 136 | syscon->regmap = regmap; |
| 137 | syscon->np = np; |
| 138 | |
| 139 | spin_lock(&syscon_list_slock); |
| 140 | list_add_tail(&syscon->list, &syscon_list); |
| 141 | spin_unlock(&syscon_list_slock); |
| 142 | |
| 143 | return syscon; |
| 144 | |
Fabrice Gasnier | a00406b | 2018-12-12 09:48:14 +0100 | [diff] [blame^] | 145 | err_attach: |
| 146 | if (!IS_ERR(clk)) |
| 147 | clk_put(clk); |
| 148 | err_clk: |
| 149 | regmap_exit(regmap); |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 150 | err_regmap: |
| 151 | iounmap(base); |
| 152 | err_map: |
| 153 | kfree(syscon); |
| 154 | return ERR_PTR(ret); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | struct regmap *syscon_node_to_regmap(struct device_node *np) |
| 158 | { |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 159 | struct syscon *entry, *syscon = NULL; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 160 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 161 | spin_lock(&syscon_list_slock); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 162 | |
Pankaj Dubey | bdb0066 | 2014-09-30 14:05:27 +0530 | [diff] [blame] | 163 | list_for_each_entry(entry, &syscon_list, list) |
| 164 | if (entry->np == np) { |
| 165 | syscon = entry; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | spin_unlock(&syscon_list_slock); |
| 170 | |
| 171 | if (!syscon) |
| 172 | syscon = of_syscon_register(np); |
| 173 | |
| 174 | if (IS_ERR(syscon)) |
| 175 | return ERR_CAST(syscon); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 176 | |
| 177 | return syscon->regmap; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(syscon_node_to_regmap); |
| 180 | |
| 181 | struct regmap *syscon_regmap_lookup_by_compatible(const char *s) |
| 182 | { |
| 183 | struct device_node *syscon_np; |
| 184 | struct regmap *regmap; |
| 185 | |
| 186 | syscon_np = of_find_compatible_node(NULL, NULL, s); |
| 187 | if (!syscon_np) |
| 188 | return ERR_PTR(-ENODEV); |
| 189 | |
| 190 | regmap = syscon_node_to_regmap(syscon_np); |
| 191 | of_node_put(syscon_np); |
| 192 | |
| 193 | return regmap; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); |
| 196 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 197 | static int syscon_match_pdevname(struct device *dev, void *data) |
| 198 | { |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 199 | return !strcmp(dev_name(dev), (const char *)data); |
| 200 | } |
| 201 | |
| 202 | struct regmap *syscon_regmap_lookup_by_pdevname(const char *s) |
| 203 | { |
| 204 | struct device *dev; |
| 205 | struct syscon *syscon; |
| 206 | |
| 207 | dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s, |
| 208 | syscon_match_pdevname); |
| 209 | if (!dev) |
| 210 | return ERR_PTR(-EPROBE_DEFER); |
| 211 | |
| 212 | syscon = dev_get_drvdata(dev); |
| 213 | |
| 214 | return syscon->regmap; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname); |
| 217 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 218 | struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, |
| 219 | const char *property) |
| 220 | { |
| 221 | struct device_node *syscon_np; |
| 222 | struct regmap *regmap; |
| 223 | |
Pankaj Dubey | 45330bb | 2014-04-30 11:14:26 +0900 | [diff] [blame] | 224 | if (property) |
| 225 | syscon_np = of_parse_phandle(np, property, 0); |
| 226 | else |
| 227 | syscon_np = np; |
| 228 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 229 | if (!syscon_np) |
| 230 | return ERR_PTR(-ENODEV); |
| 231 | |
| 232 | regmap = syscon_node_to_regmap(syscon_np); |
| 233 | of_node_put(syscon_np); |
| 234 | |
| 235 | return regmap; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); |
| 238 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 239 | static int syscon_probe(struct platform_device *pdev) |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 240 | { |
| 241 | struct device *dev = &pdev->dev; |
Pawel Moll | 29f9b6c | 2014-02-12 10:47:10 +0000 | [diff] [blame] | 242 | struct syscon_platform_data *pdata = dev_get_platdata(dev); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 243 | struct syscon *syscon; |
Philipp Zabel | c131045 | 2016-01-29 10:30:16 +0100 | [diff] [blame] | 244 | struct regmap_config syscon_config = syscon_regmap_config; |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 245 | struct resource *res; |
Alexander Shiyan | f10111cc | 2013-07-14 10:39:42 +0400 | [diff] [blame] | 246 | void __iomem *base; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 247 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 248 | syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 249 | if (!syscon) |
| 250 | return -ENOMEM; |
| 251 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 252 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 253 | if (!res) |
| 254 | return -ENOENT; |
| 255 | |
Alexander Shiyan | f10111cc | 2013-07-14 10:39:42 +0400 | [diff] [blame] | 256 | base = devm_ioremap(dev, res->start, resource_size(res)); |
| 257 | if (!base) |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 258 | return -ENOMEM; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 259 | |
Philipp Zabel | c131045 | 2016-01-29 10:30:16 +0100 | [diff] [blame] | 260 | syscon_config.max_register = res->end - res->start - 3; |
Pawel Moll | 29f9b6c | 2014-02-12 10:47:10 +0000 | [diff] [blame] | 261 | if (pdata) |
Philipp Zabel | c131045 | 2016-01-29 10:30:16 +0100 | [diff] [blame] | 262 | syscon_config.name = pdata->label; |
| 263 | syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 264 | if (IS_ERR(syscon->regmap)) { |
| 265 | dev_err(dev, "regmap init failed\n"); |
| 266 | return PTR_ERR(syscon->regmap); |
| 267 | } |
| 268 | |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 269 | platform_set_drvdata(pdev, syscon); |
| 270 | |
Alexander Shiyan | 38d8974 | 2014-02-22 10:02:25 +0400 | [diff] [blame] | 271 | dev_dbg(dev, "regmap %pR registered\n", res); |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 276 | static const struct platform_device_id syscon_ids[] = { |
| 277 | { "syscon", }, |
| 278 | { } |
| 279 | }; |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 280 | |
| 281 | static struct platform_driver syscon_driver = { |
| 282 | .driver = { |
| 283 | .name = "syscon", |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 284 | }, |
| 285 | .probe = syscon_probe, |
Alexander Shiyan | 5ab3a89 | 2013-03-13 21:34:20 +0400 | [diff] [blame] | 286 | .id_table = syscon_ids, |
Dong Aisheng | 87d6873 | 2012-09-05 10:57:13 +0800 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static int __init syscon_init(void) |
| 290 | { |
| 291 | return platform_driver_register(&syscon_driver); |
| 292 | } |
| 293 | postcore_initcall(syscon_init); |