syscon.c 3.8 KB

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