xref: /openbmc/linux/drivers/iio/adc/ep93xx_adc.c (revision 2f828fb2)
1 /*
2  * Driver for ADC module on the Cirrus Logic EP93xx series of SoCs
3  *
4  * Copyright (C) 2015 Alexander Sverdlin
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  * The driver uses polling to get the conversion status. According to EP93xx
11  * datasheets, reading ADCResult register starts the conversion, but user is also
12  * responsible for ensuring that delay between adjacent conversion triggers is
13  * long enough so that maximum allowed conversion rate is not exceeded. This
14  * basically renders IRQ mode unusable.
15  */
16 
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/iio/iio.h>
22 #include <linux/io.h>
23 #include <linux/irqflags.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/platform_device.h>
27 
28 /*
29  * This code could benefit from real HR Timers, but jiffy granularity would
30  * lower ADC conversion rate down to CONFIG_HZ, so we fallback to busy wait
31  * in such case.
32  *
33  * HR Timers-based version loads CPU only up to 10% during back to back ADC
34  * conversion, while busy wait-based version consumes whole CPU power.
35  */
36 #ifdef CONFIG_HIGH_RES_TIMERS
37 #define ep93xx_adc_delay(usmin, usmax) usleep_range(usmin, usmax)
38 #else
39 #define ep93xx_adc_delay(usmin, usmax) udelay(usmin)
40 #endif
41 
42 #define EP93XX_ADC_RESULT	0x08
43 #define   EP93XX_ADC_SDR	BIT(31)
44 #define EP93XX_ADC_SWITCH	0x18
45 #define EP93XX_ADC_SW_LOCK	0x20
46 
47 struct ep93xx_adc_priv {
48 	struct clk *clk;
49 	void __iomem *base;
50 	int lastch;
51 	struct mutex lock;
52 };
53 
54 #define EP93XX_ADC_CH(index, dname, swcfg) {			\
55 	.type = IIO_VOLTAGE,					\
56 	.indexed = 1,						\
57 	.channel = index,					\
58 	.address = swcfg,					\
59 	.datasheet_name = dname,				\
60 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
61 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) |	\
62 				   BIT(IIO_CHAN_INFO_OFFSET),	\
63 }
64 
65 /*
66  * Numbering scheme for channels 0..4 is defined in EP9301 and EP9302 datasheets.
67  * EP9307, EP9312 and EP9312 have 3 channels more (total 8), but the numbering is
68  * not defined. So the last three are numbered randomly, let's say.
69  */
70 static const struct iio_chan_spec ep93xx_adc_channels[8] = {
71 	EP93XX_ADC_CH(0, "YM",	0x608),
72 	EP93XX_ADC_CH(1, "SXP",	0x680),
73 	EP93XX_ADC_CH(2, "SXM",	0x640),
74 	EP93XX_ADC_CH(3, "SYP",	0x620),
75 	EP93XX_ADC_CH(4, "SYM",	0x610),
76 	EP93XX_ADC_CH(5, "XP",	0x601),
77 	EP93XX_ADC_CH(6, "XM",	0x602),
78 	EP93XX_ADC_CH(7, "YP",	0x604),
79 };
80 
81 static int ep93xx_read_raw(struct iio_dev *iiodev,
82 			   struct iio_chan_spec const *channel, int *value,
83 			   int *shift, long mask)
84 {
85 	struct ep93xx_adc_priv *priv = iio_priv(iiodev);
86 	unsigned long timeout;
87 	int ret;
88 
89 	switch (mask) {
90 	case IIO_CHAN_INFO_RAW:
91 		mutex_lock(&priv->lock);
92 		if (priv->lastch != channel->channel) {
93 			priv->lastch = channel->channel;
94 			/*
95 			 * Switch register is software-locked, unlocking must be
96 			 * immediately followed by write
97 			 */
98 			local_irq_disable();
99 			writel_relaxed(0xAA, priv->base + EP93XX_ADC_SW_LOCK);
100 			writel_relaxed(channel->address,
101 				       priv->base + EP93XX_ADC_SWITCH);
102 			local_irq_enable();
103 			/*
104 			 * Settling delay depends on module clock and could be
105 			 * 2ms or 500us
106 			 */
107 			ep93xx_adc_delay(2000, 2000);
108 		}
109 		/* Start the conversion, eventually discarding old result */
110 		readl_relaxed(priv->base + EP93XX_ADC_RESULT);
111 		/* Ensure maximum conversion rate is not exceeded */
112 		ep93xx_adc_delay(DIV_ROUND_UP(1000000, 925),
113 				 DIV_ROUND_UP(1000000, 925));
114 		/* At this point conversion must be completed, but anyway... */
115 		ret = IIO_VAL_INT;
116 		timeout = jiffies + msecs_to_jiffies(1) + 1;
117 		while (1) {
118 			u32 t;
119 
120 			t = readl_relaxed(priv->base + EP93XX_ADC_RESULT);
121 			if (t & EP93XX_ADC_SDR) {
122 				*value = sign_extend32(t, 15);
123 				break;
124 			}
125 
126 			if (time_after(jiffies, timeout)) {
127 				dev_err(&iiodev->dev, "Conversion timeout\n");
128 				ret = -ETIMEDOUT;
129 				break;
130 			}
131 
132 			cpu_relax();
133 		}
134 		mutex_unlock(&priv->lock);
135 		return ret;
136 
137 	case IIO_CHAN_INFO_OFFSET:
138 		/* According to datasheet, range is -25000..25000 */
139 		*value = 25000;
140 		return IIO_VAL_INT;
141 
142 	case IIO_CHAN_INFO_SCALE:
143 		/* Typical supply voltage is 3.3v */
144 		*value = (1ULL << 32) * 3300 / 50000;
145 		*shift = 32;
146 		return IIO_VAL_FRACTIONAL_LOG2;
147 	}
148 
149 	return -EINVAL;
150 }
151 
152 static const struct iio_info ep93xx_adc_info = {
153 	.read_raw = ep93xx_read_raw,
154 };
155 
156 static int ep93xx_adc_probe(struct platform_device *pdev)
157 {
158 	int ret;
159 	struct iio_dev *iiodev;
160 	struct ep93xx_adc_priv *priv;
161 	struct clk *pclk;
162 	struct resource *res;
163 
164 	iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
165 	if (!iiodev)
166 		return -ENOMEM;
167 	priv = iio_priv(iiodev);
168 
169 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 	if (!res) {
171 		dev_err(&pdev->dev, "Cannot obtain memory resource\n");
172 		return -ENXIO;
173 	}
174 	priv->base = devm_ioremap_resource(&pdev->dev, res);
175 	if (IS_ERR(priv->base)) {
176 		dev_err(&pdev->dev, "Cannot map memory resource\n");
177 		return PTR_ERR(priv->base);
178 	}
179 
180 	iiodev->dev.parent = &pdev->dev;
181 	iiodev->name = dev_name(&pdev->dev);
182 	iiodev->modes = INDIO_DIRECT_MODE;
183 	iiodev->info = &ep93xx_adc_info;
184 	iiodev->num_channels = ARRAY_SIZE(ep93xx_adc_channels);
185 	iiodev->channels = ep93xx_adc_channels;
186 
187 	priv->lastch = -1;
188 	mutex_init(&priv->lock);
189 
190 	platform_set_drvdata(pdev, iiodev);
191 
192 	priv->clk = devm_clk_get(&pdev->dev, NULL);
193 	if (IS_ERR(priv->clk)) {
194 		dev_err(&pdev->dev, "Cannot obtain clock\n");
195 		return PTR_ERR(priv->clk);
196 	}
197 
198 	pclk = clk_get_parent(priv->clk);
199 	if (!pclk) {
200 		dev_warn(&pdev->dev, "Cannot obtain parent clock\n");
201 	} else {
202 		/*
203 		 * This is actually a place for improvement:
204 		 * EP93xx ADC supports two clock divisors -- 4 and 16,
205 		 * resulting in conversion rates 3750 and 925 samples per second
206 		 * with 500us or 2ms settling time respectively.
207 		 * One might find this interesting enough to be configurable.
208 		 */
209 		ret = clk_set_rate(priv->clk, clk_get_rate(pclk) / 16);
210 		if (ret)
211 			dev_warn(&pdev->dev, "Cannot set clock rate\n");
212 		/*
213 		 * We can tolerate rate setting failure because the module should
214 		 * work in any case.
215 		 */
216 	}
217 
218 	ret = clk_enable(priv->clk);
219 	if (ret) {
220 		dev_err(&pdev->dev, "Cannot enable clock\n");
221 		return ret;
222 	}
223 
224 	ret = iio_device_register(iiodev);
225 	if (ret)
226 		clk_disable(priv->clk);
227 
228 	return ret;
229 }
230 
231 static int ep93xx_adc_remove(struct platform_device *pdev)
232 {
233 	struct iio_dev *iiodev = platform_get_drvdata(pdev);
234 	struct ep93xx_adc_priv *priv = iio_priv(iiodev);
235 
236 	iio_device_unregister(iiodev);
237 	clk_disable(priv->clk);
238 
239 	return 0;
240 }
241 
242 static struct platform_driver ep93xx_adc_driver = {
243 	.driver = {
244 		.name = "ep93xx-adc",
245 	},
246 	.probe = ep93xx_adc_probe,
247 	.remove = ep93xx_adc_remove,
248 };
249 module_platform_driver(ep93xx_adc_driver);
250 
251 MODULE_AUTHOR("Alexander Sverdlin <alexander.sverdlin@gmail.com>");
252 MODULE_DESCRIPTION("Cirrus Logic EP93XX ADC driver");
253 MODULE_LICENSE("GPL");
254 MODULE_ALIAS("platform:ep93xx-adc");
255