syscon.c 4.2 KB

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