syscon.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/syscon.h>
  23. static struct platform_driver syscon_driver;
  24. struct syscon {
  25. struct regmap *regmap;
  26. };
  27. static int syscon_match_node(struct device *dev, void *data)
  28. {
  29. struct device_node *dn = data;
  30. return (dev->of_node == dn) ? 1 : 0;
  31. }
  32. struct regmap *syscon_node_to_regmap(struct device_node *np)
  33. {
  34. struct syscon *syscon;
  35. struct device *dev;
  36. dev = driver_find_device(&syscon_driver.driver, NULL, np,
  37. syscon_match_node);
  38. if (!dev)
  39. return ERR_PTR(-EPROBE_DEFER);
  40. syscon = dev_get_drvdata(dev);
  41. return syscon->regmap;
  42. }
  43. EXPORT_SYMBOL_GPL(syscon_node_to_regmap);
  44. struct regmap *syscon_regmap_lookup_by_compatible(const char *s)
  45. {
  46. struct device_node *syscon_np;
  47. struct regmap *regmap;
  48. syscon_np = of_find_compatible_node(NULL, NULL, s);
  49. if (!syscon_np)
  50. return ERR_PTR(-ENODEV);
  51. regmap = syscon_node_to_regmap(syscon_np);
  52. of_node_put(syscon_np);
  53. return regmap;
  54. }
  55. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_compatible);
  56. static int syscon_match_pdevname(struct device *dev, void *data)
  57. {
  58. return !strcmp(dev_name(dev), (const char *)data);
  59. }
  60. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  61. {
  62. struct device *dev;
  63. struct syscon *syscon;
  64. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  65. syscon_match_pdevname);
  66. if (!dev)
  67. return ERR_PTR(-EPROBE_DEFER);
  68. syscon = dev_get_drvdata(dev);
  69. return syscon->regmap;
  70. }
  71. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  72. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  73. const char *property)
  74. {
  75. struct device_node *syscon_np;
  76. struct regmap *regmap;
  77. syscon_np = of_parse_phandle(np, property, 0);
  78. if (!syscon_np)
  79. return ERR_PTR(-ENODEV);
  80. regmap = syscon_node_to_regmap(syscon_np);
  81. of_node_put(syscon_np);
  82. return regmap;
  83. }
  84. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  85. static const struct of_device_id of_syscon_match[] = {
  86. { .compatible = "syscon", },
  87. { },
  88. };
  89. static struct regmap_config syscon_regmap_config = {
  90. .reg_bits = 32,
  91. .val_bits = 32,
  92. .reg_stride = 4,
  93. };
  94. static int syscon_probe(struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. struct syscon *syscon;
  98. struct resource *res;
  99. void __iomem *base;
  100. syscon = devm_kzalloc(dev, sizeof(*syscon), GFP_KERNEL);
  101. if (!syscon)
  102. return -ENOMEM;
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. if (!res)
  105. return -ENOENT;
  106. base = devm_ioremap(dev, res->start, resource_size(res));
  107. if (!base)
  108. return -ENOMEM;
  109. syscon_regmap_config.max_register = res->end - res->start - 3;
  110. syscon->regmap = devm_regmap_init_mmio(dev, base,
  111. &syscon_regmap_config);
  112. if (IS_ERR(syscon->regmap)) {
  113. dev_err(dev, "regmap init failed\n");
  114. return PTR_ERR(syscon->regmap);
  115. }
  116. platform_set_drvdata(pdev, syscon);
  117. dev_dbg(dev, "regmap %pR registered\n", res);
  118. return 0;
  119. }
  120. static const struct platform_device_id syscon_ids[] = {
  121. { "syscon", },
  122. { }
  123. };
  124. static struct platform_driver syscon_driver = {
  125. .driver = {
  126. .name = "syscon",
  127. .owner = THIS_MODULE,
  128. .of_match_table = of_syscon_match,
  129. },
  130. .probe = syscon_probe,
  131. .id_table = syscon_ids,
  132. };
  133. static int __init syscon_init(void)
  134. {
  135. return platform_driver_register(&syscon_driver);
  136. }
  137. postcore_initcall(syscon_init);
  138. static void __exit syscon_exit(void)
  139. {
  140. platform_driver_unregister(&syscon_driver);
  141. }
  142. module_exit(syscon_exit);
  143. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  144. MODULE_DESCRIPTION("System Control driver");
  145. MODULE_LICENSE("GPL v2");