syscon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /*
  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. syscon_config.reg_stride = reg_io_width;
  76. syscon_config.val_bits = reg_io_width * 8;
  77. syscon_config.max_register = resource_size(&res) - reg_io_width;
  78. regmap = regmap_init_mmio(NULL, base, &syscon_config);
  79. if (IS_ERR(regmap)) {
  80. pr_err("regmap init failed\n");
  81. ret = PTR_ERR(regmap);
  82. goto err_regmap;
  83. }
  84. syscon->regmap = regmap;
  85. syscon->np = np;
  86. spin_lock(&syscon_list_slock);
  87. list_add_tail(&syscon->list, &syscon_list);
  88. spin_unlock(&syscon_list_slock);
  89. return syscon;
  90. err_regmap:
  91. iounmap(base);
  92. err_map:
  93. kfree(syscon);
  94. return ERR_PTR(ret);
  95. }
  96. struct regmap *syscon_node_to_regmap(struct device_node *np)
  97. {
  98. struct syscon *entry, *syscon = NULL;
  99. spin_lock(&syscon_list_slock);
  100. list_for_each_entry(entry, &syscon_list, list)
  101. if (entry->np == np) {
  102. syscon = entry;
  103. break;
  104. }
  105. spin_unlock(&syscon_list_slock);
  106. if (!syscon)
  107. syscon = of_syscon_register(np);
  108. if (IS_ERR(syscon))
  109. return ERR_CAST(syscon);
  110. return syscon->regmap;
  111. }
  112. EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
  113. struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
  114. {
  115. struct device_node *syscon_np;
  116. struct regmap *regmap;
  117. syscon_np = of_find_compatible_node(NULL, NULL, s);
  118. if (!syscon_np)
  119. return ERR_PTR(-ENODEV);
  120. regmap = syscon_node_to_regmap(syscon_np);
  121. of_node_put(syscon_np);
  122. return regmap;
  123. }
  124. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
  125. static int syscon_match_pdevname(struct device *dev, void *data)
  126. {
  127. return !strcmp(dev_name(dev), (const char *)data);
  128. }
  129. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  130. {
  131. struct device *dev;
  132. struct syscon *syscon;
  133. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  134. syscon_match_pdevname);
  135. if (!dev)
  136. return ERR_PTR(-EPROBE_DEFER);
  137. syscon = dev_get_drvdata(dev);
  138. return syscon->regmap;
  139. }
  140. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  141. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  142. const char *property)
  143. {
  144. struct device_node *syscon_np;
  145. struct regmap *regmap;
  146. if (property)
  147. syscon_np = of_parse_phandle(np, property, 0);
  148. else
  149. syscon_np = np;
  150. if (!syscon_np)
  151. return ERR_PTR(-ENODEV);
  152. regmap = syscon_node_to_regmap(syscon_np);
  153. of_node_put(syscon_np);
  154. return regmap;
  155. }
  156. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  157. static int syscon_probe(struct platform_device *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct syscon_platform_data *pdata = dev_get_platdata(dev);
  161. struct syscon *syscon;
  162. struct regmap_config syscon_config = syscon_regmap_config;
  163. struct resource *res;
  164. void __iomem *base;
  165. syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
  166. if (!syscon)
  167. return -ENOMEM;
  168. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  169. if (!res)
  170. return -ENOENT;
  171. base = devm_ioremap(dev, res->start, resource_size(res));
  172. if (!base)
  173. return -ENOMEM;
  174. syscon_config.max_register = res->end - res->start - 3;
  175. if (pdata)
  176. syscon_config.name = pdata->label;
  177. syscon->regmap = devm_regmap_init_mmio(dev, base, &syscon_config);
  178. if (IS_ERR(syscon->regmap)) {
  179. dev_err(dev, "regmap init failed\n");
  180. return PTR_ERR(syscon->regmap);
  181. }
  182. platform_set_drvdata(pdev, syscon);
  183. dev_dbg(dev, "regmap %pR registered\n", res);
  184. return 0;
  185. }
  186. static const struct platform_device_id syscon_ids[] = {
  187. { "syscon", },
  188. { }
  189. };
  190. static struct platform_driver syscon_driver = {
  191. .driver = {
  192. .name = "syscon",
  193. },
  194. .probe = syscon_probe,
  195. .id_table = syscon_ids,
  196. };
  197. static int __init syscon_init(void)
  198. {
  199. return platform_driver_register(&syscon_driver);
  200. }
  201. postcore_initcall(syscon_init);
  202. static void __exit syscon_exit(void)
  203. {
  204. platform_driver_unregister(&syscon_driver);
  205. }
  206. module_exit(syscon_exit);
  207. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  208. MODULE_DESCRIPTION("System Control driver");
  209. MODULE_LICENSE("GPL v2");