syscon.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 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. int ret;
  45. struct regmap_config syscon_config = syscon_regmap_config;
  46. if (!of_device_is_compatible(np, "syscon"))
  47. return ERR_PTR(-EINVAL);
  48. syscon = kzalloc(sizeof(*syscon), GFP_KERNEL);
  49. if (!syscon)
  50. return ERR_PTR(-ENOMEM);
  51. base = of_iomap(np, 0);
  52. if (!base) {
  53. ret = -ENOMEM;
  54. goto err_map;
  55. }
  56. /* Parse the device's DT node for an endianness specification */
  57. if (of_property_read_bool(np, "big-endian"))
  58. syscon_config.val_format_endian = REGMAP_ENDIAN_BIG;
  59. else if (of_property_read_bool(np, "little-endian"))
  60. syscon_config.val_format_endian = REGMAP_ENDIAN_LITTLE;
  61. regmap = regmap_init_mmio(NULL, base, &syscon_config);
  62. if (IS_ERR(regmap)) {
  63. pr_err("regmap init failed\n");
  64. ret = PTR_ERR(regmap);
  65. goto err_regmap;
  66. }
  67. syscon->regmap = regmap;
  68. syscon->np = np;
  69. spin_lock(&syscon_list_slock);
  70. list_add_tail(&syscon->list, &syscon_list);
  71. spin_unlock(&syscon_list_slock);
  72. return syscon;
  73. err_regmap:
  74. iounmap(base);
  75. err_map:
  76. kfree(syscon);
  77. return ERR_PTR(ret);
  78. }
  79. struct regmap *syscon_node_to_regmap(struct device_node *np)
  80. {
  81. struct syscon *entry, *syscon = NULL;
  82. spin_lock(&syscon_list_slock);
  83. list_for_each_entry(entry, &syscon_list, list)
  84. if (entry->np == np) {
  85. syscon = entry;
  86. break;
  87. }
  88. spin_unlock(&syscon_list_slock);
  89. if (!syscon)
  90. syscon = of_syscon_register(np);
  91. if (IS_ERR(syscon))
  92. return ERR_CAST(syscon);
  93. return syscon->regmap;
  94. }
  95. EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
  96. struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
  97. {
  98. struct device_node *syscon_np;
  99. struct regmap *regmap;
  100. syscon_np = of_find_compatible_node(NULL, NULL, s);
  101. if (!syscon_np)
  102. return ERR_PTR(-ENODEV);
  103. regmap = syscon_node_to_regmap(syscon_np);
  104. of_node_put(syscon_np);
  105. return regmap;
  106. }
  107. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
  108. static int syscon_match_pdevname(struct device *dev, void *data)
  109. {
  110. return !strcmp(dev_name(dev), (const char *)data);
  111. }
  112. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  113. {
  114. struct device *dev;
  115. struct syscon *syscon;
  116. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  117. syscon_match_pdevname);
  118. if (!dev)
  119. return ERR_PTR(-EPROBE_DEFER);
  120. syscon = dev_get_drvdata(dev);
  121. return syscon->regmap;
  122. }
  123. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  124. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  125. const char *property)
  126. {
  127. struct device_node *syscon_np;
  128. struct regmap *regmap;
  129. if (property)
  130. syscon_np = of_parse_phandle(np, property, 0);
  131. else
  132. syscon_np = np;
  133. if (!syscon_np)
  134. return ERR_PTR(-ENODEV);
  135. regmap = syscon_node_to_regmap(syscon_np);
  136. of_node_put(syscon_np);
  137. return regmap;
  138. }
  139. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  140. static int syscon_probe(struct platform_device *pdev)
  141. {
  142. struct device *dev = &pdev->dev;
  143. struct syscon_platform_data *pdata = dev_get_platdata(dev);
  144. struct syscon *syscon;
  145. struct resource *res;
  146. void __iomem *base;
  147. syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
  148. if (!syscon)
  149. return -ENOMEM;
  150. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. if (!res)
  152. return -ENOENT;
  153. base = devm_ioremap(dev, res->start, resource_size(res));
  154. if (!base)
  155. return -ENOMEM;
  156. syscon_regmap_config.max_register = res->end - res->start - 3;
  157. if (pdata)
  158. syscon_regmap_config.name = pdata->label;
  159. syscon->regmap = devm_regmap_init_mmio(dev, base,
  160. &syscon_regmap_config);
  161. if (IS_ERR(syscon->regmap)) {
  162. dev_err(dev, "regmap init failed\n");
  163. return PTR_ERR(syscon->regmap);
  164. }
  165. platform_set_drvdata(pdev, syscon);
  166. dev_dbg(dev, "regmap %pR registered\n", res);
  167. return 0;
  168. }
  169. static const struct platform_device_id syscon_ids[] = {
  170. { "syscon", },
  171. { }
  172. };
  173. static struct platform_driver syscon_driver = {
  174. .driver = {
  175. .name = "syscon",
  176. },
  177. .probe = syscon_probe,
  178. .id_table = syscon_ids,
  179. };
  180. static int __init syscon_init(void)
  181. {
  182. return platform_driver_register(&syscon_driver);
  183. }
  184. postcore_initcall(syscon_init);
  185. static void __exit syscon_exit(void)
  186. {
  187. platform_driver_unregister(&syscon_driver);
  188. }
  189. module_exit(syscon_exit);
  190. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  191. MODULE_DESCRIPTION("System Control driver");
  192. MODULE_LICENSE("GPL v2");