xref: /openbmc/linux/drivers/iio/adc/lpc18xx_adc.c (revision d2574c33)
1 /*
2  * IIO ADC driver for NXP LPC18xx ADC
3  *
4  * Copyright (C) 2016 Joachim Eastwood <manabian@gmail.com>
5  *
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.
9  *
10  * UNSUPPORTED hardware features:
11  *  - Hardware triggers
12  *  - Burst mode
13  *  - Interrupts
14  *  - DMA
15  */
16 
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/driver.h>
21 #include <linux/io.h>
22 #include <linux/iopoll.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regulator/consumer.h>
29 
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
36 
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)
41 
42 /* Clock should be 4.5 MHz or less */
43 #define LPC18XX_ADC_CLK_TARGET		4500000
44 
45 struct lpc18xx_adc {
46 	struct regulator *vref;
47 	void __iomem *base;
48 	struct device *dev;
49 	struct mutex lock;
50 	struct clk *clk;
51 	u32 cr_reg;
52 };
53 
54 #define LPC18XX_ADC_CHAN(_idx) {				\
55 	.type = IIO_VOLTAGE,					\
56 	.indexed = 1,						\
57 	.channel = _idx,					\
58 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
59 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
60 }
61 
62 static const struct iio_chan_spec lpc18xx_adc_iio_channels[] = {
63 	LPC18XX_ADC_CHAN(0),
64 	LPC18XX_ADC_CHAN(1),
65 	LPC18XX_ADC_CHAN(2),
66 	LPC18XX_ADC_CHAN(3),
67 	LPC18XX_ADC_CHAN(4),
68 	LPC18XX_ADC_CHAN(5),
69 	LPC18XX_ADC_CHAN(6),
70 	LPC18XX_ADC_CHAN(7),
71 };
72 
73 static int lpc18xx_adc_read_chan(struct lpc18xx_adc *adc, unsigned int ch)
74 {
75 	int ret;
76 	u32 reg;
77 
78 	reg = adc->cr_reg | BIT(ch) | LPC18XX_ADC_CR_START_NOW;
79 	writel(reg, adc->base + LPC18XX_ADC_CR);
80 
81 	ret = readl_poll_timeout(adc->base + LPC18XX_ADC_GDR, reg,
82 				 reg & LPC18XX_ADC_CONV_DONE, 3, 9);
83 	if (ret) {
84 		dev_warn(adc->dev, "adc read timed out\n");
85 		return ret;
86 	}
87 
88 	return (reg >> LPC18XX_ADC_SAMPLE_SHIFT) & LPC18XX_ADC_SAMPLE_MASK;
89 }
90 
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)
94 {
95 	struct lpc18xx_adc *adc = iio_priv(indio_dev);
96 
97 	switch (mask) {
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);
102 		if (*val < 0)
103 			return *val;
104 
105 		return IIO_VAL_INT;
106 
107 	case IIO_CHAN_INFO_SCALE:
108 		*val = regulator_get_voltage(adc->vref) / 1000;
109 		*val2 = 10;
110 
111 		return IIO_VAL_FRACTIONAL_LOG2;
112 	}
113 
114 	return -EINVAL;
115 }
116 
117 static const struct iio_info lpc18xx_adc_info = {
118 	.read_raw = lpc18xx_adc_read_raw,
119 };
120 
121 static int lpc18xx_adc_probe(struct platform_device *pdev)
122 {
123 	struct iio_dev *indio_dev;
124 	struct lpc18xx_adc *adc;
125 	struct resource *res;
126 	unsigned int clkdiv;
127 	unsigned long rate;
128 	int ret;
129 
130 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
131 	if (!indio_dev)
132 		return -ENOMEM;
133 
134 	platform_set_drvdata(pdev, indio_dev);
135 	adc = iio_priv(indio_dev);
136 	adc->dev = &pdev->dev;
137 	mutex_init(&adc->lock);
138 
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);
143 
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);
148 	}
149 
150 	rate = clk_get_rate(adc->clk);
151 	clkdiv = DIV_ROUND_UP(rate, LPC18XX_ADC_CLK_TARGET);
152 
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);
157 	}
158 
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);
165 
166 	ret = regulator_enable(adc->vref);
167 	if (ret) {
168 		dev_err(&pdev->dev, "unable to enable regulator\n");
169 		return ret;
170 	}
171 
172 	ret = clk_prepare_enable(adc->clk);
173 	if (ret) {
174 		dev_err(&pdev->dev, "unable to enable clock\n");
175 		goto dis_reg;
176 	}
177 
178 	adc->cr_reg = (clkdiv << LPC18XX_ADC_CR_CLKDIV_SHIFT) |
179 			LPC18XX_ADC_CR_PDN;
180 	writel(adc->cr_reg, adc->base + LPC18XX_ADC_CR);
181 
182 	ret = iio_device_register(indio_dev);
183 	if (ret) {
184 		dev_err(&pdev->dev, "unable to register device\n");
185 		goto dis_clk;
186 	}
187 
188 	return 0;
189 
190 dis_clk:
191 	writel(0, adc->base + LPC18XX_ADC_CR);
192 	clk_disable_unprepare(adc->clk);
193 dis_reg:
194 	regulator_disable(adc->vref);
195 	return ret;
196 }
197 
198 static int lpc18xx_adc_remove(struct platform_device *pdev)
199 {
200 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
201 	struct lpc18xx_adc *adc = iio_priv(indio_dev);
202 
203 	iio_device_unregister(indio_dev);
204 
205 	writel(0, adc->base + LPC18XX_ADC_CR);
206 	clk_disable_unprepare(adc->clk);
207 	regulator_disable(adc->vref);
208 
209 	return 0;
210 }
211 
212 static const struct of_device_id lpc18xx_adc_match[] = {
213 	{ .compatible = "nxp,lpc1850-adc" },
214 	{ /* sentinel */ }
215 };
216 MODULE_DEVICE_TABLE(of, lpc18xx_adc_match);
217 
218 static struct platform_driver lpc18xx_adc_driver = {
219 	.probe	= lpc18xx_adc_probe,
220 	.remove	= lpc18xx_adc_remove,
221 	.driver	= {
222 		.name = "lpc18xx-adc",
223 		.of_match_table = lpc18xx_adc_match,
224 	},
225 };
226 module_platform_driver(lpc18xx_adc_driver);
227 
228 MODULE_DESCRIPTION("LPC18xx ADC driver");
229 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
230 MODULE_LICENSE("GPL v2");
231