1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2015 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
7 #include <linux/interrupt.h>
8 #include <linux/irqchip/chained_irq.h>
9 #include <linux/irqdesc.h>
10 #include <linux/irqdomain.h>
11 #include <linux/irq.h>
12 #include <linux/mfd/imx25-tsadc.h>
13 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 static struct regmap_config mx25_tsadc_regmap_config = {
27 static void mx25_tsadc_irq_handler(struct irq_desc *desc)
29 struct mx25_tsadc *tsadc = irq_desc_get_handler_data(desc);
30 struct irq_chip *chip = irq_desc_get_chip(desc);
33 chained_irq_enter(chip, desc);
35 regmap_read(tsadc->regs, MX25_TSC_TGSR, &status);
37 if (status & MX25_TGSR_GCQ_INT)
38 generic_handle_irq(irq_find_mapping(tsadc->domain, 1));
40 if (status & MX25_TGSR_TCQ_INT)
41 generic_handle_irq(irq_find_mapping(tsadc->domain, 0));
43 chained_irq_exit(chip, desc);
46 static int mx25_tsadc_domain_map(struct irq_domain *d, unsigned int irq,
47 irq_hw_number_t hwirq)
49 struct mx25_tsadc *tsadc = d->host_data;
51 irq_set_chip_data(irq, tsadc);
52 irq_set_chip_and_handler(irq, &dummy_irq_chip,
54 irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
59 static const struct irq_domain_ops mx25_tsadc_domain_ops = {
60 .map = mx25_tsadc_domain_map,
61 .xlate = irq_domain_xlate_onecell,
64 static int mx25_tsadc_setup_irq(struct platform_device *pdev,
65 struct mx25_tsadc *tsadc)
67 struct device *dev = &pdev->dev;
68 struct device_node *np = dev->of_node;
71 irq = platform_get_irq(pdev, 0);
75 tsadc->domain = irq_domain_add_simple(np, 2, 0, &mx25_tsadc_domain_ops,
78 dev_err(dev, "Failed to add irq domain\n");
82 irq_set_chained_handler_and_data(irq, mx25_tsadc_irq_handler, tsadc);
87 static int mx25_tsadc_unset_irq(struct platform_device *pdev)
89 struct mx25_tsadc *tsadc = platform_get_drvdata(pdev);
90 int irq = platform_get_irq(pdev, 0);
93 irq_set_chained_handler_and_data(irq, NULL, NULL);
94 irq_domain_remove(tsadc->domain);
100 static void mx25_tsadc_setup_clk(struct platform_device *pdev,
101 struct mx25_tsadc *tsadc)
106 * According to the datasheet the ADC clock should never
107 * exceed 1,75 MHz. Base clock is the IPG and the ADC unit uses
108 * a funny clock divider. To keep the ADC conversion time constant
109 * adapt the ADC internal clock divider to the IPG clock rate.
112 dev_dbg(&pdev->dev, "Found master clock at %lu Hz\n",
113 clk_get_rate(tsadc->clk));
115 clk_div = DIV_ROUND_UP(clk_get_rate(tsadc->clk), 1750000);
116 dev_dbg(&pdev->dev, "Setting up ADC clock divider to %u\n", clk_div);
118 /* adc clock = IPG clock / (2 * div + 2) */
123 * the ADC clock divider changes its behaviour when values below 4
124 * are used: it is fixed to "/ 10" in this case
126 clk_div = max_t(unsigned, 4, clk_div);
128 dev_dbg(&pdev->dev, "Resulting ADC conversion clock at %lu Hz\n",
129 clk_get_rate(tsadc->clk) / (2 * clk_div + 2));
131 regmap_update_bits(tsadc->regs, MX25_TSC_TGCR,
132 MX25_TGCR_ADCCLKCFG(0x1f),
133 MX25_TGCR_ADCCLKCFG(clk_div));
136 static int mx25_tsadc_probe(struct platform_device *pdev)
138 struct device *dev = &pdev->dev;
139 struct mx25_tsadc *tsadc;
140 struct resource *res;
144 tsadc = devm_kzalloc(dev, sizeof(*tsadc), GFP_KERNEL);
148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 iomem = devm_ioremap_resource(dev, res);
151 return PTR_ERR(iomem);
153 tsadc->regs = devm_regmap_init_mmio(dev, iomem,
154 &mx25_tsadc_regmap_config);
155 if (IS_ERR(tsadc->regs)) {
156 dev_err(dev, "Failed to initialize regmap\n");
157 return PTR_ERR(tsadc->regs);
160 tsadc->clk = devm_clk_get(dev, "ipg");
161 if (IS_ERR(tsadc->clk)) {
162 dev_err(dev, "Failed to get ipg clock\n");
163 return PTR_ERR(tsadc->clk);
166 /* setup clock according to the datasheet */
167 mx25_tsadc_setup_clk(pdev, tsadc);
169 /* Enable clock and reset the component */
170 regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_CLK_EN,
172 regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_TSC_RST,
175 /* Setup powersaving mode, but enable internal reference voltage */
176 regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_POWERMODE_MASK,
177 MX25_TGCR_POWERMODE_SAVE);
178 regmap_update_bits(tsadc->regs, MX25_TSC_TGCR, MX25_TGCR_INTREFEN,
181 ret = mx25_tsadc_setup_irq(pdev, tsadc);
185 platform_set_drvdata(pdev, tsadc);
187 ret = devm_of_platform_populate(dev);
194 mx25_tsadc_unset_irq(pdev);
199 static int mx25_tsadc_remove(struct platform_device *pdev)
201 mx25_tsadc_unset_irq(pdev);
206 static const struct of_device_id mx25_tsadc_ids[] = {
207 { .compatible = "fsl,imx25-tsadc" },
210 MODULE_DEVICE_TABLE(of, mx25_tsadc_ids);
212 static struct platform_driver mx25_tsadc_driver = {
214 .name = "mx25-tsadc",
215 .of_match_table = of_match_ptr(mx25_tsadc_ids),
217 .probe = mx25_tsadc_probe,
218 .remove = mx25_tsadc_remove,
220 module_platform_driver(mx25_tsadc_driver);
222 MODULE_DESCRIPTION("MFD for ADC/TSC for Freescale mx25");
223 MODULE_AUTHOR("Markus Pargmann <mpa@pengutronix.de>");
224 MODULE_LICENSE("GPL v2");
225 MODULE_ALIAS("platform:mx25-tsadc");