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> 237d1e3bd9SJeremy Kerr #include <linux/reset.h> 2475177deeSFabio Estevam #include <linux/mfd/syscon.h> 25bdb0066dSPankaj Dubey #include <linux/slab.h> 2687d68730SDong Aisheng 2787d68730SDong Aisheng static struct platform_driver syscon_driver; 2887d68730SDong Aisheng 29bdb0066dSPankaj Dubey static DEFINE_SPINLOCK(syscon_list_slock); 30bdb0066dSPankaj Dubey static LIST_HEAD(syscon_list); 31bdb0066dSPankaj Dubey 3287d68730SDong Aisheng struct syscon { 33bdb0066dSPankaj Dubey struct device_node *np; 3487d68730SDong Aisheng struct regmap *regmap; 357d1e3bd9SJeremy Kerr struct reset_control *reset; 36bdb0066dSPankaj Dubey struct list_head list; 3787d68730SDong Aisheng }; 3887d68730SDong Aisheng 39c131045dSPhilipp Zabel static const struct regmap_config syscon_regmap_config = { 40bdb0066dSPankaj Dubey .reg_bits = 32, 41bdb0066dSPankaj Dubey .val_bits = 32, 42bdb0066dSPankaj Dubey .reg_stride = 4, 43bdb0066dSPankaj Dubey }; 4487d68730SDong Aisheng 457d1e3bd9SJeremy Kerr static struct syscon *of_syscon_register(struct device_node *np, bool check_res) 46bdb0066dSPankaj Dubey { 47a00406b7SFabrice Gasnier struct clk *clk; 48bdb0066dSPankaj Dubey struct syscon *syscon; 49bdb0066dSPankaj Dubey struct regmap *regmap; 50bdb0066dSPankaj Dubey void __iomem *base; 51db2fb60cSDamien Riegel u32 reg_io_width; 52bdb0066dSPankaj Dubey int ret; 53bdb0066dSPankaj Dubey struct regmap_config syscon_config = syscon_regmap_config; 54ca668f0eSPhilipp Zabel struct resource res; 557d1e3bd9SJeremy Kerr struct reset_control *reset; 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 66452d0741SHector Martin base = of_iomap(np, 0); 67bdb0066dSPankaj Dubey if (!base) { 68bdb0066dSPankaj Dubey ret = -ENOMEM; 69bdb0066dSPankaj Dubey goto err_map; 70bdb0066dSPankaj Dubey } 71bdb0066dSPankaj Dubey 72ca4582c2SJason A. Donenfeld /* Parse the device's DT node for an endianness specification */ 73ca4582c2SJason A. Donenfeld if (of_property_read_bool(np, "big-endian")) 74ca4582c2SJason A. Donenfeld syscon_config.val_format_endian = REGMAP_ENDIAN_BIG; 75ca4582c2SJason A. Donenfeld else if (of_property_read_bool(np, "little-endian")) 76ca4582c2SJason A. Donenfeld syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE; 77ca4582c2SJason A. Donenfeld else if (of_property_read_bool(np, "native-endian")) 78ca4582c2SJason A. Donenfeld syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE; 79ca4582c2SJason A. Donenfeld 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); 101df561f66SGustavo A. R. Silva fallthrough; 1023bafc09eSBaolin Wang case -EPROBE_DEFER: 1033bafc09eSBaolin Wang goto err_regmap; 1043bafc09eSBaolin Wang } 1053bafc09eSBaolin Wang } 1063bafc09eSBaolin Wang 1077ff7d5ffSAndy Shevchenko syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &res.start); 10841673c66SKunwu Chan if (!syscon_config.name) { 10941673c66SKunwu Chan ret = -ENOMEM; 11041673c66SKunwu Chan goto err_regmap; 11141673c66SKunwu Chan } 112db2fb60cSDamien Riegel syscon_config.reg_stride = reg_io_width; 113db2fb60cSDamien Riegel syscon_config.val_bits = reg_io_width * 8; 114ca668f0eSPhilipp Zabel syscon_config.max_register = resource_size(&res) - reg_io_width; 115db2fb60cSDamien Riegel 116bdb0066dSPankaj Dubey regmap = regmap_init_mmio(NULL, base, &syscon_config); 11756a11881SLimeng kfree(syscon_config.name); 118bdb0066dSPankaj Dubey if (IS_ERR(regmap)) { 119bdb0066dSPankaj Dubey pr_err("regmap init failed\n"); 120bdb0066dSPankaj Dubey ret = PTR_ERR(regmap); 121bdb0066dSPankaj Dubey goto err_regmap; 122bdb0066dSPankaj Dubey } 123bdb0066dSPankaj Dubey 1247d1e3bd9SJeremy Kerr if (check_res) { 125a00406b7SFabrice Gasnier clk = of_clk_get(np, 0); 126a00406b7SFabrice Gasnier if (IS_ERR(clk)) { 127a00406b7SFabrice Gasnier ret = PTR_ERR(clk); 128a00406b7SFabrice Gasnier /* clock is optional */ 129a00406b7SFabrice Gasnier if (ret != -ENOENT) 130a00406b7SFabrice Gasnier goto err_clk; 131a00406b7SFabrice Gasnier } else { 132a00406b7SFabrice Gasnier ret = regmap_mmio_attach_clk(regmap, clk); 133a00406b7SFabrice Gasnier if (ret) 1347d1e3bd9SJeremy Kerr goto err_attach_clk; 135a00406b7SFabrice Gasnier } 1367d1e3bd9SJeremy Kerr 1377d1e3bd9SJeremy Kerr reset = of_reset_control_get_optional_exclusive(np, NULL); 1387d1e3bd9SJeremy Kerr if (IS_ERR(reset)) { 1397d1e3bd9SJeremy Kerr ret = PTR_ERR(reset); 1407d1e3bd9SJeremy Kerr goto err_attach_clk; 1417d1e3bd9SJeremy Kerr } 1427d1e3bd9SJeremy Kerr 1437d1e3bd9SJeremy Kerr ret = reset_control_deassert(reset); 1447d1e3bd9SJeremy Kerr if (ret) 1457d1e3bd9SJeremy Kerr goto err_reset; 14639233b7cSPaul Cercueil } 147a00406b7SFabrice Gasnier 148bdb0066dSPankaj Dubey syscon->regmap = regmap; 149bdb0066dSPankaj Dubey syscon->np = np; 150bdb0066dSPankaj Dubey 151bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 152bdb0066dSPankaj Dubey list_add_tail(&syscon->list, &syscon_list); 153bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 154bdb0066dSPankaj Dubey 155bdb0066dSPankaj Dubey return syscon; 156bdb0066dSPankaj Dubey 1577d1e3bd9SJeremy Kerr err_reset: 1587d1e3bd9SJeremy Kerr reset_control_put(reset); 1597d1e3bd9SJeremy Kerr err_attach_clk: 160a00406b7SFabrice Gasnier if (!IS_ERR(clk)) 161a00406b7SFabrice Gasnier clk_put(clk); 162a00406b7SFabrice Gasnier err_clk: 163a00406b7SFabrice Gasnier regmap_exit(regmap); 164bdb0066dSPankaj Dubey err_regmap: 165bdb0066dSPankaj Dubey iounmap(base); 166bdb0066dSPankaj Dubey err_map: 167bdb0066dSPankaj Dubey kfree(syscon); 168bdb0066dSPankaj Dubey return ERR_PTR(ret); 16987d68730SDong Aisheng } 17087d68730SDong Aisheng 17139233b7cSPaul Cercueil static struct regmap *device_node_get_regmap(struct device_node *np, 1727d1e3bd9SJeremy Kerr bool check_res) 17387d68730SDong Aisheng { 174bdb0066dSPankaj Dubey struct syscon *entry, *syscon = NULL; 17587d68730SDong Aisheng 176bdb0066dSPankaj Dubey spin_lock(&syscon_list_slock); 17787d68730SDong Aisheng 178bdb0066dSPankaj Dubey list_for_each_entry(entry, &syscon_list, list) 179bdb0066dSPankaj Dubey if (entry->np == np) { 180bdb0066dSPankaj Dubey syscon = entry; 181bdb0066dSPankaj Dubey break; 182bdb0066dSPankaj Dubey } 183bdb0066dSPankaj Dubey 184bdb0066dSPankaj Dubey spin_unlock(&syscon_list_slock); 185bdb0066dSPankaj Dubey 186bdb0066dSPankaj Dubey if (!syscon) 1877d1e3bd9SJeremy Kerr syscon = of_syscon_register(np, check_res); 188bdb0066dSPankaj Dubey 189bdb0066dSPankaj Dubey if (IS_ERR(syscon)) 190bdb0066dSPankaj Dubey return ERR_CAST(syscon); 19187d68730SDong Aisheng 19287d68730SDong Aisheng return syscon->regmap; 19387d68730SDong Aisheng } 19439233b7cSPaul Cercueil 19539233b7cSPaul Cercueil struct regmap *device_node_to_regmap(struct device_node *np) 19639233b7cSPaul Cercueil { 19739233b7cSPaul Cercueil return device_node_get_regmap(np, false); 19839233b7cSPaul Cercueil } 19939233b7cSPaul Cercueil EXPORT_SYMBOL_GPL(device_node_to_regmap); 20039233b7cSPaul Cercueil 20139233b7cSPaul Cercueil struct regmap *syscon_node_to_regmap(struct device_node *np) 20239233b7cSPaul Cercueil { 20339233b7cSPaul Cercueil if (!of_device_is_compatible(np, "syscon")) 20439233b7cSPaul Cercueil return ERR_PTR(-EINVAL); 20539233b7cSPaul Cercueil 20639233b7cSPaul Cercueil return device_node_get_regmap(np, true); 20739233b7cSPaul Cercueil } 20887d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_node_to_regmap); 20987d68730SDong Aisheng 21087d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_compatible(const char *s) 21187d68730SDong Aisheng { 21287d68730SDong Aisheng struct device_node *syscon_np; 21387d68730SDong Aisheng struct regmap *regmap; 21487d68730SDong Aisheng 21587d68730SDong Aisheng syscon_np = of_find_compatible_node(NULL, NULL, s); 21687d68730SDong Aisheng if (!syscon_np) 21787d68730SDong Aisheng return ERR_PTR(-ENODEV); 21887d68730SDong Aisheng 21987d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 22087d68730SDong Aisheng of_node_put(syscon_np); 22187d68730SDong Aisheng 22287d68730SDong Aisheng return regmap; 22387d68730SDong Aisheng } 22487d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible); 22587d68730SDong Aisheng 22687d68730SDong Aisheng struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np, 22787d68730SDong Aisheng const char *property) 22887d68730SDong Aisheng { 22987d68730SDong Aisheng struct device_node *syscon_np; 23087d68730SDong Aisheng struct regmap *regmap; 23187d68730SDong Aisheng 23245330bb4SPankaj Dubey if (property) 23387d68730SDong Aisheng syscon_np = of_parse_phandle(np, property, 0); 23445330bb4SPankaj Dubey else 23545330bb4SPankaj Dubey syscon_np = np; 23645330bb4SPankaj Dubey 23787d68730SDong Aisheng if (!syscon_np) 23887d68730SDong Aisheng return ERR_PTR(-ENODEV); 23987d68730SDong Aisheng 24087d68730SDong Aisheng regmap = syscon_node_to_regmap(syscon_np); 241*d2b0680cSPeter Griffin 242*d2b0680cSPeter Griffin if (property) 24387d68730SDong Aisheng of_node_put(syscon_np); 24487d68730SDong Aisheng 24587d68730SDong Aisheng return regmap; 24687d68730SDong Aisheng } 24787d68730SDong Aisheng EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle); 24887d68730SDong Aisheng 2496a24f567SOrson Zhai struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np, 2506a24f567SOrson Zhai const char *property, 2516a24f567SOrson Zhai int arg_count, 2526a24f567SOrson Zhai unsigned int *out_args) 2536a24f567SOrson Zhai { 2546a24f567SOrson Zhai struct device_node *syscon_np; 2556a24f567SOrson Zhai struct of_phandle_args args; 2566a24f567SOrson Zhai struct regmap *regmap; 2576a24f567SOrson Zhai unsigned int index; 2586a24f567SOrson Zhai int rc; 2596a24f567SOrson Zhai 2606a24f567SOrson Zhai rc = of_parse_phandle_with_fixed_args(np, property, arg_count, 2616a24f567SOrson Zhai 0, &args); 2626a24f567SOrson Zhai if (rc) 2636a24f567SOrson Zhai return ERR_PTR(rc); 2646a24f567SOrson Zhai 2656a24f567SOrson Zhai syscon_np = args.np; 2666a24f567SOrson Zhai if (!syscon_np) 2676a24f567SOrson Zhai return ERR_PTR(-ENODEV); 2686a24f567SOrson Zhai 2696a24f567SOrson Zhai regmap = syscon_node_to_regmap(syscon_np); 2706a24f567SOrson Zhai for (index = 0; index < arg_count; index++) 2716a24f567SOrson Zhai out_args[index] = args.args[index]; 2726a24f567SOrson Zhai of_node_put(syscon_np); 2736a24f567SOrson Zhai 2746a24f567SOrson Zhai return regmap; 2756a24f567SOrson Zhai } 2766a24f567SOrson Zhai EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args); 2776a24f567SOrson Zhai 27886b9d170SEnric Balletbo i Serra /* 27986b9d170SEnric Balletbo i Serra * It behaves the same as syscon_regmap_lookup_by_phandle() except where 28086b9d170SEnric Balletbo i Serra * there is no regmap phandle. In this case, instead of returning -ENODEV, 28186b9d170SEnric Balletbo i Serra * the function returns NULL. 28286b9d170SEnric Balletbo i Serra */ 28386b9d170SEnric Balletbo i Serra struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np, 28486b9d170SEnric Balletbo i Serra const char *property) 28586b9d170SEnric Balletbo i Serra { 28686b9d170SEnric Balletbo i Serra struct regmap *regmap; 28786b9d170SEnric Balletbo i Serra 28886b9d170SEnric Balletbo i Serra regmap = syscon_regmap_lookup_by_phandle(np, property); 28986b9d170SEnric Balletbo i Serra if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV) 29086b9d170SEnric Balletbo i Serra return NULL; 29186b9d170SEnric Balletbo i Serra 29286b9d170SEnric Balletbo i Serra return regmap; 29386b9d170SEnric Balletbo i Serra } 29486b9d170SEnric Balletbo i Serra EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional); 29586b9d170SEnric Balletbo i Serra 296f791be49SBill Pemberton static int syscon_probe(struct platform_device *pdev) 29787d68730SDong Aisheng { 29887d68730SDong Aisheng struct device *dev = &pdev->dev; 29929f9b6cfSPawel Moll struct syscon_platform_data *pdata = dev_get_platdata(dev); 30087d68730SDong Aisheng struct syscon *syscon; 301c131045dSPhilipp Zabel struct regmap_config syscon_config = syscon_regmap_config; 3025ab3a89aSAlexander Shiyan struct resource *res; 303f10111ccSAlexander Shiyan void __iomem *base; 30487d68730SDong Aisheng 3055ab3a89aSAlexander Shiyan syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL); 30687d68730SDong Aisheng if (!syscon) 30787d68730SDong Aisheng return -ENOMEM; 30887d68730SDong Aisheng 3095ab3a89aSAlexander Shiyan res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3105ab3a89aSAlexander Shiyan if (!res) 3115ab3a89aSAlexander Shiyan return -ENOENT; 3125ab3a89aSAlexander Shiyan 313f10111ccSAlexander Shiyan base = devm_ioremap(dev, res->start, resource_size(res)); 314f10111ccSAlexander Shiyan if (!base) 3155ab3a89aSAlexander Shiyan return -ENOMEM; 31687d68730SDong Aisheng 317f2a19c5bSAndy Shevchenko syscon_config.max_register = resource_size(res) - 4; 31829f9b6cfSPawel Moll if (pdata) 319c131045dSPhilipp Zabel syscon_config.name = pdata->label; 320c131045dSPhilipp Zabel syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config); 32187d68730SDong Aisheng if (IS_ERR(syscon->regmap)) { 32287d68730SDong Aisheng dev_err(dev, "regmap init failed\n"); 32387d68730SDong Aisheng return PTR_ERR(syscon->regmap); 32487d68730SDong Aisheng } 32587d68730SDong Aisheng 32687d68730SDong Aisheng platform_set_drvdata(pdev, syscon); 32787d68730SDong Aisheng 32838d8974eSAlexander Shiyan dev_dbg(dev, "regmap %pR registered\n", res); 32987d68730SDong Aisheng 33087d68730SDong Aisheng return 0; 33187d68730SDong Aisheng } 33287d68730SDong Aisheng 3335ab3a89aSAlexander Shiyan static const struct platform_device_id syscon_ids[] = { 3345ab3a89aSAlexander Shiyan { "syscon", }, 3355ab3a89aSAlexander Shiyan { } 3365ab3a89aSAlexander Shiyan }; 33787d68730SDong Aisheng 33887d68730SDong Aisheng static struct platform_driver syscon_driver = { 33987d68730SDong Aisheng .driver = { 34087d68730SDong Aisheng .name = "syscon", 34187d68730SDong Aisheng }, 34287d68730SDong Aisheng .probe = syscon_probe, 3435ab3a89aSAlexander Shiyan .id_table = syscon_ids, 34487d68730SDong Aisheng }; 34587d68730SDong Aisheng 34687d68730SDong Aisheng static int __init syscon_init(void) 34787d68730SDong Aisheng { 34887d68730SDong Aisheng return platform_driver_register(&syscon_driver); 34987d68730SDong Aisheng } 35087d68730SDong Aisheng postcore_initcall(syscon_init); 351