syscon.c 7.1 KB

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