1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
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
47struct 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
67
68
69
70static 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
81static 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
96
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
105
106
107 ep93xx_adc_delay(2000, 2000);
108 }
109
110 readl_relaxed(priv->base + EP93XX_ADC_RESULT);
111
112 ep93xx_adc_delay(DIV_ROUND_UP(1000000, 925),
113 DIV_ROUND_UP(1000000, 925));
114
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
139 *value = 25000;
140 return IIO_VAL_INT;
141
142 case IIO_CHAN_INFO_SCALE:
143
144 *value = (1ULL << 32) * 3300 / 50000;
145 *shift = 32;
146 return IIO_VAL_FRACTIONAL_LOG2;
147 }
148
149 return -EINVAL;
150}
151
152static const struct iio_info ep93xx_adc_info = {
153 .driver_module = THIS_MODULE,
154 .read_raw = ep93xx_read_raw,
155};
156
157static int ep93xx_adc_probe(struct platform_device *pdev)
158{
159 int ret;
160 struct iio_dev *iiodev;
161 struct ep93xx_adc_priv *priv;
162 struct clk *pclk;
163 struct resource *res;
164
165 iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
166 if (!iiodev)
167 return -ENOMEM;
168 priv = iio_priv(iiodev);
169
170 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171 if (!res) {
172 dev_err(&pdev->dev, "Cannot obtain memory resource\n");
173 return -ENXIO;
174 }
175 priv->base = devm_ioremap_resource(&pdev->dev, res);
176 if (IS_ERR(priv->base)) {
177 dev_err(&pdev->dev, "Cannot map memory resource\n");
178 return PTR_ERR(priv->base);
179 }
180
181 iiodev->dev.parent = &pdev->dev;
182 iiodev->name = dev_name(&pdev->dev);
183 iiodev->modes = INDIO_DIRECT_MODE;
184 iiodev->info = &ep93xx_adc_info;
185 iiodev->num_channels = ARRAY_SIZE(ep93xx_adc_channels);
186 iiodev->channels = ep93xx_adc_channels;
187
188 priv->lastch = -1;
189 mutex_init(&priv->lock);
190
191 platform_set_drvdata(pdev, iiodev);
192
193 priv->clk = devm_clk_get(&pdev->dev, NULL);
194 if (IS_ERR(priv->clk)) {
195 dev_err(&pdev->dev, "Cannot obtain clock\n");
196 return PTR_ERR(priv->clk);
197 }
198
199 pclk = clk_get_parent(priv->clk);
200 if (!pclk) {
201 dev_warn(&pdev->dev, "Cannot obtain parent clock\n");
202 } else {
203
204
205
206
207
208
209
210 ret = clk_set_rate(priv->clk, clk_get_rate(pclk) / 16);
211 if (ret)
212 dev_warn(&pdev->dev, "Cannot set clock rate\n");
213
214
215
216
217 }
218
219 ret = clk_enable(priv->clk);
220 if (ret) {
221 dev_err(&pdev->dev, "Cannot enable clock\n");
222 return ret;
223 }
224
225 ret = iio_device_register(iiodev);
226 if (ret)
227 clk_disable(priv->clk);
228
229 return ret;
230}
231
232static int ep93xx_adc_remove(struct platform_device *pdev)
233{
234 struct iio_dev *iiodev = platform_get_drvdata(pdev);
235 struct ep93xx_adc_priv *priv = iio_priv(iiodev);
236
237 iio_device_unregister(iiodev);
238 clk_disable(priv->clk);
239
240 return 0;
241}
242
243static struct platform_driver ep93xx_adc_driver = {
244 .driver = {
245 .name = "ep93xx-adc",
246 },
247 .probe = ep93xx_adc_probe,
248 .remove = ep93xx_adc_remove,
249};
250module_platform_driver(ep93xx_adc_driver);
251
252MODULE_AUTHOR("Alexander Sverdlin <alexander.sverdlin@gmail.com>");
253MODULE_DESCRIPTION("Cirrus Logic EP93XX ADC driver");
254MODULE_LICENSE("GPL");
255MODULE_ALIAS("platform:ep93xx-adc");
256