atmel-hlcdc.c 4.2 KB

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