syscon.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include <trace/hooks/regmap.h>
  25. static struct platform_driver syscon_driver;
  26. static DEFINE_SPINLOCK(syscon_list_slock);
  27. static LIST_HEAD(syscon_list);
  28. struct syscon {
  29. struct device_node *np;
  30. struct regmap *regmap;
  31. struct list_head list;
  32. };
  33. static const struct regmap_config syscon_regmap_config = {
  34. .reg_bits = 32,
  35. .val_bits = 32,
  36. .reg_stride = 4,
  37. };
  38. static struct syscon *of_syscon_register(struct device_node *np, bool check_clk)
  39. {
  40. struct clk *clk;
  41. struct syscon *syscon;
  42. struct regmap *regmap;
  43. void __iomem *base;
  44. u32 reg_io_width;
  45. int ret;
  46. struct regmap_config syscon_config = syscon_regmap_config;
  47. struct resource res;
  48. syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
  49. if (!syscon)
  50. return ERR_PTR(-ENOMEM);
  51. if (of_address_to_resource(np, 0, &res)) {
  52. ret = -ENOMEM;
  53. goto err_map;
  54. }
  55. base = of_iomap(np, 0);
  56. if (!base) {
  57. ret = -ENOMEM;
  58. goto err_map;
  59. }
  60. /* Parse the device's DT node for an endianness specification */
  61. if (of_property_read_bool(np, "big-endian"))
  62. syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
  63. else if (of_property_read_bool(np, "little-endian"))
  64. syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
  65. else if (of_property_read_bool(np, "native-endian"))
  66. syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
  67. /*
  68. * search for reg-io-width property in DT. If it is not provided,
  69. * default to 4 bytes. regmap_init_mmio will return an error if values
  70. * are invalid so there is no need to check them here.
  71. */
  72. ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
  73. if (ret)
  74. reg_io_width = 4;
  75. ret = of_hwspin_lock_get_id(np, 0);
  76. if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
  77. syscon_config.use_hwlock = true;
  78. syscon_config.hwlock_id = ret;
  79. syscon_config.hwlock_mode = HWLOCK_IRQSTATE;
  80. } else if (ret < 0) {
  81. switch (ret) {
  82. case -ENOENT:
  83. /* Ignore missing hwlock, it's optional. */
  84. break;
  85. default:
  86. pr_err("Failed to retrieve valid hwlock: %d\n", ret);
  87. fallthrough;
  88. case -EPROBE_DEFER:
  89. goto err_regmap;
  90. }
  91. }
  92. syscon_config.name = kasprintf(GFP_KERNEL, "%pOFn@%pa", np, &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. trace_android_vh_regmap_update(&syscon_config, regmap);
  117. syscon->regmap = regmap;
  118. syscon->np = np;
  119. spin_lock(&syscon_list_slock);
  120. list_add_tail(&syscon->list, &syscon_list);
  121. spin_unlock(&syscon_list_slock);
  122. return syscon;
  123. err_attach:
  124. if (!IS_ERR(clk))
  125. clk_put(clk);
  126. err_clk:
  127. regmap_exit(regmap);
  128. err_regmap:
  129. iounmap(base);
  130. err_map:
  131. kfree(syscon);
  132. return ERR_PTR(ret);
  133. }
  134. static struct regmap *device_node_get_regmap(struct device_node *np,
  135. bool check_clk)
  136. {
  137. struct syscon *entry, *syscon = NULL;
  138. spin_lock(&syscon_list_slock);
  139. list_for_each_entry(entry, &syscon_list, list)
  140. if (entry->np == np) {
  141. syscon = entry;
  142. break;
  143. }
  144. spin_unlock(&syscon_list_slock);
  145. if (!syscon)
  146. syscon = of_syscon_register(np, check_clk);
  147. if (IS_ERR(syscon))
  148. return ERR_CAST(syscon);
  149. return syscon->regmap;
  150. }
  151. struct regmap *device_node_to_regmap(struct device_node *np)
  152. {
  153. return device_node_get_regmap(np, false);
  154. }
  155. EXPORT_SYMBOL_GPL(device_node_to_regmap);
  156. struct regmap *syscon_node_to_regmap(struct device_node *np)
  157. {
  158. if (!of_device_is_compatible(np, "syscon"))
  159. return ERR_PTR(-EINVAL);
  160. return device_node_get_regmap(np, true);
  161. }
  162. EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
  163. struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
  164. {
  165. struct device_node *syscon_np;
  166. struct regmap *regmap;
  167. syscon_np = of_find_compatible_node(NULL, NULL, s);
  168. if (!syscon_np)
  169. return ERR_PTR(-ENODEV);
  170. regmap = syscon_node_to_regmap(syscon_np);
  171. of_node_put(syscon_np);
  172. return regmap;
  173. }
  174. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
  175. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  176. const char *property)
  177. {
  178. struct device_node *syscon_np;
  179. struct regmap *regmap;
  180. if (property)
  181. syscon_np = of_parse_phandle(np, property, 0);
  182. else
  183. syscon_np = np;
  184. if (!syscon_np)
  185. return ERR_PTR(-ENODEV);
  186. regmap = syscon_node_to_regmap(syscon_np);
  187. of_node_put(syscon_np);
  188. return regmap;
  189. }
  190. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  191. struct regmap *syscon_regmap_lookup_by_phandle_args(struct device_node *np,
  192. const char *property,
  193. int arg_count,
  194. unsigned int *out_args)
  195. {
  196. struct device_node *syscon_np;
  197. struct of_phandle_args args;
  198. struct regmap *regmap;
  199. unsigned int index;
  200. int rc;
  201. rc = of_parse_phandle_with_fixed_args(np, property, arg_count,
  202. 0, &args);
  203. if (rc)
  204. return ERR_PTR(rc);
  205. syscon_np = args.np;
  206. if (!syscon_np)
  207. return ERR_PTR(-ENODEV);
  208. regmap = syscon_node_to_regmap(syscon_np);
  209. for (index = 0; index < arg_count; index++)
  210. out_args[index] = args.args[index];
  211. of_node_put(syscon_np);
  212. return regmap;
  213. }
  214. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_args);
  215. /*
  216. * It behaves the same as syscon_regmap_lookup_by_phandle() except where
  217. * there is no regmap phandle. In this case, instead of returning -ENODEV,
  218. * the function returns NULL.
  219. */
  220. struct regmap *syscon_regmap_lookup_by_phandle_optional(struct device_node *np,
  221. const char *property)
  222. {
  223. struct regmap *regmap;
  224. regmap = syscon_regmap_lookup_by_phandle(np, property);
  225. if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
  226. return NULL;
  227. return regmap;
  228. }
  229. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle_optional);
  230. static int syscon_probe(struct platform_device *pdev)
  231. {
  232. struct device *dev = &pdev->dev;
  233. struct syscon_platform_data *pdata = dev_get_platdata(dev);
  234. struct syscon *syscon;
  235. struct regmap_config syscon_config = syscon_regmap_config;
  236. struct resource *res;
  237. void __iomem *base;
  238. syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
  239. if (!syscon)
  240. return -ENOMEM;
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (!res)
  243. return -ENOENT;
  244. base = devm_ioremap(dev, res->start, resource_size(res));
  245. if (!base)
  246. return -ENOMEM;
  247. syscon_config.max_register = resource_size(res) - 4;
  248. if (pdata)
  249. syscon_config.name = pdata->label;
  250. syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
  251. if (IS_ERR(syscon->regmap)) {
  252. dev_err(dev, "regmap init failed\n");
  253. return PTR_ERR(syscon->regmap);
  254. }
  255. platform_set_drvdata(pdev, syscon);
  256. dev_dbg(dev, "regmap %pR registered\n", res);
  257. return 0;
  258. }
  259. static const struct platform_device_id syscon_ids[] = {
  260. { "syscon", },
  261. { }
  262. };
  263. static struct platform_driver syscon_driver = {
  264. .driver = {
  265. .name = "syscon",
  266. },
  267. .probe = syscon_probe,
  268. .id_table = syscon_ids,
  269. };
  270. static int __init syscon_init(void)
  271. {
  272. return platform_driver_register(&syscon_driver);
  273. }
  274. postcore_initcall(syscon_init);