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 63452d0741SHector Martin base = of_iomap(np, 0); 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 104*7ff7d5ffSAndy Shevchenko syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start); 105db2fb60cSDamien Riegel syscon_config.reg_stride = reg_io_width; 106db2fb60cSDamien Riegel syscon_config.val_bits = reg_io_width * 8; 107ca668f0eSPhilipp Zabel syscon_config.max_register = resource_size(&res) - reg_io_width; 108db2fb60cSDamien Riegel 109bdb0066dSPankaj Dubey regmap = regmap_init_mmio(NULL, base, &syscon_config); 11056a11881SLimeng kfree(syscon_config.name); 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); 147bdb0066dSPankaj Dubey err_map: 148bdb0066dSPankaj Dubey kfree(syscon); 149bdb0066dSPankaj Dubey return ERR_PTR(ret); 15087d68730SDong Aisheng } 15187d68730SDong Aisheng 15239233b7cSPaul Cercueil static struct regmap *device_node_get_regmap(struct device_node *np, 15339233b7cSPaul Cercueil bool check_clk) 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) 16839233b7cSPaul Cercueil syscon = of_syscon_register(np, check_clk); 169bdb0066dSPankaj Dubey 170bdb0066dSPankaj Dubey if (IS_ERR(syscon)) 171bdb0066dSPankaj Dubey return ERR_CAST(syscon); 17287d68730SDong Aisheng 17387d68730SDong Aisheng return syscon->regmap; 17487d68730SDong Aisheng } 17539233b7cSPaul Cercueil 17639233b7cSPaul Cercueil struct regmap *device_node_to_regmap(struct device_node *np) 17739233b7cSPaul Cercueil { 17839233b7cSPaul Cercueil return device_node_get_regmap(np, false); 17939233b7cSPaul Cercueil } 18039233b7cSPaul Cercueil EXPORT_SYMBOL_GPL(device_node_to_regmap); 18139233b7cSPaul Cercueil 18239233b7cSPaul Cercueil struct regmap *syscon_node_to_regmap(struct device_node *np) 18339233b7cSPaul Cercueil { 18439233b7cSPaul Cercueil if (!of_device_is_compatible(np, "syscon")) 18539233b7cSPaul Cercueil return ERR_PTR(-EINVAL); 18639233b7cSPaul Cercueil 18739233b7cSPaul Cercueil return device_node_get_regmap(np, true); 18839233b7cSPaul Cercueil } 18987d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap); 19087d68730SDong Aisheng 19187d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s) 19287d68730SDong Aisheng { 19387d68730SDong Aisheng struct device_node *syscon_np; 19487d68730SDong Aisheng struct regmap *regmap; 19587d68730SDong Aisheng 19687d68730SDong Aisheng syscon_np = of_find_compatible_node(NULL, NULL, s); 19787d68730SDong Aisheng if (!syscon_np) 19887d68730SDong Aisheng return ERR_PTR(-ENODEV); 19987d68730SDong Aisheng 20087d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 20187d68730SDong Aisheng of_node_put(syscon_np); 20287d68730SDong Aisheng 20387d68730SDong Aisheng return regmap; 20487d68730SDong Aisheng } 20587d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); 20687d68730SDong Aisheng 20787d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, 20887d68730SDong Aisheng const char *property) 20987d68730SDong Aisheng { 21087d68730SDong Aisheng struct device_node *syscon_np; 21187d68730SDong Aisheng struct regmap *regmap; 21287d68730SDong Aisheng 21345330bb4SPankaj Dubey if (property) 21487d68730SDong Aisheng syscon_np = of_parse_phandle(np, property, 0); 21545330bb4SPankaj Dubey else 21645330bb4SPankaj Dubey syscon_np = np; 21745330bb4SPankaj Dubey 21887d68730SDong Aisheng if (!syscon_np) 21987d68730SDong Aisheng return ERR_PTR(-ENODEV); 22087d68730SDong Aisheng 22187d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 22287d68730SDong Aisheng of_node_put(syscon_np); 22387d68730SDong Aisheng 22487d68730SDong Aisheng return regmap; 22587d68730SDong Aisheng } 22687d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); 22787d68730SDong Aisheng 2286a24f567SOrson Zhai struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np, 2296a24f567SOrson Zhai const char *property, 2306a24f567SOrson Zhai int arg_count, 2316a24f567SOrson Zhai unsigned int *out_args) 2326a24f567SOrson Zhai { 2336a24f567SOrson Zhai struct device_node *syscon_np; 2346a24f567SOrson Zhai struct of_phandle_args args; 2356a24f567SOrson Zhai struct regmap *regmap; 2366a24f567SOrson Zhai unsigned int index; 2376a24f567SOrson Zhai int rc; 2386a24f567SOrson Zhai 2396a24f567SOrson Zhai rc = of_parse_phandle_with_fixed_args(np, property, arg_count, 2406a24f567SOrson Zhai 0, &args); 2416a24f567SOrson Zhai if (rc) 2426a24f567SOrson Zhai return ERR_PTR(rc); 2436a24f567SOrson Zhai 2446a24f567SOrson Zhai syscon_np = args.np; 2456a24f567SOrson Zhai if (!syscon_np) 2466a24f567SOrson Zhai return ERR_PTR(-ENODEV); 2476a24f567SOrson Zhai 2486a24f567SOrson Zhai regmap = syscon_node_to_regmap(syscon_np); 2496a24f567SOrson Zhai for (index = 0; index < arg_count; index++) 2506a24f567SOrson Zhai out_args[index] = args.args[index]; 2516a24f567SOrson Zhai of_node_put(syscon_np); 2526a24f567SOrson Zhai 2536a24f567SOrson Zhai return regmap; 2546a24f567SOrson Zhai } 2556a24f567SOrson Zhai EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args); 2566a24f567SOrson Zhai 25786b9d170SEnric Balletbo i Serra /* 25886b9d170SEnric Balletbo i Serra * It behaves the same as syscon_regmap_lookup_by_phandle() except where 25986b9d170SEnric Balletbo i Serra * there is no regmap phandle. In this case, instead of returning -ENODEV, 26086b9d170SEnric Balletbo i Serra * the function returns NULL. 26186b9d170SEnric Balletbo i Serra */ 26286b9d170SEnric Balletbo i Serra struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np, 26386b9d170SEnric Balletbo i Serra const char *property) 26486b9d170SEnric Balletbo i Serra { 26586b9d170SEnric Balletbo i Serra struct regmap *regmap; 26686b9d170SEnric Balletbo i Serra 26786b9d170SEnric Balletbo i Serra regmap = syscon_regmap_lookup_by_phandle(np, property); 26886b9d170SEnric Balletbo i Serra if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV) 26986b9d170SEnric Balletbo i Serra return NULL; 27086b9d170SEnric Balletbo i Serra 27186b9d170SEnric Balletbo i Serra return regmap; 27286b9d170SEnric Balletbo i Serra } 27386b9d170SEnric Balletbo i Serra EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional); 27486b9d170SEnric Balletbo i Serra 275f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev) 27687d68730SDong Aisheng { 27787d68730SDong Aisheng struct device *dev = &pdev->dev; 27829f9b6cfSPawel Moll struct syscon_platform_data *pdata = dev_get_platdata(dev); 27987d68730SDong Aisheng struct syscon *syscon; 280c131045dSPhilipp Zabel struct regmap_config syscon_config = syscon_regmap_config; 2815ab3a89aSAlexander Shiyan struct resource *res; 282f10111ccSAlexander Shiyan void __iomem *base; 28387d68730SDong Aisheng 2845ab3a89aSAlexander Shiyan syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); 28587d68730SDong Aisheng if (!syscon) 28687d68730SDong Aisheng return -ENOMEM; 28787d68730SDong Aisheng 2885ab3a89aSAlexander Shiyan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2895ab3a89aSAlexander Shiyan if (!res) 2905ab3a89aSAlexander Shiyan return -ENOENT; 2915ab3a89aSAlexander Shiyan 292f10111ccSAlexander Shiyan base = devm_ioremap(dev, res->start, resource_size(res)); 293f10111ccSAlexander Shiyan if (!base) 2945ab3a89aSAlexander Shiyan return -ENOMEM; 29587d68730SDong Aisheng 296f2a19c5bSAndy Shevchenko syscon_config.max_register = resource_size(res) - 4; 29729f9b6cfSPawel Moll if (pdata) 298c131045dSPhilipp Zabel syscon_config.name = pdata->label; 299c131045dSPhilipp Zabel syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config); 30087d68730SDong Aisheng if (IS_ERR(syscon->regmap)) { 30187d68730SDong Aisheng dev_err(dev, "regmap init failed\n"); 30287d68730SDong Aisheng return PTR_ERR(syscon->regmap); 30387d68730SDong Aisheng } 30487d68730SDong Aisheng 30587d68730SDong Aisheng platform_set_drvdata(pdev, syscon); 30687d68730SDong Aisheng 30738d8974eSAlexander Shiyan dev_dbg(dev, "regmap %pR registered\n", res); 30887d68730SDong Aisheng 30987d68730SDong Aisheng return 0; 31087d68730SDong Aisheng } 31187d68730SDong Aisheng 3125ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = { 3135ab3a89aSAlexander Shiyan { "syscon", }, 3145ab3a89aSAlexander Shiyan { } 3155ab3a89aSAlexander Shiyan }; 31687d68730SDong Aisheng 31787d68730SDong Aisheng static struct platform_driver syscon_driver = { 31887d68730SDong Aisheng .driver = { 31987d68730SDong Aisheng .name = "syscon", 32087d68730SDong Aisheng }, 32187d68730SDong Aisheng .probe = syscon_probe, 3225ab3a89aSAlexander Shiyan .id_table = syscon_ids, 32387d68730SDong Aisheng }; 32487d68730SDong Aisheng 32587d68730SDong Aisheng static int __init syscon_init(void) 32687d68730SDong Aisheng { 32787d68730SDong Aisheng return platform_driver_register(&syscon_driver); 32887d68730SDong Aisheng } 32987d68730SDong Aisheng postcore_initcall(syscon_init); 330