blob: 20f2d555e7cdc695ede30907abaf40e6561b170f [file] [log] [blame]
Stefan Roeseb3201b52012-04-12 11:05:35 +02001/*
2 * ST SPEAr ADC driver
3 *
4 * Copyright 2012 Stefan Roese <sr@denx.de>
5 *
6 * Licensed under the GPL-2.
7 */
8
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/interrupt.h>
12#include <linux/device.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/io.h>
16#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/completion.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21
Jonathan Cameron06458e22012-04-25 15:54:58 +010022#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
Stefan Roeseb3201b52012-04-12 11:05:35 +020024
25/*
26 * SPEAR registers definitions
27 */
28
29#define SCAN_RATE_LO(x) ((x) & 0xFFFF)
30#define SCAN_RATE_HI(x) (((x) >> 0x10) & 0xFFFF)
31#define CLK_LOW(x) (((x) & 0xf) << 0)
32#define CLK_HIGH(x) (((x) & 0xf) << 4)
33
34/* Bit definitions for SPEAR_ADC_STATUS */
35#define START_CONVERSION (1 << 0)
36#define CHANNEL_NUM(x) ((x) << 1)
37#define ADC_ENABLE (1 << 4)
38#define AVG_SAMPLE(x) ((x) << 5)
39#define VREF_INTERNAL (1 << 9)
40
41#define DATA_MASK 0x03ff
42#define DATA_BITS 10
43
44#define MOD_NAME "spear-adc"
45
46#define ADC_CHANNEL_NUM 8
47
48#define CLK_MIN 2500000
49#define CLK_MAX 20000000
50
51struct adc_regs_spear3xx {
52 u32 status;
53 u32 average;
54 u32 scan_rate;
55 u32 clk; /* Not avail for 1340 & 1310 */
56 u32 ch_ctrl[ADC_CHANNEL_NUM];
57 u32 ch_data[ADC_CHANNEL_NUM];
58};
59
60struct chan_data {
61 u32 lsb;
62 u32 msb;
63};
64
65struct adc_regs_spear6xx {
66 u32 status;
67 u32 pad[2];
68 u32 clk;
69 u32 ch_ctrl[ADC_CHANNEL_NUM];
70 struct chan_data ch_data[ADC_CHANNEL_NUM];
71 u32 scan_rate_lo;
72 u32 scan_rate_hi;
73 struct chan_data average;
74};
75
76struct spear_adc_info {
77 struct device_node *np;
78 struct adc_regs_spear3xx __iomem *adc_base_spear3xx;
79 struct adc_regs_spear6xx __iomem *adc_base_spear6xx;
80 struct clk *clk;
81 struct completion completion;
82 u32 current_clk;
83 u32 sampling_freq;
84 u32 avg_samples;
85 u32 vref_external;
86 u32 value;
87};
88
89/*
90 * Functions to access some SPEAr ADC register. Abstracted into
91 * static inline functions, because of different register offsets
92 * on different SoC variants (SPEAr300 vs SPEAr600 etc).
93 */
94static void spear_adc_set_status(struct spear_adc_info *info, u32 val)
95{
96 __raw_writel(val, &info->adc_base_spear6xx->status);
97}
98
99static void spear_adc_set_clk(struct spear_adc_info *info, u32 val)
100{
101 u32 clk_high, clk_low, count;
102 u32 apb_clk = clk_get_rate(info->clk);
103
104 count = (apb_clk + val - 1) / val;
105 clk_low = count / 2;
106 clk_high = count - clk_low;
107 info->current_clk = apb_clk / count;
108
109 __raw_writel(CLK_LOW(clk_low) | CLK_HIGH(clk_high),
110 &info->adc_base_spear6xx->clk);
111}
112
113static void spear_adc_set_ctrl(struct spear_adc_info *info, int n,
114 u32 val)
115{
116 __raw_writel(val, &info->adc_base_spear6xx->ch_ctrl[n]);
117}
118
119static u32 spear_adc_get_average(struct spear_adc_info *info)
120{
121 if (of_device_is_compatible(info->np, "st,spear600-adc")) {
122 return __raw_readl(&info->adc_base_spear6xx->average.msb) &
123 DATA_MASK;
124 } else {
125 return __raw_readl(&info->adc_base_spear3xx->average) &
126 DATA_MASK;
127 }
128}
129
130static void spear_adc_set_scanrate(struct spear_adc_info *info, u32 rate)
131{
132 if (of_device_is_compatible(info->np, "st,spear600-adc")) {
133 __raw_writel(SCAN_RATE_LO(rate),
134 &info->adc_base_spear6xx->scan_rate_lo);
135 __raw_writel(SCAN_RATE_HI(rate),
136 &info->adc_base_spear6xx->scan_rate_hi);
137 } else {
138 __raw_writel(rate, &info->adc_base_spear3xx->scan_rate);
139 }
140}
141
142static int spear_read_raw(struct iio_dev *indio_dev,
143 struct iio_chan_spec const *chan,
144 int *val,
145 int *val2,
146 long mask)
147{
148 struct spear_adc_info *info = iio_priv(indio_dev);
149 u32 scale_mv;
150 u32 status;
151
152 switch (mask) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100153 case IIO_CHAN_INFO_RAW:
Stefan Roeseb3201b52012-04-12 11:05:35 +0200154 mutex_lock(&indio_dev->mlock);
155
156 status = CHANNEL_NUM(chan->channel) |
157 AVG_SAMPLE(info->avg_samples) |
158 START_CONVERSION | ADC_ENABLE;
159 if (info->vref_external == 0)
160 status |= VREF_INTERNAL;
161
162 spear_adc_set_status(info, status);
163 wait_for_completion(&info->completion); /* set by ISR */
164 *val = info->value;
165
166 mutex_unlock(&indio_dev->mlock);
167
168 return IIO_VAL_INT;
169
170 case IIO_CHAN_INFO_SCALE:
171 scale_mv = (info->vref_external * 1000) >> DATA_BITS;
172 *val = scale_mv / 1000;
173 *val2 = (scale_mv % 1000) * 1000;
174 return IIO_VAL_INT_PLUS_MICRO;
175 }
176
177 return -EINVAL;
178}
179
180#define SPEAR_ADC_CHAN(idx) { \
181 .type = IIO_VOLTAGE, \
182 .indexed = 1, \
Jonathan Cameronf1e067b2013-03-04 21:06:04 +0000183 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
184 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Stefan Roeseb3201b52012-04-12 11:05:35 +0200185 .channel = idx, \
186 .scan_type = { \
187 .sign = 'u', \
188 .storagebits = 16, \
189 }, \
190}
191
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100192static const struct iio_chan_spec spear_adc_iio_channels[] = {
Stefan Roeseb3201b52012-04-12 11:05:35 +0200193 SPEAR_ADC_CHAN(0),
194 SPEAR_ADC_CHAN(1),
195 SPEAR_ADC_CHAN(2),
196 SPEAR_ADC_CHAN(3),
197 SPEAR_ADC_CHAN(4),
198 SPEAR_ADC_CHAN(5),
199 SPEAR_ADC_CHAN(6),
200 SPEAR_ADC_CHAN(7),
201};
202
203static irqreturn_t spear_adc_isr(int irq, void *dev_id)
204{
205 struct spear_adc_info *info = (struct spear_adc_info *)dev_id;
206
207 /* Read value to clear IRQ */
208 info->value = spear_adc_get_average(info);
209 complete(&info->completion);
210
211 return IRQ_HANDLED;
212}
213
214static int spear_adc_configure(struct spear_adc_info *info)
215{
216 int i;
217
218 /* Reset ADC core */
219 spear_adc_set_status(info, 0);
220 __raw_writel(0, &info->adc_base_spear6xx->clk);
221 for (i = 0; i < 8; i++)
222 spear_adc_set_ctrl(info, i, 0);
223 spear_adc_set_scanrate(info, 0);
224
225 spear_adc_set_clk(info, info->sampling_freq);
226
227 return 0;
228}
229
230static ssize_t spear_adc_read_frequency(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
233{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200234 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200235 struct spear_adc_info *info = iio_priv(indio_dev);
236
237 return sprintf(buf, "%d\n", info->current_clk);
238}
239
240static ssize_t spear_adc_write_frequency(struct device *dev,
241 struct device_attribute *attr,
242 const char *buf,
243 size_t len)
244{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200245 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200246 struct spear_adc_info *info = iio_priv(indio_dev);
247 u32 clk_high, clk_low, count;
248 u32 apb_clk = clk_get_rate(info->clk);
249 unsigned long lval;
250 int ret;
251
252 ret = kstrtoul(buf, 10, &lval);
253 if (ret)
254 return ret;
255
256 mutex_lock(&indio_dev->mlock);
257
258 if ((lval < CLK_MIN) || (lval > CLK_MAX)) {
259 ret = -EINVAL;
260 goto out;
261 }
262
263 count = (apb_clk + lval - 1) / lval;
264 clk_low = count / 2;
265 clk_high = count - clk_low;
266 info->current_clk = apb_clk / count;
267 spear_adc_set_clk(info, lval);
268
269out:
270 mutex_unlock(&indio_dev->mlock);
271
272 return ret ? ret : len;
273}
274
275static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
276 spear_adc_read_frequency,
277 spear_adc_write_frequency);
278
279static struct attribute *spear_attributes[] = {
280 &iio_dev_attr_sampling_frequency.dev_attr.attr,
281 NULL
282};
283
284static const struct attribute_group spear_attribute_group = {
285 .attrs = spear_attributes,
286};
287
288static const struct iio_info spear_adc_iio_info = {
289 .read_raw = &spear_read_raw,
290 .attrs = &spear_attribute_group,
291 .driver_module = THIS_MODULE,
292};
293
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500294static int spear_adc_probe(struct platform_device *pdev)
Stefan Roeseb3201b52012-04-12 11:05:35 +0200295{
296 struct device_node *np = pdev->dev.of_node;
297 struct device *dev = &pdev->dev;
298 struct spear_adc_info *info;
299 struct iio_dev *iodev = NULL;
300 int ret = -ENODEV;
301 int irq;
302
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100303 iodev = devm_iio_device_alloc(dev, sizeof(struct spear_adc_info));
Stefan Roeseb3201b52012-04-12 11:05:35 +0200304 if (!iodev) {
305 dev_err(dev, "failed allocating iio device\n");
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100306 return -ENOMEM;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200307 }
308
309 info = iio_priv(iodev);
310 info->np = np;
311
312 /*
313 * SPEAr600 has a different register layout than other SPEAr SoC's
314 * (e.g. SPEAr3xx). Let's provide two register base addresses
315 * to support multi-arch kernels.
316 */
317 info->adc_base_spear6xx = of_iomap(np, 0);
318 if (!info->adc_base_spear6xx) {
319 dev_err(dev, "failed mapping memory\n");
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100320 return -ENOMEM;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200321 }
322 info->adc_base_spear3xx =
323 (struct adc_regs_spear3xx *)info->adc_base_spear6xx;
324
325 info->clk = clk_get(dev, NULL);
326 if (IS_ERR(info->clk)) {
327 dev_err(dev, "failed getting clock\n");
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100328 goto errout1;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200329 }
330
Julia Lawallf5ed9c32012-08-26 17:00:00 +0100331 ret = clk_prepare_enable(info->clk);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200332 if (ret) {
333 dev_err(dev, "failed enabling clock\n");
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100334 goto errout2;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200335 }
336
337 irq = platform_get_irq(pdev, 0);
338 if ((irq < 0) || (irq >= NR_IRQS)) {
339 dev_err(dev, "failed getting interrupt resource\n");
340 ret = -EINVAL;
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100341 goto errout3;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200342 }
343
344 ret = devm_request_irq(dev, irq, spear_adc_isr, 0, MOD_NAME, info);
345 if (ret < 0) {
346 dev_err(dev, "failed requesting interrupt\n");
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100347 goto errout3;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200348 }
349
350 if (of_property_read_u32(np, "sampling-frequency",
351 &info->sampling_freq)) {
352 dev_err(dev, "sampling-frequency missing in DT\n");
353 ret = -EINVAL;
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100354 goto errout3;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200355 }
356
357 /*
358 * Optional avg_samples defaults to 0, resulting in single data
359 * conversion
360 */
361 of_property_read_u32(np, "average-samples", &info->avg_samples);
362
363 /*
364 * Optional vref_external defaults to 0, resulting in internal vref
365 * selection
366 */
367 of_property_read_u32(np, "vref-external", &info->vref_external);
368
369 spear_adc_configure(info);
370
371 platform_set_drvdata(pdev, iodev);
372
373 init_completion(&info->completion);
374
375 iodev->name = MOD_NAME;
376 iodev->dev.parent = dev;
377 iodev->info = &spear_adc_iio_info;
378 iodev->modes = INDIO_DIRECT_MODE;
379 iodev->channels = spear_adc_iio_channels;
380 iodev->num_channels = ARRAY_SIZE(spear_adc_iio_channels);
381
382 ret = iio_device_register(iodev);
383 if (ret)
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100384 goto errout3;
Stefan Roeseb3201b52012-04-12 11:05:35 +0200385
386 dev_info(dev, "SPEAR ADC driver loaded, IRQ %d\n", irq);
387
388 return 0;
389
Stefan Roeseb3201b52012-04-12 11:05:35 +0200390errout3:
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100391 clk_disable_unprepare(info->clk);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200392errout2:
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100393 clk_put(info->clk);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200394errout1:
Sachin Kamat8c7f6d52013-07-22 12:02:00 +0100395 iounmap(info->adc_base_spear6xx);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200396 return ret;
397}
398
Bill Pemberton447d4f22012-11-19 13:26:37 -0500399static int spear_adc_remove(struct platform_device *pdev)
Stefan Roeseb3201b52012-04-12 11:05:35 +0200400{
401 struct iio_dev *iodev = platform_get_drvdata(pdev);
402 struct spear_adc_info *info = iio_priv(iodev);
403
404 iio_device_unregister(iodev);
Julia Lawallf5ed9c32012-08-26 17:00:00 +0100405 clk_disable_unprepare(info->clk);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200406 clk_put(info->clk);
407 iounmap(info->adc_base_spear6xx);
Stefan Roeseb3201b52012-04-12 11:05:35 +0200408
409 return 0;
410}
411
Sachin Kamate89b6742013-06-07 12:06:00 +0100412#ifdef CONFIG_OF
Stefan Roeseb3201b52012-04-12 11:05:35 +0200413static const struct of_device_id spear_adc_dt_ids[] = {
414 { .compatible = "st,spear600-adc", },
415 { /* sentinel */ }
416};
417MODULE_DEVICE_TABLE(of, spear_adc_dt_ids);
Sachin Kamate89b6742013-06-07 12:06:00 +0100418#endif
Stefan Roeseb3201b52012-04-12 11:05:35 +0200419
420static struct platform_driver spear_adc_driver = {
421 .probe = spear_adc_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500422 .remove = spear_adc_remove,
Stefan Roeseb3201b52012-04-12 11:05:35 +0200423 .driver = {
424 .name = MOD_NAME,
425 .owner = THIS_MODULE,
426 .of_match_table = of_match_ptr(spear_adc_dt_ids),
427 },
428};
429
430module_platform_driver(spear_adc_driver);
431
432MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
433MODULE_DESCRIPTION("SPEAr ADC driver");
434MODULE_LICENSE("GPL");