1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 287d68730SDong Aisheng /* 387d68730SDong Aisheng * System Control Driver 487d68730SDong Aisheng * 587d68730SDong Aisheng * Copyright (C) 2012 Freescale Semiconductor, Inc. 687d68730SDong Aisheng * Copyright (C) 2012 Linaro Ltd. 787d68730SDong Aisheng * 887d68730SDong Aisheng * Author: Dong Aisheng <[email protected]> 987d68730SDong Aisheng */ 1087d68730SDong Aisheng 11a00406b7SFabrice Gasnier #include <linux/clk.h> 1287d68730SDong Aisheng #include <linux/err.h> 133bafc09eSBaolin Wang #include <linux/hwspinlock.h> 1487d68730SDong Aisheng #include <linux/io.h> 151345da73SPaul Gortmaker #include <linux/init.h> 16bdb0066dSPankaj Dubey #include <linux/list.h> 1787d68730SDong Aisheng #include <linux/of.h> 1887d68730SDong Aisheng #include <linux/of_address.h> 1987d68730SDong Aisheng #include <linux/of_platform.h> 2029f9b6cfSPawel Moll #include <linux/platform_data/syscon.h> 2187d68730SDong Aisheng #include <linux/platform_device.h> 2287d68730SDong Aisheng #include <linux/regmap.h> 2375177deeSFabio Estevam #include <linux/mfd/syscon.h> 24bdb0066dSPankaj Dubey #include <linux/slab.h> 2587d68730SDong Aisheng 2687d68730SDong Aisheng static struct platform_driver syscon_driver; 2787d68730SDong Aisheng 28bdb0066dSPankaj Dubey static DEFINE_SPINLOCK(syscon_list_slock); 29bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list); 30bdb0066dSPankaj Dubey 3187d68730SDong Aisheng struct syscon { 32bdb0066dSPankaj Dubey struct device_node *np; 3387d68730SDong Aisheng struct regmap *regmap; 34bdb0066dSPankaj Dubey struct list_head list; 3587d68730SDong Aisheng }; 3687d68730SDong Aisheng 37c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = { 38bdb0066dSPankaj Dubey .reg_bits = 32, 39bdb0066dSPankaj Dubey .val_bits = 32, 40bdb0066dSPankaj Dubey .reg_stride = 4, 41bdb0066dSPankaj Dubey }; 4287d68730SDong Aisheng 43bdb0066dSPankaj Dubey static struct syscon *of_syscon_register(struct device_node *np) 44bdb0066dSPankaj Dubey { 45a00406b7SFabrice Gasnier struct clk *clk; 46bdb0066dSPankaj Dubey struct syscon *syscon; 47bdb0066dSPankaj Dubey struct regmap *regmap; 48bdb0066dSPankaj Dubey void __iomem *base; 49db2fb60cSDamien Riegel u32 reg_io_width; 50bdb0066dSPankaj Dubey int ret; 51bdb0066dSPankaj Dubey struct regmap_config syscon_config = syscon_regmap_config; 52ca668f0eSPhilipp Zabel struct resource res; 53bdb0066dSPankaj Dubey 54bdb0066dSPankaj Dubey if (!of_device_is_compatible(np, "syscon")) 55bdb0066dSPankaj Dubey return ERR_PTR(-EINVAL); 56bdb0066dSPankaj Dubey 57bdb0066dSPankaj Dubey syscon = kzalloc(sizeof(*syscon), GFP_KERNEL); 58bdb0066dSPankaj Dubey if (!syscon) 59bdb0066dSPankaj Dubey return ERR_PTR(-ENOMEM); 60bdb0066dSPankaj Dubey 61ca668f0eSPhilipp Zabel if (of_address_to_resource(np, 0, &res)) { 62ca668f0eSPhilipp Zabel ret = -ENOMEM; 63ca668f0eSPhilipp Zabel goto err_map; 64ca668f0eSPhilipp Zabel } 65ca668f0eSPhilipp Zabel 66ca668f0eSPhilipp Zabel base = ioremap(res.start, resource_size(&res)); 67bdb0066dSPankaj Dubey if (!base) { 68bdb0066dSPankaj Dubey ret = -ENOMEM; 69bdb0066dSPankaj Dubey goto err_map; 70bdb0066dSPankaj Dubey } 71bdb0066dSPankaj Dubey 72bdb0066dSPankaj Dubey /* Parse the device's DT node for an endianness specification */ 73bdb0066dSPankaj Dubey if (of_property_read_bool(np, "big-endian")) 74bdb0066dSPankaj Dubey syscon_config.val_format_endian = REGMAP_ENDIAN_BIG; 75bdb0066dSPankaj Dubey else if (of_property_read_bool(np, "little-endian")) 76bdb0066dSPankaj Dubey syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE; 77d29ccdb3SPaul Burton else if (of_property_read_bool(np, "native-endian")) 78d29ccdb3SPaul Burton syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE; 79bdb0066dSPankaj Dubey 80db2fb60cSDamien Riegel /* 81db2fb60cSDamien Riegel * search for reg-io-width property in DT. If it is not provided, 82db2fb60cSDamien Riegel * default to 4 bytes. regmap_init_mmio will return an error if values 83db2fb60cSDamien Riegel * are invalid so there is no need to check them here. 84db2fb60cSDamien Riegel */ 85db2fb60cSDamien Riegel ret = of_property_read_u32(np, "reg-io-width", ®_io_width); 86db2fb60cSDamien Riegel if (ret) 87db2fb60cSDamien Riegel reg_io_width = 4; 88db2fb60cSDamien Riegel 893bafc09eSBaolin Wang ret = of_hwspin_lock_get_id(np, 0); 903bafc09eSBaolin Wang if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) { 913bafc09eSBaolin Wang syscon_config.use_hwlock = true; 923bafc09eSBaolin Wang syscon_config.hwlock_id = ret; 933bafc09eSBaolin Wang syscon_config.hwlock_mode = HWLOCK_IRQSTATE; 943bafc09eSBaolin Wang } else if (ret < 0) { 953bafc09eSBaolin Wang switch (ret) { 963bafc09eSBaolin Wang case -ENOENT: 973bafc09eSBaolin Wang /* Ignore missing hwlock, it's optional. */ 983bafc09eSBaolin Wang break; 993bafc09eSBaolin Wang default: 1003bafc09eSBaolin Wang pr_err("Failed to retrieve valid hwlock: %d\n", ret); 1013bafc09eSBaolin Wang /* fall-through */ 1023bafc09eSBaolin Wang case -EPROBE_DEFER: 1033bafc09eSBaolin Wang goto err_regmap; 1043bafc09eSBaolin Wang } 1053bafc09eSBaolin Wang } 1063bafc09eSBaolin Wang 107408d1d57SDavid Lechner syscon_config.name = of_node_full_name(np); 108db2fb60cSDamien Riegel syscon_config.reg_stride = reg_io_width; 109db2fb60cSDamien Riegel syscon_config.val_bits = reg_io_width * 8; 110ca668f0eSPhilipp Zabel syscon_config.max_register = resource_size(&res) - reg_io_width; 111500f9ff5SJeffy Chen syscon_config.name = of_node_full_name(np); 112db2fb60cSDamien Riegel 113bdb0066dSPankaj Dubey regmap = regmap_init_mmio(NULL, base, &syscon_config); 114bdb0066dSPankaj Dubey if (IS_ERR(regmap)) { 115bdb0066dSPankaj Dubey pr_err("regmap init failed\n"); 116bdb0066dSPankaj Dubey ret = PTR_ERR(regmap); 117bdb0066dSPankaj Dubey goto err_regmap; 118bdb0066dSPankaj Dubey } 119bdb0066dSPankaj Dubey 120a00406b7SFabrice Gasnier clk = of_clk_get(np, 0); 121a00406b7SFabrice Gasnier if (IS_ERR(clk)) { 122a00406b7SFabrice Gasnier ret = PTR_ERR(clk); 123a00406b7SFabrice Gasnier /* clock is optional */ 124a00406b7SFabrice Gasnier if (ret != -ENOENT) 125a00406b7SFabrice Gasnier goto err_clk; 126a00406b7SFabrice Gasnier } else { 127a00406b7SFabrice Gasnier ret = regmap_mmio_attach_clk(regmap, clk); 128a00406b7SFabrice Gasnier if (ret) 129a00406b7SFabrice Gasnier goto err_attach; 130a00406b7SFabrice Gasnier } 131a00406b7SFabrice Gasnier 132bdb0066dSPankaj Dubey syscon->regmap = regmap; 133bdb0066dSPankaj Dubey syscon->np = np; 134bdb0066dSPankaj Dubey 135bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 136bdb0066dSPankaj Dubey list_add_tail(&syscon->list, &syscon_list); 137bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 138bdb0066dSPankaj Dubey 139bdb0066dSPankaj Dubey return syscon; 140bdb0066dSPankaj Dubey 141a00406b7SFabrice Gasnier err_attach: 142a00406b7SFabrice Gasnier if (!IS_ERR(clk)) 143a00406b7SFabrice Gasnier clk_put(clk); 144a00406b7SFabrice Gasnier err_clk: 145a00406b7SFabrice Gasnier regmap_exit(regmap); 146bdb0066dSPankaj Dubey err_regmap: 147bdb0066dSPankaj Dubey iounmap(base); 148bdb0066dSPankaj Dubey err_map: 149bdb0066dSPankaj Dubey kfree(syscon); 150bdb0066dSPankaj Dubey return ERR_PTR(ret); 15187d68730SDong Aisheng } 15287d68730SDong Aisheng 15387d68730SDong Aisheng struct regmap *syscon_node_to_regmap(struct device_node *np) 15487d68730SDong Aisheng { 155bdb0066dSPankaj Dubey struct syscon *entry, *syscon = NULL; 15687d68730SDong Aisheng 157bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 15887d68730SDong Aisheng 159bdb0066dSPankaj Dubey list_for_each_entry(entry, &syscon_list, list) 160bdb0066dSPankaj Dubey if (entry->np == np) { 161bdb0066dSPankaj Dubey syscon = entry; 162bdb0066dSPankaj Dubey break; 163bdb0066dSPankaj Dubey } 164bdb0066dSPankaj Dubey 165bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 166bdb0066dSPankaj Dubey 167bdb0066dSPankaj Dubey if (!syscon) 168bdb0066dSPankaj Dubey syscon = of_syscon_register(np); 169bdb0066dSPankaj Dubey 170bdb0066dSPankaj Dubey if (IS_ERR(syscon)) 171bdb0066dSPankaj Dubey return ERR_CAST(syscon); 17287d68730SDong Aisheng 17387d68730SDong Aisheng return syscon->regmap; 17487d68730SDong Aisheng } 17587d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap); 17687d68730SDong Aisheng 17787d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s) 17887d68730SDong Aisheng { 17987d68730SDong Aisheng struct device_node *syscon_np; 18087d68730SDong Aisheng struct regmap *regmap; 18187d68730SDong Aisheng 18287d68730SDong Aisheng syscon_np = of_find_compatible_node(NULL, NULL, s); 18387d68730SDong Aisheng if (!syscon_np) 18487d68730SDong Aisheng return ERR_PTR(-ENODEV); 18587d68730SDong Aisheng 18687d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 18787d68730SDong Aisheng of_node_put(syscon_np); 18887d68730SDong Aisheng 18987d68730SDong Aisheng return regmap; 19087d68730SDong Aisheng } 19187d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); 19287d68730SDong Aisheng 1935ab3a89aSAlexander Shiyan static int syscon_match_pdevname(struct device *dev, void *data) 1945ab3a89aSAlexander Shiyan { 1955ab3a89aSAlexander Shiyan return !strcmp(dev_name(dev), (const char *)data); 1965ab3a89aSAlexander Shiyan } 1975ab3a89aSAlexander Shiyan 1985ab3a89aSAlexander Shiyan struct regmap *syscon_regmap_lookup_by_pdevname(const char *s) 1995ab3a89aSAlexander Shiyan { 2005ab3a89aSAlexander Shiyan struct device *dev; 2015ab3a89aSAlexander Shiyan struct syscon *syscon; 2025ab3a89aSAlexander Shiyan 2035ab3a89aSAlexander Shiyan dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s, 2045ab3a89aSAlexander Shiyan syscon_match_pdevname); 2055ab3a89aSAlexander Shiyan if (!dev) 2065ab3a89aSAlexander Shiyan return ERR_PTR(-EPROBE_DEFER); 2075ab3a89aSAlexander Shiyan 2085ab3a89aSAlexander Shiyan syscon = dev_get_drvdata(dev); 2095ab3a89aSAlexander Shiyan 2105ab3a89aSAlexander Shiyan return syscon->regmap; 2115ab3a89aSAlexander Shiyan } 2125ab3a89aSAlexander Shiyan EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname); 2135ab3a89aSAlexander Shiyan 21487d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, 21587d68730SDong Aisheng const char *property) 21687d68730SDong Aisheng { 21787d68730SDong Aisheng struct device_node *syscon_np; 21887d68730SDong Aisheng struct regmap *regmap; 21987d68730SDong Aisheng 22045330bb4SPankaj Dubey if (property) 22187d68730SDong Aisheng syscon_np = of_parse_phandle(np, property, 0); 22245330bb4SPankaj Dubey else 22345330bb4SPankaj Dubey syscon_np = np; 22445330bb4SPankaj Dubey 22587d68730SDong Aisheng if (!syscon_np) 22687d68730SDong Aisheng return ERR_PTR(-ENODEV); 22787d68730SDong Aisheng 22887d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 22987d68730SDong Aisheng of_node_put(syscon_np); 23087d68730SDong Aisheng 23187d68730SDong Aisheng return regmap; 23287d68730SDong Aisheng } 23387d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); 23487d68730SDong Aisheng 235f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev) 23687d68730SDong Aisheng { 23787d68730SDong Aisheng struct device *dev = &pdev->dev; 23829f9b6cfSPawel Moll struct syscon_platform_data *pdata = dev_get_platdata(dev); 23987d68730SDong Aisheng struct syscon *syscon; 240c131045dSPhilipp Zabel struct regmap_config syscon_config = syscon_regmap_config; 2415ab3a89aSAlexander Shiyan struct resource *res; 242f10111ccSAlexander Shiyan void __iomem *base; 24387d68730SDong Aisheng 2445ab3a89aSAlexander Shiyan syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); 24587d68730SDong Aisheng if (!syscon) 24687d68730SDong Aisheng return -ENOMEM; 24787d68730SDong Aisheng 2485ab3a89aSAlexander Shiyan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2495ab3a89aSAlexander Shiyan if (!res) 2505ab3a89aSAlexander Shiyan return -ENOENT; 2515ab3a89aSAlexander Shiyan 252f10111ccSAlexander Shiyan base = devm_ioremap(dev, res->start, resource_size(res)); 253f10111ccSAlexander Shiyan if (!base) 2545ab3a89aSAlexander Shiyan return -ENOMEM; 25587d68730SDong Aisheng 256c131045dSPhilipp Zabel syscon_config.max_register = res->end - res->start - 3; 25729f9b6cfSPawel Moll if (pdata) 258c131045dSPhilipp Zabel syscon_config.name = pdata->label; 259c131045dSPhilipp Zabel syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config); 26087d68730SDong Aisheng if (IS_ERR(syscon->regmap)) { 26187d68730SDong Aisheng dev_err(dev, "regmap init failed\n"); 26287d68730SDong Aisheng return PTR_ERR(syscon->regmap); 26387d68730SDong Aisheng } 26487d68730SDong Aisheng 26587d68730SDong Aisheng platform_set_drvdata(pdev, syscon); 26687d68730SDong Aisheng 26738d8974eSAlexander Shiyan dev_dbg(dev, "regmap %pR registered\n", res); 26887d68730SDong Aisheng 26987d68730SDong Aisheng return 0; 27087d68730SDong Aisheng } 27187d68730SDong Aisheng 2725ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = { 2735ab3a89aSAlexander Shiyan { "syscon", }, 2745ab3a89aSAlexander Shiyan { } 2755ab3a89aSAlexander Shiyan }; 27687d68730SDong Aisheng 27787d68730SDong Aisheng static struct platform_driver syscon_driver = { 27887d68730SDong Aisheng .driver = { 27987d68730SDong Aisheng .name = "syscon", 28087d68730SDong Aisheng }, 28187d68730SDong Aisheng .probe = syscon_probe, 2825ab3a89aSAlexander Shiyan .id_table = syscon_ids, 28387d68730SDong Aisheng }; 28487d68730SDong Aisheng 28587d68730SDong Aisheng static int __init syscon_init(void) 28687d68730SDong Aisheng { 28787d68730SDong Aisheng return platform_driver_register(&syscon_driver); 28887d68730SDong Aisheng } 28987d68730SDong Aisheng postcore_initcall(syscon_init); 290