1 // SPDX-License-Identifier: GPL-2.0+
3 * lpc32xx_adc.c - Support for ADC in LPC32XX
5 * 3-channel, 10-bit ADC
7 * Copyright (C) 2011, 2012 Roland Stigge <stigge@antcom.de>
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/err.h>
13 #include <linux/iio/iio.h>
14 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/consumer.h>
22 * LPC32XX registers definitions
24 #define LPC32XXAD_SELECT(x) ((x) + 0x04)
25 #define LPC32XXAD_CTRL(x) ((x) + 0x08)
26 #define LPC32XXAD_VALUE(x) ((x) + 0x48)
28 /* Bit definitions for LPC32XXAD_SELECT: */
29 /* constant, always write this value! */
30 #define LPC32XXAD_REFm 0x00000200
31 /* constant, always write this value! */
32 #define LPC32XXAD_REFp 0x00000080
33 /* multiple of this is the channel number: 0, 1, 2 */
34 #define LPC32XXAD_IN 0x00000010
35 /* constant, always write this value! */
36 #define LPC32XXAD_INTERNAL 0x00000004
38 /* Bit definitions for LPC32XXAD_CTRL: */
39 #define LPC32XXAD_STROBE 0x00000002
40 #define LPC32XXAD_PDN_CTRL 0x00000004
42 /* Bit definitions for LPC32XXAD_VALUE: */
43 #define LPC32XXAD_VALUE_MASK 0x000003FF
45 #define LPC32XXAD_NAME "lpc32xx-adc"
47 struct lpc32xx_adc_state
{
48 void __iomem
*adc_base
;
50 struct completion completion
;
51 struct regulator
*vref
;
56 static int lpc32xx_read_raw(struct iio_dev
*indio_dev
,
57 struct iio_chan_spec
const *chan
,
62 struct lpc32xx_adc_state
*st
= iio_priv(indio_dev
);
66 case IIO_CHAN_INFO_RAW
:
67 mutex_lock(&indio_dev
->mlock
);
68 ret
= clk_prepare_enable(st
->clk
);
70 mutex_unlock(&indio_dev
->mlock
);
73 /* Measurement setup */
74 __raw_writel(LPC32XXAD_INTERNAL
| (chan
->address
) |
75 LPC32XXAD_REFp
| LPC32XXAD_REFm
,
76 LPC32XXAD_SELECT(st
->adc_base
));
77 /* Trigger conversion */
78 __raw_writel(LPC32XXAD_PDN_CTRL
| LPC32XXAD_STROBE
,
79 LPC32XXAD_CTRL(st
->adc_base
));
80 wait_for_completion(&st
->completion
); /* set by ISR */
81 clk_disable_unprepare(st
->clk
);
83 mutex_unlock(&indio_dev
->mlock
);
87 case IIO_CHAN_INFO_SCALE
:
88 *val
= regulator_get_voltage(st
->vref
) / 1000;
91 return IIO_VAL_FRACTIONAL_LOG2
;
97 static const struct iio_info lpc32xx_adc_iio_info
= {
98 .read_raw
= &lpc32xx_read_raw
,
101 #define LPC32XX_ADC_CHANNEL_BASE(_index) \
102 .type = IIO_VOLTAGE, \
105 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
106 .address = LPC32XXAD_IN * _index, \
107 .scan_index = _index,
109 #define LPC32XX_ADC_CHANNEL(_index) { \
110 LPC32XX_ADC_CHANNEL_BASE(_index) \
113 #define LPC32XX_ADC_SCALE_CHANNEL(_index) { \
114 LPC32XX_ADC_CHANNEL_BASE(_index) \
115 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
118 static const struct iio_chan_spec lpc32xx_adc_iio_channels
[] = {
119 LPC32XX_ADC_CHANNEL(0),
120 LPC32XX_ADC_CHANNEL(1),
121 LPC32XX_ADC_CHANNEL(2),
124 static const struct iio_chan_spec lpc32xx_adc_iio_scale_channels
[] = {
125 LPC32XX_ADC_SCALE_CHANNEL(0),
126 LPC32XX_ADC_SCALE_CHANNEL(1),
127 LPC32XX_ADC_SCALE_CHANNEL(2),
130 static irqreturn_t
lpc32xx_adc_isr(int irq
, void *dev_id
)
132 struct lpc32xx_adc_state
*st
= dev_id
;
134 /* Read value and clear irq */
135 st
->value
= __raw_readl(LPC32XXAD_VALUE(st
->adc_base
)) &
136 LPC32XXAD_VALUE_MASK
;
137 complete(&st
->completion
);
142 static int lpc32xx_adc_probe(struct platform_device
*pdev
)
144 struct lpc32xx_adc_state
*st
= NULL
;
145 struct resource
*res
;
146 int retval
= -ENODEV
;
147 struct iio_dev
*iodev
= NULL
;
150 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
152 dev_err(&pdev
->dev
, "failed to get platform I/O memory\n");
156 iodev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*st
));
160 st
= iio_priv(iodev
);
162 st
->adc_base
= devm_ioremap(&pdev
->dev
, res
->start
,
165 dev_err(&pdev
->dev
, "failed mapping memory\n");
169 st
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
170 if (IS_ERR(st
->clk
)) {
171 dev_err(&pdev
->dev
, "failed getting clock\n");
172 return PTR_ERR(st
->clk
);
175 irq
= platform_get_irq(pdev
, 0);
179 retval
= devm_request_irq(&pdev
->dev
, irq
, lpc32xx_adc_isr
, 0,
182 dev_err(&pdev
->dev
, "failed requesting interrupt\n");
186 st
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
187 if (IS_ERR(st
->vref
)) {
188 iodev
->channels
= lpc32xx_adc_iio_channels
;
190 "Missing vref regulator: No scaling available\n");
192 iodev
->channels
= lpc32xx_adc_iio_scale_channels
;
195 platform_set_drvdata(pdev
, iodev
);
197 init_completion(&st
->completion
);
199 iodev
->name
= LPC32XXAD_NAME
;
200 iodev
->info
= &lpc32xx_adc_iio_info
;
201 iodev
->modes
= INDIO_DIRECT_MODE
;
202 iodev
->num_channels
= ARRAY_SIZE(lpc32xx_adc_iio_channels
);
204 retval
= devm_iio_device_register(&pdev
->dev
, iodev
);
208 dev_info(&pdev
->dev
, "LPC32XX ADC driver loaded, IRQ %d\n", irq
);
213 static const struct of_device_id lpc32xx_adc_match
[] = {
214 { .compatible
= "nxp,lpc3220-adc" },
217 MODULE_DEVICE_TABLE(of
, lpc32xx_adc_match
);
219 static struct platform_driver lpc32xx_adc_driver
= {
220 .probe
= lpc32xx_adc_probe
,
222 .name
= LPC32XXAD_NAME
,
223 .of_match_table
= lpc32xx_adc_match
,
227 module_platform_driver(lpc32xx_adc_driver
);
229 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
230 MODULE_DESCRIPTION("LPC32XX ADC driver");
231 MODULE_LICENSE("GPL");