syscon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. struct platform_device *pdev = to_platform_device(dev);
  59. const struct platform_device_id *id = platform_get_device_id(pdev);
  60. if (id)
  61. if (!strcmp(id->name, (const char *)data))
  62. return 1;
  63. return !strcmp(dev_name(dev), (const char *)data);
  64. }
  65. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  66. {
  67. struct device *dev;
  68. struct syscon *syscon;
  69. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  70. syscon_match_pdevname);
  71. if (!dev)
  72. return ERR_PTR(-EPROBE_DEFER);
  73. syscon = dev_get_drvdata(dev);
  74. return syscon->regmap;
  75. }
  76. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  77. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  78. const char *property)
  79. {
  80. struct device_node *syscon_np;
  81. struct regmap *regmap;
  82. syscon_np = of_parse_phandle(np, property, 0);
  83. if (!syscon_np)
  84. return ERR_PTR(-ENODEV);
  85. regmap = syscon_node_to_regmap(syscon_np);
  86. of_node_put(syscon_np);
  87. return regmap;
  88. }
  89. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  90. static const struct of_device_id of_syscon_match[] = {
  91. { .compatible = "syscon", },
  92. { },
  93. };
  94. static struct regmap_config syscon_regmap_config = {
  95. .reg_bits = 32,
  96. .val_bits = 32,
  97. .reg_stride = 4,
  98. };
  99. static int syscon_probe(struct platform_device *pdev)
  100. {
  101. struct device *dev = &pdev->dev;
  102. struct syscon *syscon;
  103. struct resource *res;
  104. void __iomem *base;
  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. base = devm_ioremap(dev, res->start, resource_size(res));
  112. if (!base)
  113. return -ENOMEM;
  114. syscon_regmap_config.max_register = res->end - res->start - 3;
  115. syscon->regmap = devm_regmap_init_mmio(dev, 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. #ifdef CONFIG_ARCH_CLPS711X
  128. { "clps711x-syscon", },
  129. #endif
  130. { }
  131. };
  132. static struct platform_driver syscon_driver = {
  133. .driver = {
  134. .name = "syscon",
  135. .owner = THIS_MODULE,
  136. .of_match_table = of_syscon_match,
  137. },
  138. .probe = syscon_probe,
  139. .id_table = syscon_ids,
  140. };
  141. static int __init syscon_init(void)
  142. {
  143. return platform_driver_register(&syscon_driver);
  144. }
  145. postcore_initcall(syscon_init);
  146. static void __exit syscon_exit(void)
  147. {
  148. platform_driver_unregister(&syscon_driver);
  149. }
  150. module_exit(syscon_exit);
  151. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  152. MODULE_DESCRIPTION("System Control driver");
  153. MODULE_LICENSE("GPL v2");