fsl-imx25-tsadc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2014-2015 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License version 2 as published by the
  6. * Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irqchip/chained_irq.h>
  11. #include <linux/irqdesc.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/irq.h>
  14. #include <linux/mfd/imx25-tsadc.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. static struct regmap_config mx25_tsadc_regmap_config = {
  21. .fast_io = true,
  22. .max_register = 8,
  23. .reg_bits = 32,
  24. .val_bits = 32,
  25. .reg_stride = 4,
  26. };
  27. static void mx25_tsadc_irq_handler(struct irq_desc *desc)
  28. {
  29. struct mx25_tsadc *tsadc = irq_desc_get_handler_data(desc);
  30. struct irq_chip *chip = irq_desc_get_chip(desc);
  31. u32 status;
  32. chained_irq_enter(chip, desc);
  33. regmap_read(tsadc->regs, MX25_TSC_TGSR, &status);
  34. if (status & MX25_TGSR_GCQ_INT)
  35. generic_handle_irq(irq_find_mapping(tsadc->domain, 1));
  36. if (status & MX25_TGSR_TCQ_INT)
  37. generic_handle_irq(irq_find_mapping(tsadc->domain, 0));
  38. chained_irq_exit(chip, desc);
  39. }
  40. static int mx25_tsadc_domain_map(struct irq_domain *d, unsigned int irq,
  41. irq_hw_number_t hwirq)
  42. {
  43. struct mx25_tsadc *tsadc = d->host_data;
  44. irq_set_chip_data(irq, tsadc);
  45. irq_set_chip_and_handler(irq, &dummy_irq_chip,
  46. handle_level_irq);
  47. irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
  48. return 0;
  49. }
  50. static struct irq_domain_ops mx25_tsadc_domain_ops = {
  51. .map = mx25_tsadc_domain_map,
  52. .xlate = irq_domain_xlate_onecell,
  53. };
  54. static int mx25_tsadc_setup_irq(struct platform_device *pdev,
  55. struct mx25_tsadc *tsadc)
  56. {
  57. struct device *dev = &pdev->dev;
  58. struct device_node *np = dev->of_node;
  59. int irq;
  60. irq = platform_get_irq(pdev, 0);
  61. if (irq <= 0) {
  62. dev_err(dev, "Failed to get irq\n");
  63. return irq;
  64. }
  65. tsadc->domain = irq_domain_add_simple(np, 2, 0, &mx25_tsadc_domain_ops,
  66. tsadc);
  67. if (!tsadc->domain) {
  68. dev_err(dev, "Failed to add irq domain\n");
  69. return -ENOMEM;
  70. }
  71. irq_set_chained_handler(irq, mx25_tsadc_irq_handler);
  72. irq_set_handler_data(irq, tsadc);
  73. return 0;
  74. }
  75. static void mx25_tsadc_setup_clk(struct platform_device *pdev,
  76. struct mx25_tsadc *tsadc)
  77. {
  78. unsigned clk_div;
  79. /*
  80. * According to the datasheet the ADC clock should never
  81. * exceed 1,75 MHz. Base clock is the IPG and the ADC unit uses
  82. * a funny clock divider. To keep the ADC conversion time constant
  83. * adapt the ADC internal clock divider to the IPG clock rate.
  84. */
  85. dev_dbg(&pdev->dev, "Found master clock at %lu Hz\n",
  86. clk_get_rate(tsadc->clk));
  87. clk_div = DIV_ROUND_UP(clk_get_rate(tsadc->clk), 1750000);
  88. dev_dbg(&pdev->dev, "Setting up ADC clock divider to %u\n", clk_div);
  89. /* adc clock = IPG clock / (2 * div + 2) */
  90. clk_div -= 2;
  91. clk_div /= 2;
  92. /*
  93. * the ADC clock divider changes its behaviour when values below 4
  94. * are used: it is fixed to "/ 10" in this case
  95. */
  96. clk_div = max_t(unsigned, 4, clk_div);
  97. dev_dbg(&pdev->dev, "Resulting ADC conversion clock at %lu Hz\n",
  98. clk_get_rate(tsadc->clk) / (2 * clk_div + 2));
  99. regmap_update_bits(tsadc->regs, MX25_TSC_TGCR,
  100. MX25_TGCR_ADCCLKCFG(0x1f),
  101. MX25_TGCR_ADCCLKCFG(clk_div));
  102. }
  103. static int mx25_tsadc_probe(struct platform_device *pdev)
  104. {
  105. struct device *dev = &pdev->dev;
  106. struct device_node *np = dev->of_node;
  107. struct mx25_tsadc *tsadc;
  108. struct resource *res;
  109. int ret;
  110. void __iomem *iomem;
  111. tsadc = devm_kzalloc(dev, sizeof(*tsadc), GFP_KERNEL);
  112. if (!tsadc)
  113. return -ENOMEM;
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. iomem = devm_ioremap_resource(dev, res);
  116. if (IS_ERR(iomem))
  117. return PTR_ERR(iomem);
  118. tsadc->regs = devm_regmap_init_mmio(dev, iomem,
  119. &mx25_tsadc_regmap_config);
  120. if (IS_ERR(tsadc->regs)) {
  121. dev_err(dev, "Failed to initialize regmap\n");
  122. return PTR_ERR(tsadc->regs);
  123. }
  124. tsadc->clk = devm_clk_get(dev, "ipg");
  125. if (IS_ERR(tsadc->clk)) {
  126. dev_err(dev, "Failed to get ipg clock\n");
  127. return PTR_ERR(tsadc->clk);
  128. }
  129. /* setup clock according to the datasheet */
  130. mx25_tsadc_setup_clk(pdev, tsadc);
  131. /* Enable clock and reset the component */
  132. regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_CLK_EN,
  133. MX25_TGCR_CLK_EN);
  134. regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_TSC_RST,
  135. MX25_TGCR_TSC_RST);
  136. /* Setup powersaving mode, but enable internal reference voltage */
  137. regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_POWERMODE_MASK,
  138. MX25_TGCR_POWERMODE_SAVE);
  139. regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_INTREFEN,
  140. MX25_TGCR_INTREFEN);
  141. ret = mx25_tsadc_setup_irq(pdev, tsadc);
  142. if (ret)
  143. return ret;
  144. platform_set_drvdata(pdev, tsadc);
  145. of_platform_populate(np, NULL, NULL, dev);
  146. return 0;
  147. }
  148. static int mx25_tsadc_remove(struct platform_device *pdev)
  149. {
  150. struct mx25_tsadc *tsadc = platform_get_drvdata(pdev);
  151. int irq = platform_get_irq(pdev, 0);
  152. if (irq) {
  153. irq_set_chained_handler_and_data(irq, NULL, NULL);
  154. irq_domain_remove(tsadc->domain);
  155. }
  156. return 0;
  157. }
  158. static const struct of_device_id mx25_tsadc_ids[] = {
  159. { .compatible = "fsl,imx25-tsadc" },
  160. { /* Sentinel */ }
  161. };
  162. static struct platform_driver mx25_tsadc_driver = {
  163. .driver = {
  164. .name = "mx25-tsadc",
  165. .of_match_table = of_match_ptr(mx25_tsadc_ids),
  166. },
  167. .probe = mx25_tsadc_probe,
  168. .remove = mx25_tsadc_remove,
  169. };
  170. module_platform_driver(mx25_tsadc_driver);
  171. MODULE_DESCRIPTION("MFD for ADC/TSC for Freescale mx25");
  172. MODULE_AUTHOR("Markus Pargmann <mpa@pengutronix.de>");
  173. MODULE_LICENSE("GPL v2");
  174. MODULE_ALIAS("platform:mx25-tsadc");