syscon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_data/syscon.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/syscon.h>
  24. static struct platform_driver syscon_driver;
  25. struct syscon {
  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. return !strcmp(dev_name(dev), (const char *)data);
  60. }
  61. struct regmap *syscon_regmap_lookup_by_pdevname(const char *s)
  62. {
  63. struct device *dev;
  64. struct syscon *syscon;
  65. dev = driver_find_device(&syscon_driver.driver, NULL, (void *)s,
  66. syscon_match_pdevname);
  67. if (!dev)
  68. return ERR_PTR(-EPROBE_DEFER);
  69. syscon = dev_get_drvdata(dev);
  70. return syscon->regmap;
  71. }
  72. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_pdevname);
  73. struct regmap *syscon_regmap_lookup_by_phandle(struct device_node *np,
  74. const char *property)
  75. {
  76. struct device_node *syscon_np;
  77. struct regmap *regmap;
  78. if (property)
  79. syscon_np = of_parse_phandle(np, property, 0);
  80. else
  81. syscon_np = np;
  82. if (!syscon_np)
  83. return ERR_PTR(-ENODEV);
  84. regmap = syscon_node_to_regmap(syscon_np);
  85. of_node_put(syscon_np);
  86. return regmap;
  87. }
  88. EXPORT_SYMBOL_GPL(syscon_regmap_lookup_by_phandle);
  89. static const struct of_device_id of_syscon_match[] = {
  90. { .compatible = "syscon", },
  91. { },
  92. };
  93. static struct regmap_config syscon_regmap_config = {
  94. .reg_bits = 32,
  95. .val_bits = 32,
  96. .reg_stride = 4,
  97. };
  98. static int syscon_probe(struct platform_device *pdev)
  99. {
  100. struct device *dev = &pdev->dev;
  101. struct syscon_platform_data *pdata = dev_get_platdata(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. if (pdata)
  116. syscon_regmap_config.name = pdata->label;
  117. syscon->regmap = devm_regmap_init_mmio(dev, base,
  118. &syscon_regmap_config);
  119. if (IS_ERR(syscon->regmap)) {
  120. dev_err(dev, "regmap init failed\n");
  121. return PTR_ERR(syscon->regmap);
  122. }
  123. platform_set_drvdata(pdev, syscon);
  124. dev_dbg(dev, "regmap %pR registered\n", res);
  125. return 0;
  126. }
  127. static const struct platform_device_id syscon_ids[] = {
  128. { "syscon", },
  129. { }
  130. };
  131. static struct platform_driver syscon_driver = {
  132. .driver = {
  133. .name = "syscon",
  134. .owner = THIS_MODULE,
  135. .of_match_table = of_syscon_match,
  136. },
  137. .probe = syscon_probe,
  138. .id_table = syscon_ids,
  139. };
  140. static int __init syscon_init(void)
  141. {
  142. return platform_driver_register(&syscon_driver);
  143. }
  144. postcore_initcall(syscon_init);
  145. static void __exit syscon_exit(void)
  146. {
  147. platform_driver_unregister(&syscon_driver);
  148. }
  149. module_exit(syscon_exit);
  150. MODULE_AUTHOR("Dong Aisheng <dong.aisheng@linaro.org>");
  151. MODULE_DESCRIPTION("System Control driver");
  152. MODULE_LICENSE("GPL v2");