^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * System Control Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2012 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Dong Aisheng <dong.aisheng@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/hwspinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/platform_data/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static struct platform_driver syscon_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static DEFINE_SPINLOCK(syscon_list_slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static LIST_HEAD(syscon_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct syscon {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct regmap_config syscon_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .reg_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .val_bits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .reg_stride = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct syscon *syscon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u32 reg_io_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct regmap_config syscon_config = syscon_regmap_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!syscon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (of_address_to_resource(np, 0, &res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto err_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) base = ioremap(res.start, resource_size(&res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) goto err_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Parse the device's DT node for an endianness specification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (of_property_read_bool(np, "big-endian"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else if (of_property_read_bool(np, "little-endian"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) else if (of_property_read_bool(np, "native-endian"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * search for reg-io-width property in DT. If it is not provided,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * default to 4 bytes. regmap_init_mmio will return an error if values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * are invalid so there is no need to check them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) ret = of_property_read_u32(np, "reg-io-width", ®_io_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) reg_io_width = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = of_hwspin_lock_get_id(np, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) syscon_config.use_hwlock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) syscon_config.hwlock_id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Ignore missing hwlock, it's optional. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pr_err("Failed to retrieve valid hwlock: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case -EPROBE_DEFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) goto err_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (u64)res.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) syscon_config.reg_stride = reg_io_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) syscon_config.val_bits = reg_io_width * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) syscon_config.max_register = resource_size(&res) - reg_io_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) regmap = regmap_init_mmio(NULL, base, &syscon_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kfree(syscon_config.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pr_err("regmap init failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto err_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (check_clk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) clk = of_clk_get(np, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (IS_ERR(clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ret = PTR_ERR(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* clock is optional */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (ret != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto err_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = regmap_mmio_attach_clk(regmap, clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto err_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) syscon->regmap = regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) syscon->np = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_lock(&syscon_list_slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) list_add_tail(&syscon->list, &syscon_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) spin_unlock(&syscon_list_slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return syscon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) err_attach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!IS_ERR(clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) clk_put(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) err_clk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) regmap_exit(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err_regmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) iounmap(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) err_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) kfree(syscon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static struct regmap *device_node_get_regmap(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bool check_clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct syscon *entry, *syscon = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spin_lock(&syscon_list_slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) list_for_each_entry(entry, &syscon_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (entry->np == np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) syscon = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) spin_unlock(&syscon_list_slock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (!syscon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) syscon = of_syscon_register(np, check_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (IS_ERR(syscon))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return ERR_CAST(syscon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return syscon->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct regmap *device_node_to_regmap(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return device_node_get_regmap(np, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) EXPORT_SYMBOL_GPL(device_node_to_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct regmap *syscon_node_to_regmap(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!of_device_is_compatible(np, "syscon"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return device_node_get_regmap(np, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct device_node *syscon_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) syscon_np = of_find_compatible_node(NULL, NULL, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!syscon_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) regmap = syscon_node_to_regmap(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) of_node_put(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) const char *property)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct device_node *syscon_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (property)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) syscon_np = of_parse_phandle(np, property, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) syscon_np = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!syscon_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) regmap = syscon_node_to_regmap(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) of_node_put(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const char *property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int arg_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) unsigned int *out_args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct device_node *syscon_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct of_phandle_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 0, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) syscon_np = args.np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!syscon_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) regmap = syscon_node_to_regmap(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) for (index = 0; index < arg_count; index++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) out_args[index] = args.args[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) of_node_put(syscon_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int syscon_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct syscon_platform_data *pdata = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct syscon *syscon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct regmap_config syscon_config = syscon_regmap_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!syscon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) base = devm_ioremap(dev, res->start, resource_size(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) syscon_config.max_register = resource_size(res) - 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) syscon_config.name = pdata->label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (IS_ERR(syscon->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_err(dev, "regmap init failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return PTR_ERR(syscon->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) platform_set_drvdata(pdev, syscon);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev_dbg(dev, "regmap %pR registered\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static const struct platform_device_id syscon_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) { "syscon", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static struct platform_driver syscon_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .name = "syscon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .probe = syscon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .id_table = syscon_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int __init syscon_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return platform_driver_register(&syscon_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) postcore_initcall(syscon_init);