atmel-hlcdc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2014 Free Electrons
  3. * Copyright (C) 2014 Atmel
  4. *
  5. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/mfd/atmel-hlcdc.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #define ATMEL_HLCDC_REG_MAX (0x4000 - 0x4)
  27. struct atmel_hlcdc_regmap {
  28. void __iomem *regs;
  29. };
  30. static const struct mfd_cell atmel_hlcdc_cells[] = {
  31. {
  32. .name = "atmel-hlcdc-pwm",
  33. .of_compatible = "atmel,hlcdc-pwm",
  34. },
  35. {
  36. .name = "atmel-hlcdc-dc",
  37. .of_compatible = "atmel,hlcdc-display-controller",
  38. },
  39. };
  40. static int regmap_atmel_hlcdc_reg_write(void *context, unsigned int reg,
  41. unsigned int val)
  42. {
  43. struct atmel_hlcdc_regmap *hregmap = context;
  44. if (reg <= ATMEL_HLCDC_DIS) {
  45. u32 status;
  46. readl_poll_timeout_atomic(hregmap->regs + ATMEL_HLCDC_SR,
  47. status, !(status & ATMEL_HLCDC_SIP),
  48. 1, 100);
  49. }
  50. writel(val, hregmap->regs + reg);
  51. return 0;
  52. }
  53. static int regmap_atmel_hlcdc_reg_read(void *context, unsigned int reg,
  54. unsigned int *val)
  55. {
  56. struct atmel_hlcdc_regmap *hregmap = context;
  57. *val = readl(hregmap->regs + reg);
  58. return 0;
  59. }
  60. static const struct regmap_config atmel_hlcdc_regmap_config = {
  61. .reg_bits = 32,
  62. .val_bits = 32,
  63. .reg_stride = 4,
  64. .max_register = ATMEL_HLCDC_REG_MAX,
  65. .reg_write = regmap_atmel_hlcdc_reg_write,
  66. .reg_read = regmap_atmel_hlcdc_reg_read,
  67. .fast_io = true,
  68. };
  69. static int atmel_hlcdc_probe(struct platform_device *pdev)
  70. {
  71. struct atmel_hlcdc_regmap *hregmap;
  72. struct device *dev = &pdev->dev;
  73. struct atmel_hlcdc *hlcdc;
  74. struct resource *res;
  75. hregmap = devm_kzalloc(dev, sizeof(*hregmap), GFP_KERNEL);
  76. if (!hregmap)
  77. return -ENOMEM;
  78. hlcdc = devm_kzalloc(dev, sizeof(*hlcdc), GFP_KERNEL);
  79. if (!hlcdc)
  80. return -ENOMEM;
  81. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  82. hregmap->regs = devm_ioremap_resource(dev, res);
  83. if (IS_ERR(hregmap->regs))
  84. return PTR_ERR(hregmap->regs);
  85. hlcdc->irq = platform_get_irq(pdev, 0);
  86. if (hlcdc->irq < 0)
  87. return hlcdc->irq;
  88. hlcdc->periph_clk = devm_clk_get(dev, "periph_clk");
  89. if (IS_ERR(hlcdc->periph_clk)) {
  90. dev_err(dev, "failed to get peripheral clock\n");
  91. return PTR_ERR(hlcdc->periph_clk);
  92. }
  93. hlcdc->sys_clk = devm_clk_get(dev, "sys_clk");
  94. if (IS_ERR(hlcdc->sys_clk)) {
  95. dev_err(dev, "failed to get system clock\n");
  96. return PTR_ERR(hlcdc->sys_clk);
  97. }
  98. hlcdc->slow_clk = devm_clk_get(dev, "slow_clk");
  99. if (IS_ERR(hlcdc->slow_clk)) {
  100. dev_err(dev, "failed to get slow clock\n");
  101. return PTR_ERR(hlcdc->slow_clk);
  102. }
  103. hlcdc->regmap = devm_regmap_init(dev, NULL, hregmap,
  104. &atmel_hlcdc_regmap_config);
  105. if (IS_ERR(hlcdc->regmap))
  106. return PTR_ERR(hlcdc->regmap);
  107. dev_set_drvdata(dev, hlcdc);
  108. return mfd_add_devices(dev, -1, atmel_hlcdc_cells,
  109. ARRAY_SIZE(atmel_hlcdc_cells),
  110. NULL, 0, NULL);
  111. }
  112. static int atmel_hlcdc_remove(struct platform_device *pdev)
  113. {
  114. mfd_remove_devices(&pdev->dev);
  115. return 0;
  116. }
  117. static const struct of_device_id atmel_hlcdc_match[] = {
  118. { .compatible = "atmel,at91sam9n12-hlcdc" },
  119. { .compatible = "atmel,at91sam9x5-hlcdc" },
  120. { .compatible = "atmel,sama5d2-hlcdc" },
  121. { .compatible = "atmel,sama5d3-hlcdc" },
  122. { .compatible = "atmel,sama5d4-hlcdc" },
  123. { /* sentinel */ },
  124. };
  125. MODULE_DEVICE_TABLE(of, atmel_hlcdc_match);
  126. static struct platform_driver atmel_hlcdc_driver = {
  127. .probe = atmel_hlcdc_probe,
  128. .remove = atmel_hlcdc_remove,
  129. .driver = {
  130. .name = "atmel-hlcdc",
  131. .of_match_table = atmel_hlcdc_match,
  132. },
  133. };
  134. module_platform_driver(atmel_hlcdc_driver);
  135. MODULE_ALIAS("platform:atmel-hlcdc");
  136. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  137. MODULE_DESCRIPTION("Atmel HLCDC driver");
  138. MODULE_LICENSE("GPL v2");