syscon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * System Control Driver
  3. *
  4. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  5. * Copyright (C) 2012 Linaro Ltd.
  6. *
  7. * Author: Dong Aisheng <dong.aisheng@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_data/syscon.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/mfd/syscon.h>
  25. #include <linux/slab.h>
  26. static struct platform_driver syscon_driver;
  27. static DEFINE_SPINLOCK(syscon_list_slock);
  28. static LIST_HEAD(syscon_list);
  29. struct syscon {
  30. struct device_node *np;
  31. struct regmap *regmap;
  32. struct list_head list;
  33. };
  34. static const struct regmap_config syscon_regmap_config = {
  35. .reg_bits = 32,
  36. .val_bits = 32,
  37. .reg_stride = 4,
  38. };
  39. static struct syscon *of_syscon_register(struct device_node *np)
  40. {
  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. if (!of_device_is_compatible(np, "syscon"))
  49. return ERR_PTR(-EINVAL);
  50. syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
  51. if (!syscon)
  52. return ERR_PTR(-ENOMEM);
  53. if (of_address_to_resource(np, 0, &res)) {
  54. ret = -ENOMEM;
  55. goto err_map;
  56. }
  57. base = ioremap(res.start, resource_size(&res));
  58. if (!base) {
  59. ret = -ENOMEM;
  60. goto err_map;
  61. }
  62. /* Parse the device's DT node for an endianness specification */
  63. if (of_property_read_bool(np, "big-endian"))
  64. syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
  65. else if (of_property_read_bool(np, "little-endian"))
  66. syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
  67. else if (of_property_read_bool(np, "native-endian"))
  68. syscon_config.val_format_endian = REGMAP_ENDIAN_NATIVE;
  69. /*
  70. * search for reg-io-width property in DT. If it is not provided,
  71. * default to 4 bytes. regmap_init_mmio will return an error if values
  72. * are invalid so there is no need to check them here.
  73. */
  74. ret = of_property_read_u32(np, "reg-io-width", &reg_io_width);
  75. if (ret)
  76. reg_io_width = 4;
  77. syscon_config.reg_stride = reg_io_width;
  78. syscon_config.val_bits = reg_io_width * 8;
  79. syscon_config.max_register = resource_size(&res) - reg_io_width;
  80. regmap = regmap_init_mmio(NULL, base, &syscon_config);
  81. if (IS_ERR(regmap)) {
  82. pr_err("regmap init failed\n");
  83. ret = PTR_ERR(regmap);
  84. goto err_regmap;
  85. }
  86. syscon->regmap = regmap;
  87. syscon->np = np;
  88. spin_lock(&syscon_list_slock);
  89. list_add_tail(&syscon->list, &syscon_list);
  90. spin_unlock(&syscon_list_slock);
  91. return syscon;
  92. err_regmap:
  93. iounmap(base);
  94. err_map:
  95. kfree(syscon);
  96. return ERR_PTR(ret);
  97. }
  98. struct regmap *syscon_node_to_regmap(struct device_node *np)
  99. {
  100. struct syscon *entry, *syscon = NULL;
  101. spin_lock(&syscon_list_slock);
  102. list_for_each_entry(entry, &syscon_list, list)
  103. if (entry->np == np) {
  104. syscon = entry;
  105. break;
  106. }
  107. spin_unlock(&syscon_list_slock);
  108. if (!syscon)
  109. syscon = of_syscon_register(np);
  110. if (IS_ERR(syscon))
  111. return ERR_CAST(syscon);
  112. return syscon->regmap;
  113. }
  114. EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
  115. struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
  116. {
  117. struct device_node *syscon_np;
  118. struct regmap *regmap;
  119. syscon_np = of_find_compatible_node(NULL, NULL, s);
  120. if (!syscon_np)
  121. return ERR_PTR(-ENODEV);
  122. regmap = syscon_node_to_regmap(syscon_np);
  123. of_node_put(syscon_np);
  124. return regmap;
  125. }
  126. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
  127. static int syscon_match_pdevname(struct device *dev, void *data)
  128. {
  129. return !strcmp(dev_name(dev), (const char *)data);
  130. }
  131. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  132. {
  133. struct device *dev;
  134. struct syscon *syscon;
  135. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  136. syscon_match_pdevname);
  137. if (!dev)
  138. return ERR_PTR(-EPROBE_DEFER);
  139. syscon = dev_get_drvdata(dev);
  140. return syscon->regmap;
  141. }
  142. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  143. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  144. const char *property)
  145. {
  146. struct device_node *syscon_np;
  147. struct regmap *regmap;
  148. if (property)
  149. syscon_np = of_parse_phandle(np, property, 0);
  150. else
  151. syscon_np = np;
  152. if (!syscon_np)
  153. return ERR_PTR(-ENODEV);
  154. regmap = syscon_node_to_regmap(syscon_np);
  155. of_node_put(syscon_np);
  156. return regmap;
  157. }
  158. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  159. static int syscon_probe(struct platform_device *pdev)
  160. {
  161. struct device *dev = &pdev->dev;
  162. struct syscon_platform_data *pdata = dev_get_platdata(dev);
  163. struct syscon *syscon;
  164. struct regmap_config syscon_config = syscon_regmap_config;
  165. struct resource *res;
  166. void __iomem *base;
  167. syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
  168. if (!syscon)
  169. return -ENOMEM;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (!res)
  172. return -ENOENT;
  173. base = devm_ioremap(dev, res->start, resource_size(res));
  174. if (!base)
  175. return -ENOMEM;
  176. syscon_config.max_register = res->end - res->start - 3;
  177. if (pdata)
  178. syscon_config.name = pdata->label;
  179. syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
  180. if (IS_ERR(syscon->regmap)) {
  181. dev_err(dev, "regmap init failed\n");
  182. return PTR_ERR(syscon->regmap);
  183. }
  184. platform_set_drvdata(pdev, syscon);
  185. dev_dbg(dev, "regmap %pR registered\n", res);
  186. return 0;
  187. }
  188. static const struct platform_device_id syscon_ids[] = {
  189. { "syscon", },
  190. { }
  191. };
  192. static struct platform_driver syscon_driver = {
  193. .driver = {
  194. .name = "syscon",
  195. },
  196. .probe = syscon_probe,
  197. .id_table = syscon_ids,
  198. };
  199. static int __init syscon_init(void)
  200. {
  201. return platform_driver_register(&syscon_driver);
  202. }
  203. postcore_initcall(syscon_init);
  204. static void __exit syscon_exit(void)
  205. {
  206. platform_driver_unregister(&syscon_driver);
  207. }
  208. module_exit(syscon_exit);
  209. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  210. MODULE_DESCRIPTION("System Control driver");
  211. MODULE_LICENSE("GPL v2");