2 * IIO ADC driver for NXP LPC18xx ADC
4 * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * UNSUPPORTED hardware features:
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/driver.h>
22 #include <linux/iopoll.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
30 /* LPC18XX ADC registers and bits */
31 #define LPC18XX_ADC_CR 0x000
32 #define LPC18XX_ADC_CR_CLKDIV_SHIFT 8
33 #define LPC18XX_ADC_CR_PDN BIT(21)
34 #define LPC18XX_ADC_CR_START_NOW (0x1 << 24)
35 #define LPC18XX_ADC_GDR 0x004
37 /* Data register bits */
38 #define LPC18XX_ADC_SAMPLE_SHIFT 6
39 #define LPC18XX_ADC_SAMPLE_MASK 0x3ff
40 #define LPC18XX_ADC_CONV_DONE BIT(31)
42 /* Clock should be 4.5 MHz or less */
43 #define LPC18XX_ADC_CLK_TARGET 4500000
46 struct regulator
*vref
;
54 #define LPC18XX_ADC_CHAN(_idx) { \
55 .type = IIO_VOLTAGE, \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
59 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
62 static const struct iio_chan_spec lpc18xx_adc_iio_channels
[] = {
73 static int lpc18xx_adc_read_chan(struct lpc18xx_adc
*adc
, unsigned int ch
)
78 reg
= adc
->cr_reg
| BIT(ch
) | LPC18XX_ADC_CR_START_NOW
;
79 writel(reg
, adc
->base
+ LPC18XX_ADC_CR
);
81 ret
= readl_poll_timeout(adc
->base
+ LPC18XX_ADC_GDR
, reg
,
82 reg
& LPC18XX_ADC_CONV_DONE
, 3, 9);
84 dev_warn(adc
->dev
, "adc read timed out\n");
88 return (reg
>> LPC18XX_ADC_SAMPLE_SHIFT
) & LPC18XX_ADC_SAMPLE_MASK
;
91 static int lpc18xx_adc_read_raw(struct iio_dev
*indio_dev
,
92 struct iio_chan_spec
const *chan
,
93 int *val
, int *val2
, long mask
)
95 struct lpc18xx_adc
*adc
= iio_priv(indio_dev
);
98 case IIO_CHAN_INFO_RAW
:
99 mutex_lock(&adc
->lock
);
100 *val
= lpc18xx_adc_read_chan(adc
, chan
->channel
);
101 mutex_unlock(&adc
->lock
);
107 case IIO_CHAN_INFO_SCALE
:
108 *val
= regulator_get_voltage(adc
->vref
) / 1000;
111 return IIO_VAL_FRACTIONAL_LOG2
;
117 static const struct iio_info lpc18xx_adc_info
= {
118 .read_raw
= lpc18xx_adc_read_raw
,
121 static int lpc18xx_adc_probe(struct platform_device
*pdev
)
123 struct iio_dev
*indio_dev
;
124 struct lpc18xx_adc
*adc
;
125 struct resource
*res
;
130 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*adc
));
134 platform_set_drvdata(pdev
, indio_dev
);
135 adc
= iio_priv(indio_dev
);
136 adc
->dev
= &pdev
->dev
;
137 mutex_init(&adc
->lock
);
139 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
140 adc
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
141 if (IS_ERR(adc
->base
))
142 return PTR_ERR(adc
->base
);
144 adc
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
145 if (IS_ERR(adc
->clk
)) {
146 dev_err(&pdev
->dev
, "error getting clock\n");
147 return PTR_ERR(adc
->clk
);
150 rate
= clk_get_rate(adc
->clk
);
151 clkdiv
= DIV_ROUND_UP(rate
, LPC18XX_ADC_CLK_TARGET
);
153 adc
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
154 if (IS_ERR(adc
->vref
)) {
155 dev_err(&pdev
->dev
, "error getting regulator\n");
156 return PTR_ERR(adc
->vref
);
159 indio_dev
->name
= dev_name(&pdev
->dev
);
160 indio_dev
->dev
.parent
= &pdev
->dev
;
161 indio_dev
->info
= &lpc18xx_adc_info
;
162 indio_dev
->modes
= INDIO_DIRECT_MODE
;
163 indio_dev
->channels
= lpc18xx_adc_iio_channels
;
164 indio_dev
->num_channels
= ARRAY_SIZE(lpc18xx_adc_iio_channels
);
166 ret
= regulator_enable(adc
->vref
);
168 dev_err(&pdev
->dev
, "unable to enable regulator\n");
172 ret
= clk_prepare_enable(adc
->clk
);
174 dev_err(&pdev
->dev
, "unable to enable clock\n");
178 adc
->cr_reg
= (clkdiv
<< LPC18XX_ADC_CR_CLKDIV_SHIFT
) |
180 writel(adc
->cr_reg
, adc
->base
+ LPC18XX_ADC_CR
);
182 ret
= iio_device_register(indio_dev
);
184 dev_err(&pdev
->dev
, "unable to register device\n");
191 writel(0, adc
->base
+ LPC18XX_ADC_CR
);
192 clk_disable_unprepare(adc
->clk
);
194 regulator_disable(adc
->vref
);
198 static int lpc18xx_adc_remove(struct platform_device
*pdev
)
200 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
201 struct lpc18xx_adc
*adc
= iio_priv(indio_dev
);
203 iio_device_unregister(indio_dev
);
205 writel(0, adc
->base
+ LPC18XX_ADC_CR
);
206 clk_disable_unprepare(adc
->clk
);
207 regulator_disable(adc
->vref
);
212 static const struct of_device_id lpc18xx_adc_match
[] = {
213 { .compatible
= "nxp,lpc1850-adc" },
216 MODULE_DEVICE_TABLE(of
, lpc18xx_adc_match
);
218 static struct platform_driver lpc18xx_adc_driver
= {
219 .probe
= lpc18xx_adc_probe
,
220 .remove
= lpc18xx_adc_remove
,
222 .name
= "lpc18xx-adc",
223 .of_match_table
= lpc18xx_adc_match
,
226 module_platform_driver(lpc18xx_adc_driver
);
228 MODULE_DESCRIPTION("LPC18xx ADC driver");
229 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
230 MODULE_LICENSE("GPL v2");