atmel-hlcdc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(hregmap->regs + ATMEL_HLCDC_SR, status,
  47. !(status & ATMEL_HLCDC_SIP), 1, 100);
  48. }
  49. writel(val, hregmap->regs + reg);
  50. return 0;
  51. }
  52. static int regmap_atmel_hlcdc_reg_read(void *context, unsigned int reg,
  53. unsigned int *val)
  54. {
  55. struct atmel_hlcdc_regmap *hregmap = context;
  56. *val = readl(hregmap->regs + reg);
  57. return 0;
  58. }
  59. static const struct regmap_config atmel_hlcdc_regmap_config = {
  60. .reg_bits = 32,
  61. .val_bits = 32,
  62. .reg_stride = 4,
  63. .max_register = ATMEL_HLCDC_REG_MAX,
  64. .reg_write = regmap_atmel_hlcdc_reg_write,
  65. .reg_read = regmap_atmel_hlcdc_reg_read,
  66. .fast_io = true,
  67. };
  68. static int atmel_hlcdc_probe(struct platform_device *pdev)
  69. {
  70. struct atmel_hlcdc_regmap *hregmap;
  71. struct device *dev = &pdev->dev;
  72. struct atmel_hlcdc *hlcdc;
  73. struct resource *res;
  74. hregmap = devm_kzalloc(dev, sizeof(*hregmap), GFP_KERNEL);
  75. if (!hregmap)
  76. return -ENOMEM;
  77. hlcdc = devm_kzalloc(dev, sizeof(*hlcdc), GFP_KERNEL);
  78. if (!hlcdc)
  79. return -ENOMEM;
  80. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. hregmap->regs = devm_ioremap_resource(dev, res);
  82. if (IS_ERR(hregmap->regs))
  83. return PTR_ERR(hregmap->regs);
  84. hlcdc->irq = platform_get_irq(pdev, 0);
  85. if (hlcdc->irq < 0)
  86. return hlcdc->irq;
  87. hlcdc->periph_clk = devm_clk_get(dev, "periph_clk");
  88. if (IS_ERR(hlcdc->periph_clk)) {
  89. dev_err(dev, "failed to get peripheral clock\n");
  90. return PTR_ERR(hlcdc->periph_clk);
  91. }
  92. hlcdc->sys_clk = devm_clk_get(dev, "sys_clk");
  93. if (IS_ERR(hlcdc->sys_clk)) {
  94. dev_err(dev, "failed to get system clock\n");
  95. return PTR_ERR(hlcdc->sys_clk);
  96. }
  97. hlcdc->slow_clk = devm_clk_get(dev, "slow_clk");
  98. if (IS_ERR(hlcdc->slow_clk)) {
  99. dev_err(dev, "failed to get slow clock\n");
  100. return PTR_ERR(hlcdc->slow_clk);
  101. }
  102. hlcdc->regmap = devm_regmap_init(dev, NULL, hregmap,
  103. &atmel_hlcdc_regmap_config);
  104. if (IS_ERR(hlcdc->regmap))
  105. return PTR_ERR(hlcdc->regmap);
  106. dev_set_drvdata(dev, hlcdc);
  107. return mfd_add_devices(dev, -1, atmel_hlcdc_cells,
  108. ARRAY_SIZE(atmel_hlcdc_cells),
  109. NULL, 0, NULL);
  110. }
  111. static int atmel_hlcdc_remove(struct platform_device *pdev)
  112. {
  113. mfd_remove_devices(&pdev->dev);
  114. return 0;
  115. }
  116. static const struct of_device_id atmel_hlcdc_match[] = {
  117. { .compatible = "atmel,at91sam9n12-hlcdc" },
  118. { .compatible = "atmel,at91sam9x5-hlcdc" },
  119. { .compatible = "atmel,sama5d2-hlcdc" },
  120. { .compatible = "atmel,sama5d3-hlcdc" },
  121. { .compatible = "atmel,sama5d4-hlcdc" },
  122. { /* sentinel */ },
  123. };
  124. MODULE_DEVICE_TABLE(of, atmel_hlcdc_match);
  125. static struct platform_driver atmel_hlcdc_driver = {
  126. .probe = atmel_hlcdc_probe,
  127. .remove = atmel_hlcdc_remove,
  128. .driver = {
  129. .name = "atmel-hlcdc",
  130. .of_match_table = atmel_hlcdc_match,
  131. },
  132. };
  133. module_platform_driver(atmel_hlcdc_driver);
  134. MODULE_ALIAS("platform:atmel-hlcdc");
  135. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  136. MODULE_DESCRIPTION("Atmel HLCDC driver");
  137. MODULE_LICENSE("GPL v2");