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/platform_device.h>
18 #include <linux/regulator/consumer.h>
21 * LPC32XX registers definitions
23 #define LPC32XXAD_SELECT(x) ((x) + 0x04)
24 #define LPC32XXAD_CTRL(x) ((x) + 0x08)
25 #define LPC32XXAD_VALUE(x) ((x) + 0x48)
27 /* Bit definitions for LPC32XXAD_SELECT: */
28 /* constant, always write this value! */
29 #define LPC32XXAD_REFm 0x00000200
30 /* constant, always write this value! */
31 #define LPC32XXAD_REFp 0x00000080
32 /* multiple of this is the channel number: 0, 1, 2 */
33 #define LPC32XXAD_IN 0x00000010
34 /* constant, always write this value! */
35 #define LPC32XXAD_INTERNAL 0x00000004
37 /* Bit definitions for LPC32XXAD_CTRL: */
38 #define LPC32XXAD_STROBE 0x00000002
39 #define LPC32XXAD_PDN_CTRL 0x00000004
41 /* Bit definitions for LPC32XXAD_VALUE: */
42 #define LPC32XXAD_VALUE_MASK 0x000003FF
44 #define LPC32XXAD_NAME "lpc32xx-adc"
46 struct lpc32xx_adc_state
{
47 void __iomem
*adc_base
;
49 struct completion completion
;
50 struct regulator
*vref
;
55 static int lpc32xx_read_raw(struct iio_dev
*indio_dev
,
56 struct iio_chan_spec
const *chan
,
61 struct lpc32xx_adc_state
*st
= iio_priv(indio_dev
);
65 case IIO_CHAN_INFO_RAW
:
66 mutex_lock(&indio_dev
->mlock
);
67 ret
= clk_prepare_enable(st
->clk
);
69 mutex_unlock(&indio_dev
->mlock
);
72 /* Measurement setup */
73 __raw_writel(LPC32XXAD_INTERNAL
| (chan
->address
) |
74 LPC32XXAD_REFp
| LPC32XXAD_REFm
,
75 LPC32XXAD_SELECT(st
->adc_base
));
76 /* Trigger conversion */
77 __raw_writel(LPC32XXAD_PDN_CTRL
| LPC32XXAD_STROBE
,
78 LPC32XXAD_CTRL(st
->adc_base
));
79 wait_for_completion(&st
->completion
); /* set by ISR */
80 clk_disable_unprepare(st
->clk
);
82 mutex_unlock(&indio_dev
->mlock
);
86 case IIO_CHAN_INFO_SCALE
:
87 *val
= regulator_get_voltage(st
->vref
) / 1000;
90 return IIO_VAL_FRACTIONAL_LOG2
;
96 static const struct iio_info lpc32xx_adc_iio_info
= {
97 .read_raw
= &lpc32xx_read_raw
,
100 #define LPC32XX_ADC_CHANNEL_BASE(_index) \
101 .type = IIO_VOLTAGE, \
104 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
105 .address = LPC32XXAD_IN * _index, \
106 .scan_index = _index,
108 #define LPC32XX_ADC_CHANNEL(_index) { \
109 LPC32XX_ADC_CHANNEL_BASE(_index) \
112 #define LPC32XX_ADC_SCALE_CHANNEL(_index) { \
113 LPC32XX_ADC_CHANNEL_BASE(_index) \
114 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
117 static const struct iio_chan_spec lpc32xx_adc_iio_channels
[] = {
118 LPC32XX_ADC_CHANNEL(0),
119 LPC32XX_ADC_CHANNEL(1),
120 LPC32XX_ADC_CHANNEL(2),
123 static const struct iio_chan_spec lpc32xx_adc_iio_scale_channels
[] = {
124 LPC32XX_ADC_SCALE_CHANNEL(0),
125 LPC32XX_ADC_SCALE_CHANNEL(1),
126 LPC32XX_ADC_SCALE_CHANNEL(2),
129 static irqreturn_t
lpc32xx_adc_isr(int irq
, void *dev_id
)
131 struct lpc32xx_adc_state
*st
= dev_id
;
133 /* Read value and clear irq */
134 st
->value
= __raw_readl(LPC32XXAD_VALUE(st
->adc_base
)) &
135 LPC32XXAD_VALUE_MASK
;
136 complete(&st
->completion
);
141 static int lpc32xx_adc_probe(struct platform_device
*pdev
)
143 struct lpc32xx_adc_state
*st
= NULL
;
144 struct resource
*res
;
145 int retval
= -ENODEV
;
146 struct iio_dev
*iodev
= NULL
;
149 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
151 dev_err(&pdev
->dev
, "failed to get platform I/O memory\n");
155 iodev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*st
));
159 st
= iio_priv(iodev
);
161 st
->adc_base
= devm_ioremap(&pdev
->dev
, res
->start
,
164 dev_err(&pdev
->dev
, "failed mapping memory\n");
168 st
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
169 if (IS_ERR(st
->clk
)) {
170 dev_err(&pdev
->dev
, "failed getting clock\n");
171 return PTR_ERR(st
->clk
);
174 irq
= platform_get_irq(pdev
, 0);
176 dev_err(&pdev
->dev
, "failed getting interrupt resource\n");
180 retval
= devm_request_irq(&pdev
->dev
, irq
, lpc32xx_adc_isr
, 0,
183 dev_err(&pdev
->dev
, "failed requesting interrupt\n");
187 st
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
188 if (IS_ERR(st
->vref
)) {
189 iodev
->channels
= lpc32xx_adc_iio_channels
;
191 "Missing vref regulator: No scaling available\n");
193 iodev
->channels
= lpc32xx_adc_iio_scale_channels
;
196 platform_set_drvdata(pdev
, iodev
);
198 init_completion(&st
->completion
);
200 iodev
->name
= LPC32XXAD_NAME
;
201 iodev
->dev
.parent
= &pdev
->dev
;
202 iodev
->info
= &lpc32xx_adc_iio_info
;
203 iodev
->modes
= INDIO_DIRECT_MODE
;
204 iodev
->num_channels
= ARRAY_SIZE(lpc32xx_adc_iio_channels
);
206 retval
= devm_iio_device_register(&pdev
->dev
, iodev
);
210 dev_info(&pdev
->dev
, "LPC32XX ADC driver loaded, IRQ %d\n", irq
);
216 static const struct of_device_id lpc32xx_adc_match
[] = {
217 { .compatible
= "nxp,lpc3220-adc" },
220 MODULE_DEVICE_TABLE(of
, lpc32xx_adc_match
);
223 static struct platform_driver lpc32xx_adc_driver
= {
224 .probe
= lpc32xx_adc_probe
,
226 .name
= LPC32XXAD_NAME
,
227 .of_match_table
= of_match_ptr(lpc32xx_adc_match
),
231 module_platform_driver(lpc32xx_adc_driver
);
233 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
234 MODULE_DESCRIPTION("LPC32XX ADC driver");
235 MODULE_LICENSE("GPL");