1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2015 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
5 * This is the driver for the imx25 GCQ (Generic Conversion Queue)
6 * connected to the imx25 ADC.
9 #include <dt-bindings/iio/adc/fsl-imx25-gcq.h>
10 #include <linux/clk.h>
11 #include <linux/iio/iio.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/imx25-tsadc.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
20 #define MX25_GCQ_TIMEOUT (msecs_to_jiffies(2000))
22 static const char * const driver_name
= "mx25-gcq";
36 struct mx25_gcq_priv
{
38 struct completion completed
;
41 struct regulator
*vref
[4];
42 u32 channel_vref_mv
[MX25_NUM_CFGS
];
44 * Lock to protect the device state during a potential concurrent
45 * read access from userspace. Reading a raw value requires a sequence
46 * of register writes, then a wait for a completion callback,
47 * and finally a register read, during which userspace could issue
48 * another read request. This lock protects a read access from
49 * ocurring before another one has finished.
54 #define MX25_CQG_CHAN(chan, id) {\
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
59 BIT(IIO_CHAN_INFO_SCALE),\
60 .datasheet_name = id,\
63 static const struct iio_chan_spec mx25_gcq_channels
[MX25_NUM_CFGS
] = {
64 MX25_CQG_CHAN(MX25_CFG_XP
, "xp"),
65 MX25_CQG_CHAN(MX25_CFG_YP
, "yp"),
66 MX25_CQG_CHAN(MX25_CFG_XN
, "xn"),
67 MX25_CQG_CHAN(MX25_CFG_YN
, "yn"),
68 MX25_CQG_CHAN(MX25_CFG_WIPER
, "wiper"),
69 MX25_CQG_CHAN(MX25_CFG_INAUX0
, "inaux0"),
70 MX25_CQG_CHAN(MX25_CFG_INAUX1
, "inaux1"),
71 MX25_CQG_CHAN(MX25_CFG_INAUX2
, "inaux2"),
74 static const char * const mx25_gcq_refp_names
[] = {
75 [MX25_ADC_REFP_YP
] = "yp",
76 [MX25_ADC_REFP_XP
] = "xp",
77 [MX25_ADC_REFP_INT
] = "int",
78 [MX25_ADC_REFP_EXT
] = "ext",
81 static irqreturn_t
mx25_gcq_irq(int irq
, void *data
)
83 struct mx25_gcq_priv
*priv
= data
;
86 regmap_read(priv
->regs
, MX25_ADCQ_SR
, &stats
);
88 if (stats
& MX25_ADCQ_SR_EOQ
) {
89 regmap_update_bits(priv
->regs
, MX25_ADCQ_MR
,
90 MX25_ADCQ_MR_EOQ_IRQ
, MX25_ADCQ_MR_EOQ_IRQ
);
91 complete(&priv
->completed
);
94 /* Disable conversion queue run */
95 regmap_update_bits(priv
->regs
, MX25_ADCQ_CR
, MX25_ADCQ_CR_FQS
, 0);
97 /* Acknowledge all possible irqs */
98 regmap_write(priv
->regs
, MX25_ADCQ_SR
, MX25_ADCQ_SR_FRR
|
99 MX25_ADCQ_SR_FUR
| MX25_ADCQ_SR_FOR
|
100 MX25_ADCQ_SR_EOQ
| MX25_ADCQ_SR_PD
);
105 static int mx25_gcq_get_raw_value(struct device
*dev
,
106 struct iio_chan_spec
const *chan
,
107 struct mx25_gcq_priv
*priv
,
113 /* Setup the configuration we want to use */
114 regmap_write(priv
->regs
, MX25_ADCQ_ITEM_7_0
,
115 MX25_ADCQ_ITEM(0, chan
->channel
));
117 regmap_update_bits(priv
->regs
, MX25_ADCQ_MR
, MX25_ADCQ_MR_EOQ_IRQ
, 0);
119 /* Trigger queue for one run */
120 regmap_update_bits(priv
->regs
, MX25_ADCQ_CR
, MX25_ADCQ_CR_FQS
,
123 timeout
= wait_for_completion_interruptible_timeout(
124 &priv
->completed
, MX25_GCQ_TIMEOUT
);
126 dev_err(dev
, "ADC wait for measurement failed\n");
128 } else if (timeout
== 0) {
129 dev_err(dev
, "ADC timed out\n");
133 regmap_read(priv
->regs
, MX25_ADCQ_FIFO
, &data
);
135 *val
= MX25_ADCQ_FIFO_DATA(data
);
140 static int mx25_gcq_read_raw(struct iio_dev
*indio_dev
,
141 struct iio_chan_spec
const *chan
, int *val
,
142 int *val2
, long mask
)
144 struct mx25_gcq_priv
*priv
= iio_priv(indio_dev
);
148 case IIO_CHAN_INFO_RAW
:
149 mutex_lock(&priv
->lock
);
150 ret
= mx25_gcq_get_raw_value(&indio_dev
->dev
, chan
, priv
, val
);
151 mutex_unlock(&priv
->lock
);
154 case IIO_CHAN_INFO_SCALE
:
155 *val
= priv
->channel_vref_mv
[chan
->channel
];
157 return IIO_VAL_FRACTIONAL_LOG2
;
164 static const struct iio_info mx25_gcq_iio_info
= {
165 .read_raw
= mx25_gcq_read_raw
,
168 static const struct regmap_config mx25_gcq_regconfig
= {
169 .max_register
= 0x5c,
175 static int mx25_gcq_setup_cfgs(struct platform_device
*pdev
,
176 struct mx25_gcq_priv
*priv
)
178 struct device_node
*np
= pdev
->dev
.of_node
;
179 struct device_node
*child
;
180 struct device
*dev
= &pdev
->dev
;
181 unsigned int refp_used
[4] = {};
185 * Setup all configurations registers with a default conversion
186 * configuration for each input
188 for (i
= 0; i
< MX25_NUM_CFGS
; ++i
)
189 regmap_write(priv
->regs
, MX25_ADCQ_CFG(i
),
190 MX25_ADCQ_CFG_YPLL_OFF
|
191 MX25_ADCQ_CFG_XNUR_OFF
|
192 MX25_ADCQ_CFG_XPUL_OFF
|
193 MX25_ADCQ_CFG_REFP_INT
|
194 MX25_ADCQ_CFG_IN(i
) |
195 MX25_ADCQ_CFG_REFN_NGND2
);
198 * First get all regulators to store them in channel_vref_mv if
199 * necessary. Later we use that information for proper IIO scale
202 priv
->vref
[MX25_ADC_REFP_INT
] = NULL
;
203 priv
->vref
[MX25_ADC_REFP_EXT
] =
204 devm_regulator_get_optional(&pdev
->dev
, "vref-ext");
205 priv
->vref
[MX25_ADC_REFP_XP
] =
206 devm_regulator_get_optional(&pdev
->dev
, "vref-xp");
207 priv
->vref
[MX25_ADC_REFP_YP
] =
208 devm_regulator_get_optional(&pdev
->dev
, "vref-yp");
210 for_each_child_of_node(np
, child
) {
212 u32 refp
= MX25_ADCQ_CFG_REFP_INT
;
213 u32 refn
= MX25_ADCQ_CFG_REFN_NGND2
;
215 ret
= of_property_read_u32(child
, "reg", ®
);
217 dev_err(dev
, "Failed to get reg property\n");
222 if (reg
>= MX25_NUM_CFGS
) {
224 "reg value is greater than the number of available configuration registers\n");
229 of_property_read_u32(child
, "fsl,adc-refp", &refp
);
230 of_property_read_u32(child
, "fsl,adc-refn", &refn
);
233 case MX25_ADC_REFP_EXT
:
234 case MX25_ADC_REFP_XP
:
235 case MX25_ADC_REFP_YP
:
236 if (IS_ERR(priv
->vref
[refp
])) {
237 dev_err(dev
, "Error, trying to use external voltage reference without a vref-%s regulator.",
238 mx25_gcq_refp_names
[refp
]);
240 return PTR_ERR(priv
->vref
[refp
]);
242 priv
->channel_vref_mv
[reg
] =
243 regulator_get_voltage(priv
->vref
[refp
]);
244 /* Conversion from uV to mV */
245 priv
->channel_vref_mv
[reg
] /= 1000;
247 case MX25_ADC_REFP_INT
:
248 priv
->channel_vref_mv
[reg
] = 2500;
251 dev_err(dev
, "Invalid positive reference %d\n", refp
);
259 * Shift the read values to the correct positions within the
262 refp
= MX25_ADCQ_CFG_REFP(refp
);
263 refn
= MX25_ADCQ_CFG_REFN(refn
);
265 if ((refp
& MX25_ADCQ_CFG_REFP_MASK
) != refp
) {
266 dev_err(dev
, "Invalid fsl,adc-refp property value\n");
270 if ((refn
& MX25_ADCQ_CFG_REFN_MASK
) != refn
) {
271 dev_err(dev
, "Invalid fsl,adc-refn property value\n");
276 regmap_update_bits(priv
->regs
, MX25_ADCQ_CFG(reg
),
277 MX25_ADCQ_CFG_REFP_MASK
|
278 MX25_ADCQ_CFG_REFN_MASK
,
281 regmap_update_bits(priv
->regs
, MX25_ADCQ_CR
,
282 MX25_ADCQ_CR_FRST
| MX25_ADCQ_CR_QRST
,
283 MX25_ADCQ_CR_FRST
| MX25_ADCQ_CR_QRST
);
285 regmap_write(priv
->regs
, MX25_ADCQ_CR
,
286 MX25_ADCQ_CR_PDMSK
| MX25_ADCQ_CR_QSM_FQS
);
288 /* Remove unused regulators */
289 for (i
= 0; i
!= 4; ++i
) {
291 if (!IS_ERR_OR_NULL(priv
->vref
[i
]))
292 devm_regulator_put(priv
->vref
[i
]);
293 priv
->vref
[i
] = NULL
;
300 static int mx25_gcq_probe(struct platform_device
*pdev
)
302 struct iio_dev
*indio_dev
;
303 struct mx25_gcq_priv
*priv
;
304 struct mx25_tsadc
*tsadc
= dev_get_drvdata(pdev
->dev
.parent
);
305 struct device
*dev
= &pdev
->dev
;
310 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*priv
));
314 priv
= iio_priv(indio_dev
);
316 mem
= devm_platform_ioremap_resource(pdev
, 0);
320 priv
->regs
= devm_regmap_init_mmio(dev
, mem
, &mx25_gcq_regconfig
);
321 if (IS_ERR(priv
->regs
)) {
322 dev_err(dev
, "Failed to initialize regmap\n");
323 return PTR_ERR(priv
->regs
);
326 mutex_init(&priv
->lock
);
328 init_completion(&priv
->completed
);
330 ret
= mx25_gcq_setup_cfgs(pdev
, priv
);
334 for (i
= 0; i
!= 4; ++i
) {
338 ret
= regulator_enable(priv
->vref
[i
]);
340 goto err_regulator_disable
;
343 priv
->clk
= tsadc
->clk
;
344 ret
= clk_prepare_enable(priv
->clk
);
346 dev_err(dev
, "Failed to enable clock\n");
347 goto err_vref_disable
;
350 priv
->irq
= platform_get_irq(pdev
, 0);
351 if (priv
->irq
<= 0) {
355 goto err_clk_unprepare
;
358 ret
= request_irq(priv
->irq
, mx25_gcq_irq
, 0, pdev
->name
, priv
);
360 dev_err(dev
, "Failed requesting IRQ\n");
361 goto err_clk_unprepare
;
364 indio_dev
->channels
= mx25_gcq_channels
;
365 indio_dev
->num_channels
= ARRAY_SIZE(mx25_gcq_channels
);
366 indio_dev
->info
= &mx25_gcq_iio_info
;
367 indio_dev
->name
= driver_name
;
369 ret
= iio_device_register(indio_dev
);
371 dev_err(dev
, "Failed to register iio device\n");
375 platform_set_drvdata(pdev
, indio_dev
);
380 free_irq(priv
->irq
, priv
);
382 clk_disable_unprepare(priv
->clk
);
385 err_regulator_disable
:
388 regulator_disable(priv
->vref
[i
]);
393 static int mx25_gcq_remove(struct platform_device
*pdev
)
395 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
396 struct mx25_gcq_priv
*priv
= iio_priv(indio_dev
);
399 iio_device_unregister(indio_dev
);
400 free_irq(priv
->irq
, priv
);
401 clk_disable_unprepare(priv
->clk
);
402 for (i
= 4; i
-- > 0;) {
404 regulator_disable(priv
->vref
[i
]);
410 static const struct of_device_id mx25_gcq_ids
[] = {
411 { .compatible
= "fsl,imx25-gcq", },
414 MODULE_DEVICE_TABLE(of
, mx25_gcq_ids
);
416 static struct platform_driver mx25_gcq_driver
= {
419 .of_match_table
= mx25_gcq_ids
,
421 .probe
= mx25_gcq_probe
,
422 .remove
= mx25_gcq_remove
,
424 module_platform_driver(mx25_gcq_driver
);
426 MODULE_DESCRIPTION("ADC driver for Freescale mx25");
427 MODULE_AUTHOR("Markus Pargmann <mpa@pengutronix.de>");
428 MODULE_LICENSE("GPL v2");