12874c5fdSThomas 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 4339233b7cSPaul Cercueil static struct syscon *of_syscon_register(struct device_node *np, bool check_clk) 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 syscon = kzalloc(sizeof(*syscon), GFP_KERNEL); 55bdb0066dSPankaj Dubey if (!syscon) 56bdb0066dSPankaj Dubey return ERR_PTR(-ENOMEM); 57bdb0066dSPankaj Dubey 58ca668f0eSPhilipp Zabel if (of_address_to_resource(np, 0, &res)) { 59ca668f0eSPhilipp Zabel ret = -ENOMEM; 60ca668f0eSPhilipp Zabel goto err_map; 61ca668f0eSPhilipp Zabel } 62ca668f0eSPhilipp Zabel 63ca668f0eSPhilipp Zabel base = ioremap(res.start, resource_size(&res)); 64bdb0066dSPankaj Dubey if (!base) { 65bdb0066dSPankaj Dubey ret = -ENOMEM; 66bdb0066dSPankaj Dubey goto err_map; 67bdb0066dSPankaj Dubey } 68bdb0066dSPankaj Dubey 69bdb0066dSPankaj Dubey /* Parse the device's DT node for an endianness specification */ 70bdb0066dSPankaj Dubey if (of_property_read_bool(np, "big-endian")) 71bdb0066dSPankaj Dubey syscon_config.val_format_endian = REGMAP_ENDIAN_BIG; 72bdb0066dSPankaj Dubey else if (of_property_read_bool(np, "little-endian")) 73bdb0066dSPankaj Dubey syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE; 74d29ccdb3SPaul Burton else if (of_property_read_bool(np, "native-endian")) 75d29ccdb3SPaul Burton syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE; 76bdb0066dSPankaj Dubey 77db2fb60cSDamien Riegel /* 78db2fb60cSDamien Riegel * search for reg-io-width property in DT. If it is not provided, 79db2fb60cSDamien Riegel * default to 4 bytes. regmap_init_mmio will return an error if values 80db2fb60cSDamien Riegel * are invalid so there is no need to check them here. 81db2fb60cSDamien Riegel */ 82db2fb60cSDamien Riegel ret = of_property_read_u32(np, "reg-io-width", ®_io_width); 83db2fb60cSDamien Riegel if (ret) 84db2fb60cSDamien Riegel reg_io_width = 4; 85db2fb60cSDamien Riegel 863bafc09eSBaolin Wang ret = of_hwspin_lock_get_id(np, 0); 873bafc09eSBaolin Wang if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) { 883bafc09eSBaolin Wang syscon_config.use_hwlock = true; 893bafc09eSBaolin Wang syscon_config.hwlock_id = ret; 903bafc09eSBaolin Wang syscon_config.hwlock_mode = HWLOCK_IRQSTATE; 913bafc09eSBaolin Wang } else if (ret < 0) { 923bafc09eSBaolin Wang switch (ret) { 933bafc09eSBaolin Wang case -ENOENT: 943bafc09eSBaolin Wang /* Ignore missing hwlock, it's optional. */ 953bafc09eSBaolin Wang break; 963bafc09eSBaolin Wang default: 973bafc09eSBaolin Wang pr_err("Failed to retrieve valid hwlock: %d\n", ret); 98df561f66SGustavo A. R. Silva fallthrough; 993bafc09eSBaolin Wang case -EPROBE_DEFER: 1003bafc09eSBaolin Wang goto err_regmap; 1013bafc09eSBaolin Wang } 1023bafc09eSBaolin Wang } 1033bafc09eSBaolin Wang 104e15d7f2bSSuman Anna syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", np, 105e15d7f2bSSuman Anna (u64)res.start); 106db2fb60cSDamien Riegel syscon_config.reg_stride = reg_io_width; 107db2fb60cSDamien Riegel syscon_config.val_bits = reg_io_width * 8; 108ca668f0eSPhilipp Zabel syscon_config.max_register = resource_size(&res) - reg_io_width; 109db2fb60cSDamien Riegel 110bdb0066dSPankaj Dubey regmap = regmap_init_mmio(NULL, base, &syscon_config); 111bdb0066dSPankaj Dubey if (IS_ERR(regmap)) { 112bdb0066dSPankaj Dubey pr_err("regmap init failed\n"); 113bdb0066dSPankaj Dubey ret = PTR_ERR(regmap); 114bdb0066dSPankaj Dubey goto err_regmap; 115bdb0066dSPankaj Dubey } 116bdb0066dSPankaj Dubey 11739233b7cSPaul Cercueil if (check_clk) { 118a00406b7SFabrice Gasnier clk = of_clk_get(np, 0); 119a00406b7SFabrice Gasnier if (IS_ERR(clk)) { 120a00406b7SFabrice Gasnier ret = PTR_ERR(clk); 121a00406b7SFabrice Gasnier /* clock is optional */ 122a00406b7SFabrice Gasnier if (ret != -ENOENT) 123a00406b7SFabrice Gasnier goto err_clk; 124a00406b7SFabrice Gasnier } else { 125a00406b7SFabrice Gasnier ret = regmap_mmio_attach_clk(regmap, clk); 126a00406b7SFabrice Gasnier if (ret) 127a00406b7SFabrice Gasnier goto err_attach; 128a00406b7SFabrice Gasnier } 12939233b7cSPaul Cercueil } 130a00406b7SFabrice Gasnier 131bdb0066dSPankaj Dubey syscon->regmap = regmap; 132bdb0066dSPankaj Dubey syscon->np = np; 133bdb0066dSPankaj Dubey 134bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 135bdb0066dSPankaj Dubey list_add_tail(&syscon->list, &syscon_list); 136bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 137bdb0066dSPankaj Dubey 138bdb0066dSPankaj Dubey return syscon; 139bdb0066dSPankaj Dubey 140a00406b7SFabrice Gasnier err_attach: 141a00406b7SFabrice Gasnier if (!IS_ERR(clk)) 142a00406b7SFabrice Gasnier clk_put(clk); 143a00406b7SFabrice Gasnier err_clk: 144a00406b7SFabrice Gasnier regmap_exit(regmap); 145bdb0066dSPankaj Dubey err_regmap: 146bdb0066dSPankaj Dubey iounmap(base); 147529a1101SMarc Zyngier kfree(syscon_config.name); 148bdb0066dSPankaj Dubey err_map: 149bdb0066dSPankaj Dubey kfree(syscon); 150bdb0066dSPankaj Dubey return ERR_PTR(ret); 15187d68730SDong Aisheng } 15287d68730SDong Aisheng 15339233b7cSPaul Cercueil static struct regmap *device_node_get_regmap(struct device_node *np, 15439233b7cSPaul Cercueil bool check_clk) 15587d68730SDong Aisheng { 156bdb0066dSPankaj Dubey struct syscon *entry, *syscon = NULL; 15787d68730SDong Aisheng 158bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 15987d68730SDong Aisheng 160bdb0066dSPankaj Dubey list_for_each_entry(entry, &syscon_list, list) 161bdb0066dSPankaj Dubey if (entry->np == np) { 162bdb0066dSPankaj Dubey syscon = entry; 163bdb0066dSPankaj Dubey break; 164bdb0066dSPankaj Dubey } 165bdb0066dSPankaj Dubey 166bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 167bdb0066dSPankaj Dubey 168bdb0066dSPankaj Dubey if (!syscon) 16939233b7cSPaul Cercueil syscon = of_syscon_register(np, check_clk); 170bdb0066dSPankaj Dubey 171bdb0066dSPankaj Dubey if (IS_ERR(syscon)) 172bdb0066dSPankaj Dubey return ERR_CAST(syscon); 17387d68730SDong Aisheng 17487d68730SDong Aisheng return syscon->regmap; 17587d68730SDong Aisheng } 17639233b7cSPaul Cercueil 17739233b7cSPaul Cercueil struct regmap *device_node_to_regmap(struct device_node *np) 17839233b7cSPaul Cercueil { 17939233b7cSPaul Cercueil return device_node_get_regmap(np, false); 18039233b7cSPaul Cercueil } 18139233b7cSPaul Cercueil EXPORT_SYMBOL_GPL(device_node_to_regmap); 18239233b7cSPaul Cercueil 18339233b7cSPaul Cercueil struct regmap *syscon_node_to_regmap(struct device_node *np) 18439233b7cSPaul Cercueil { 18539233b7cSPaul Cercueil if (!of_device_is_compatible(np, "syscon")) 18639233b7cSPaul Cercueil return ERR_PTR(-EINVAL); 18739233b7cSPaul Cercueil 18839233b7cSPaul Cercueil return device_node_get_regmap(np, true); 18939233b7cSPaul Cercueil } 19087d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap); 19187d68730SDong Aisheng 19287d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s) 19387d68730SDong Aisheng { 19487d68730SDong Aisheng struct device_node *syscon_np; 19587d68730SDong Aisheng struct regmap *regmap; 19687d68730SDong Aisheng 19787d68730SDong Aisheng syscon_np = of_find_compatible_node(NULL, NULL, s); 19887d68730SDong Aisheng if (!syscon_np) 19987d68730SDong Aisheng return ERR_PTR(-ENODEV); 20087d68730SDong Aisheng 20187d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 20287d68730SDong Aisheng of_node_put(syscon_np); 20387d68730SDong Aisheng 20487d68730SDong Aisheng return regmap; 20587d68730SDong Aisheng } 20687d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); 20787d68730SDong Aisheng 20887d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, 20987d68730SDong Aisheng const char *property) 21087d68730SDong Aisheng { 21187d68730SDong Aisheng struct device_node *syscon_np; 21287d68730SDong Aisheng struct regmap *regmap; 21387d68730SDong Aisheng 21445330bb4SPankaj Dubey if (property) 21587d68730SDong Aisheng syscon_np = of_parse_phandle(np, property, 0); 21645330bb4SPankaj Dubey else 21745330bb4SPankaj Dubey syscon_np = np; 21845330bb4SPankaj Dubey 21987d68730SDong Aisheng if (!syscon_np) 22087d68730SDong Aisheng return ERR_PTR(-ENODEV); 22187d68730SDong Aisheng 22287d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 22387d68730SDong Aisheng of_node_put(syscon_np); 22487d68730SDong Aisheng 22587d68730SDong Aisheng return regmap; 22687d68730SDong Aisheng } 22787d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); 22887d68730SDong Aisheng 2296a24f567SOrson Zhai struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np, 2306a24f567SOrson Zhai const char *property, 2316a24f567SOrson Zhai int arg_count, 2326a24f567SOrson Zhai unsigned int *out_args) 2336a24f567SOrson Zhai { 2346a24f567SOrson Zhai struct device_node *syscon_np; 2356a24f567SOrson Zhai struct of_phandle_args args; 2366a24f567SOrson Zhai struct regmap *regmap; 2376a24f567SOrson Zhai unsigned int index; 2386a24f567SOrson Zhai int rc; 2396a24f567SOrson Zhai 2406a24f567SOrson Zhai rc = of_parse_phandle_with_fixed_args(np, property, arg_count, 2416a24f567SOrson Zhai 0, &args); 2426a24f567SOrson Zhai if (rc) 2436a24f567SOrson Zhai return ERR_PTR(rc); 2446a24f567SOrson Zhai 2456a24f567SOrson Zhai syscon_np = args.np; 2466a24f567SOrson Zhai if (!syscon_np) 2476a24f567SOrson Zhai return ERR_PTR(-ENODEV); 2486a24f567SOrson Zhai 2496a24f567SOrson Zhai regmap = syscon_node_to_regmap(syscon_np); 2506a24f567SOrson Zhai for (index = 0; index < arg_count; index++) 2516a24f567SOrson Zhai out_args[index] = args.args[index]; 2526a24f567SOrson Zhai of_node_put(syscon_np); 2536a24f567SOrson Zhai 2546a24f567SOrson Zhai return regmap; 2556a24f567SOrson Zhai } 2566a24f567SOrson Zhai EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args); 2576a24f567SOrson Zhai 258*86b9d170SEnric Balletbo i Serra /* 259*86b9d170SEnric Balletbo i Serra * It behaves the same as syscon_regmap_lookup_by_phandle() except where 260*86b9d170SEnric Balletbo i Serra * there is no regmap phandle. In this case, instead of returning -ENODEV, 261*86b9d170SEnric Balletbo i Serra * the function returns NULL. 262*86b9d170SEnric Balletbo i Serra */ 263*86b9d170SEnric Balletbo i Serra struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np, 264*86b9d170SEnric Balletbo i Serra const char *property) 265*86b9d170SEnric Balletbo i Serra { 266*86b9d170SEnric Balletbo i Serra struct regmap *regmap; 267*86b9d170SEnric Balletbo i Serra 268*86b9d170SEnric Balletbo i Serra regmap = syscon_regmap_lookup_by_phandle(np, property); 269*86b9d170SEnric Balletbo i Serra if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV) 270*86b9d170SEnric Balletbo i Serra return NULL; 271*86b9d170SEnric Balletbo i Serra 272*86b9d170SEnric Balletbo i Serra return regmap; 273*86b9d170SEnric Balletbo i Serra } 274*86b9d170SEnric Balletbo i Serra EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional); 275*86b9d170SEnric Balletbo i Serra 276f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev) 27787d68730SDong Aisheng { 27887d68730SDong Aisheng struct device *dev = &pdev->dev; 27929f9b6cfSPawel Moll struct syscon_platform_data *pdata = dev_get_platdata(dev); 28087d68730SDong Aisheng struct syscon *syscon; 281c131045dSPhilipp Zabel struct regmap_config syscon_config = syscon_regmap_config; 2825ab3a89aSAlexander Shiyan struct resource *res; 283f10111ccSAlexander Shiyan void __iomem *base; 28487d68730SDong Aisheng 2855ab3a89aSAlexander Shiyan syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); 28687d68730SDong Aisheng if (!syscon) 28787d68730SDong Aisheng return -ENOMEM; 28887d68730SDong Aisheng 2895ab3a89aSAlexander Shiyan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2905ab3a89aSAlexander Shiyan if (!res) 2915ab3a89aSAlexander Shiyan return -ENOENT; 2925ab3a89aSAlexander Shiyan 293f10111ccSAlexander Shiyan base = devm_ioremap(dev, res->start, resource_size(res)); 294f10111ccSAlexander Shiyan if (!base) 2955ab3a89aSAlexander Shiyan return -ENOMEM; 29687d68730SDong Aisheng 297f2a19c5bSAndy Shevchenko syscon_config.max_register = resource_size(res) - 4; 29829f9b6cfSPawel Moll if (pdata) 299c131045dSPhilipp Zabel syscon_config.name = pdata->label; 300c131045dSPhilipp Zabel syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config); 30187d68730SDong Aisheng if (IS_ERR(syscon->regmap)) { 30287d68730SDong Aisheng dev_err(dev, "regmap init failed\n"); 30387d68730SDong Aisheng return PTR_ERR(syscon->regmap); 30487d68730SDong Aisheng } 30587d68730SDong Aisheng 30687d68730SDong Aisheng platform_set_drvdata(pdev, syscon); 30787d68730SDong Aisheng 30838d8974eSAlexander Shiyan dev_dbg(dev, "regmap %pR registered\n", res); 30987d68730SDong Aisheng 31087d68730SDong Aisheng return 0; 31187d68730SDong Aisheng } 31287d68730SDong Aisheng 3135ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = { 3145ab3a89aSAlexander Shiyan { "syscon", }, 3155ab3a89aSAlexander Shiyan { } 3165ab3a89aSAlexander Shiyan }; 31787d68730SDong Aisheng 31887d68730SDong Aisheng static struct platform_driver syscon_driver = { 31987d68730SDong Aisheng .driver = { 32087d68730SDong Aisheng .name = "syscon", 32187d68730SDong Aisheng }, 32287d68730SDong Aisheng .probe = syscon_probe, 3235ab3a89aSAlexander Shiyan .id_table = syscon_ids, 32487d68730SDong Aisheng }; 32587d68730SDong Aisheng 32687d68730SDong Aisheng static int __init syscon_init(void) 32787d68730SDong Aisheng { 32887d68730SDong Aisheng return platform_driver_register(&syscon_driver); 32987d68730SDong Aisheng } 33087d68730SDong Aisheng postcore_initcall(syscon_init); 331