blob: e5b88d5d3b59425cf07e79ba20937322fa8c50fc [file] [log] [blame]
Maxime Ripard0e589d52012-05-11 15:35:33 +02001/*
2 * Driver for the ADC present in the Atmel AT91 evaluation boards.
3 *
4 * Copyright 2011 Free Electrons
5 *
6 * Licensed under the GPLv2 or later.
7 */
8
9#include <linux/bitmap.h>
10#include <linux/bitops.h>
11#include <linux/clk.h>
12#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/interrupt.h>
15#include <linux/jiffies.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
Maxime Riparde3641852012-05-11 15:35:37 +020018#include <linux/of.h>
19#include <linux/of_device.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020020#include <linux/platform_device.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/wait.h>
24
25#include <linux/platform_data/at91_adc.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020029#include <linux/iio/trigger.h>
30#include <linux/iio/trigger_consumer.h>
Lars-Peter Clausen90032e42012-06-18 18:33:49 +020031#include <linux/iio/triggered_buffer.h>
Maxime Ripard0e589d52012-05-11 15:35:33 +020032
33#include <mach/at91_adc.h>
34
35#define AT91_ADC_CHAN(st, ch) \
36 (st->registers->channel_base + (ch * 4))
37#define at91_adc_readl(st, reg) \
38 (readl_relaxed(st->reg_base + reg))
39#define at91_adc_writel(st, reg, val) \
40 (writel_relaxed(val, st->reg_base + reg))
41
42struct at91_adc_state {
43 struct clk *adc_clk;
44 u16 *buffer;
45 unsigned long channels_mask;
46 struct clk *clk;
47 bool done;
48 int irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +020049 u16 last_value;
50 struct mutex lock;
51 u8 num_channels;
52 void __iomem *reg_base;
53 struct at91_adc_reg_desc *registers;
54 u8 startup_time;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +000055 u8 sample_hold_time;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +000056 bool sleep_mode;
Maxime Ripard0e589d52012-05-11 15:35:33 +020057 struct iio_trigger **trig;
58 struct at91_adc_trigger *trigger_list;
59 u32 trigger_number;
60 bool use_external;
61 u32 vref_mv;
Ludovic Desroches47be16b2013-03-29 14:54:00 +000062 u32 res; /* resolution used for convertions */
63 bool low_res; /* the resolution corresponds to the lowest one */
Maxime Ripard0e589d52012-05-11 15:35:33 +020064 wait_queue_head_t wq_data_avail;
65};
66
67static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
68{
69 struct iio_poll_func *pf = p;
70 struct iio_dev *idev = pf->indio_dev;
71 struct at91_adc_state *st = iio_priv(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +020072 int i, j = 0;
73
74 for (i = 0; i < idev->masklength; i++) {
75 if (!test_bit(i, idev->active_scan_mask))
76 continue;
77 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
78 j++;
79 }
80
81 if (idev->scan_timestamp) {
82 s64 *timestamp = (s64 *)((u8 *)st->buffer +
83 ALIGN(j, sizeof(s64)));
84 *timestamp = pf->timestamp;
85 }
86
Jean-Christophe PLAGNIOL-VILLARD11679762012-12-12 15:17:00 +000087 iio_push_to_buffers(idev, (u8 *)st->buffer);
Maxime Ripard0e589d52012-05-11 15:35:33 +020088
89 iio_trigger_notify_done(idev->trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +020090
91 /* Needed to ACK the DRDY interruption */
92 at91_adc_readl(st, AT91_ADC_LCDR);
93
94 enable_irq(st->irq);
95
96 return IRQ_HANDLED;
97}
98
99static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
100{
101 struct iio_dev *idev = private;
102 struct at91_adc_state *st = iio_priv(idev);
103 u32 status = at91_adc_readl(st, st->registers->status_register);
104
105 if (!(status & st->registers->drdy_mask))
106 return IRQ_HANDLED;
107
108 if (iio_buffer_enabled(idev)) {
109 disable_irq_nosync(irq);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200110 iio_trigger_poll(idev->trig, iio_get_time_ns());
111 } else {
112 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
113 st->done = true;
114 wake_up_interruptible(&st->wq_data_avail);
115 }
116
117 return IRQ_HANDLED;
118}
119
120static int at91_adc_channel_init(struct iio_dev *idev)
121{
122 struct at91_adc_state *st = iio_priv(idev);
123 struct iio_chan_spec *chan_array, *timestamp;
124 int bit, idx = 0;
125
126 idev->num_channels = bitmap_weight(&st->channels_mask,
127 st->num_channels) + 1;
128
Axel Lin6b3aa312012-10-29 08:25:00 +0000129 chan_array = devm_kzalloc(&idev->dev,
130 ((idev->num_channels + 1) *
131 sizeof(struct iio_chan_spec)),
132 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200133
134 if (!chan_array)
135 return -ENOMEM;
136
137 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
138 struct iio_chan_spec *chan = chan_array + idx;
139
140 chan->type = IIO_VOLTAGE;
141 chan->indexed = 1;
142 chan->channel = bit;
143 chan->scan_index = idx;
144 chan->scan_type.sign = 'u';
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000145 chan->scan_type.realbits = st->res;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200146 chan->scan_type.storagebits = 16;
Jonathan Cameron01bdab62013-02-27 19:06:10 +0000147 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
148 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200149 idx++;
150 }
151 timestamp = chan_array + idx;
152
153 timestamp->type = IIO_TIMESTAMP;
154 timestamp->channel = -1;
155 timestamp->scan_index = idx;
156 timestamp->scan_type.sign = 's';
157 timestamp->scan_type.realbits = 64;
158 timestamp->scan_type.storagebits = 64;
159
160 idev->channels = chan_array;
161 return idev->num_channels;
162}
163
164static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
165 struct at91_adc_trigger *triggers,
166 const char *trigger_name)
167{
168 struct at91_adc_state *st = iio_priv(idev);
169 u8 value = 0;
170 int i;
171
172 for (i = 0; i < st->trigger_number; i++) {
173 char *name = kasprintf(GFP_KERNEL,
174 "%s-dev%d-%s",
175 idev->name,
176 idev->id,
177 triggers[i].name);
178 if (!name)
179 return -ENOMEM;
180
181 if (strcmp(trigger_name, name) == 0) {
182 value = triggers[i].value;
183 kfree(name);
184 break;
185 }
186
187 kfree(name);
188 }
189
190 return value;
191}
192
193static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
194{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000195 struct iio_dev *idev = iio_trigger_get_drvdata(trig);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200196 struct at91_adc_state *st = iio_priv(idev);
197 struct iio_buffer *buffer = idev->buffer;
198 struct at91_adc_reg_desc *reg = st->registers;
199 u32 status = at91_adc_readl(st, reg->trigger_register);
200 u8 value;
201 u8 bit;
202
203 value = at91_adc_get_trigger_value_by_name(idev,
204 st->trigger_list,
205 idev->trig->name);
206 if (value == 0)
207 return -EINVAL;
208
209 if (state) {
210 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
211 if (st->buffer == NULL)
212 return -ENOMEM;
213
214 at91_adc_writel(st, reg->trigger_register,
215 status | value);
216
217 for_each_set_bit(bit, buffer->scan_mask,
218 st->num_channels) {
219 struct iio_chan_spec const *chan = idev->channels + bit;
220 at91_adc_writel(st, AT91_ADC_CHER,
221 AT91_ADC_CH(chan->channel));
222 }
223
224 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
225
226 } else {
227 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
228
229 at91_adc_writel(st, reg->trigger_register,
230 status & ~value);
231
232 for_each_set_bit(bit, buffer->scan_mask,
233 st->num_channels) {
234 struct iio_chan_spec const *chan = idev->channels + bit;
235 at91_adc_writel(st, AT91_ADC_CHDR,
236 AT91_ADC_CH(chan->channel));
237 }
238 kfree(st->buffer);
239 }
240
241 return 0;
242}
243
244static const struct iio_trigger_ops at91_adc_trigger_ops = {
245 .owner = THIS_MODULE,
246 .set_trigger_state = &at91_adc_configure_trigger,
247};
248
249static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
250 struct at91_adc_trigger *trigger)
251{
252 struct iio_trigger *trig;
253 int ret;
254
255 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
256 idev->id, trigger->name);
257 if (trig == NULL)
258 return NULL;
259
260 trig->dev.parent = idev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000261 iio_trigger_set_drvdata(trig, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200262 trig->ops = &at91_adc_trigger_ops;
263
264 ret = iio_trigger_register(trig);
265 if (ret)
266 return NULL;
267
268 return trig;
269}
270
271static int at91_adc_trigger_init(struct iio_dev *idev)
272{
273 struct at91_adc_state *st = iio_priv(idev);
274 int i, ret;
275
Axel Lin6b3aa312012-10-29 08:25:00 +0000276 st->trig = devm_kzalloc(&idev->dev,
277 st->trigger_number * sizeof(st->trig),
278 GFP_KERNEL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200279
280 if (st->trig == NULL) {
281 ret = -ENOMEM;
282 goto error_ret;
283 }
284
285 for (i = 0; i < st->trigger_number; i++) {
286 if (st->trigger_list[i].is_external && !(st->use_external))
287 continue;
288
289 st->trig[i] = at91_adc_allocate_trigger(idev,
290 st->trigger_list + i);
291 if (st->trig[i] == NULL) {
292 dev_err(&idev->dev,
293 "Could not allocate trigger %d\n", i);
294 ret = -ENOMEM;
295 goto error_trigger;
296 }
297 }
298
299 return 0;
300
301error_trigger:
302 for (i--; i >= 0; i--) {
303 iio_trigger_unregister(st->trig[i]);
304 iio_trigger_free(st->trig[i]);
305 }
306error_ret:
307 return ret;
308}
309
310static void at91_adc_trigger_remove(struct iio_dev *idev)
311{
312 struct at91_adc_state *st = iio_priv(idev);
313 int i;
314
315 for (i = 0; i < st->trigger_number; i++) {
316 iio_trigger_unregister(st->trig[i]);
317 iio_trigger_free(st->trig[i]);
318 }
319}
320
Maxime Ripard0e589d52012-05-11 15:35:33 +0200321static int at91_adc_buffer_init(struct iio_dev *idev)
322{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200323 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
324 &at91_adc_trigger_handler, NULL);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200325}
326
327static void at91_adc_buffer_remove(struct iio_dev *idev)
328{
Lars-Peter Clausen90032e42012-06-18 18:33:49 +0200329 iio_triggered_buffer_cleanup(idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200330}
331
332static int at91_adc_read_raw(struct iio_dev *idev,
333 struct iio_chan_spec const *chan,
334 int *val, int *val2, long mask)
335{
336 struct at91_adc_state *st = iio_priv(idev);
337 int ret;
338
339 switch (mask) {
340 case IIO_CHAN_INFO_RAW:
341 mutex_lock(&st->lock);
342
343 at91_adc_writel(st, AT91_ADC_CHER,
344 AT91_ADC_CH(chan->channel));
345 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
346 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
347
348 ret = wait_event_interruptible_timeout(st->wq_data_avail,
349 st->done,
350 msecs_to_jiffies(1000));
351 if (ret == 0)
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200352 ret = -ETIMEDOUT;
353 if (ret < 0) {
354 mutex_unlock(&st->lock);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200355 return ret;
Lars-Peter Clausen90e6dc72012-06-26 10:43:05 +0200356 }
Maxime Ripard0e589d52012-05-11 15:35:33 +0200357
358 *val = st->last_value;
359
360 at91_adc_writel(st, AT91_ADC_CHDR,
361 AT91_ADC_CH(chan->channel));
362 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
363
364 st->last_value = 0;
365 st->done = false;
366 mutex_unlock(&st->lock);
367 return IIO_VAL_INT;
368
369 case IIO_CHAN_INFO_SCALE:
370 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
371 *val2 = 0;
372 return IIO_VAL_INT_PLUS_MICRO;
373 default:
374 break;
375 }
376 return -EINVAL;
377}
378
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000379static int at91_adc_of_get_resolution(struct at91_adc_state *st,
380 struct platform_device *pdev)
381{
382 struct iio_dev *idev = iio_priv_to_dev(st);
383 struct device_node *np = pdev->dev.of_node;
384 int count, i, ret = 0;
385 char *res_name, *s;
386 u32 *resolutions;
387
388 count = of_property_count_strings(np, "atmel,adc-res-names");
389 if (count < 2) {
390 dev_err(&idev->dev, "You must specified at least two resolution names for "
391 "adc-res-names property in the DT\n");
392 return count;
393 }
394
395 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
396 if (!resolutions)
397 return -ENOMEM;
398
399 if (of_property_read_u32_array(np, "atmel,adc-res", resolutions, count)) {
400 dev_err(&idev->dev, "Missing adc-res property in the DT.\n");
401 ret = -ENODEV;
402 goto ret;
403 }
404
405 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
406 res_name = "highres";
407
408 for (i = 0; i < count; i++) {
409 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
410 continue;
411
412 if (strcmp(res_name, s))
413 continue;
414
415 st->res = resolutions[i];
416 if (!strcmp(res_name, "lowres"))
417 st->low_res = true;
418 else
419 st->low_res = false;
420
421 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
422 goto ret;
423 }
424
425 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
426
427ret:
428 kfree(resolutions);
429 return ret;
430}
431
Maxime Riparde3641852012-05-11 15:35:37 +0200432static int at91_adc_probe_dt(struct at91_adc_state *st,
433 struct platform_device *pdev)
434{
435 struct iio_dev *idev = iio_priv_to_dev(st);
436 struct device_node *node = pdev->dev.of_node;
437 struct device_node *trig_node;
438 int i = 0, ret;
439 u32 prop;
440
441 if (!node)
442 return -EINVAL;
443
444 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
445
446 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
447 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
448 ret = -EINVAL;
449 goto error_ret;
450 }
451 st->channels_mask = prop;
452
453 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
454 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
455 ret = -EINVAL;
456 goto error_ret;
457 }
458 st->num_channels = prop;
459
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000460 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
461
Maxime Riparde3641852012-05-11 15:35:37 +0200462 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
463 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
464 ret = -EINVAL;
465 goto error_ret;
466 }
467 st->startup_time = prop;
468
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000469 prop = 0;
470 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
471 st->sample_hold_time = prop;
Maxime Riparde3641852012-05-11 15:35:37 +0200472
473 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
474 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
475 ret = -EINVAL;
476 goto error_ret;
477 }
478 st->vref_mv = prop;
479
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000480 ret = at91_adc_of_get_resolution(st, pdev);
481 if (ret)
482 goto error_ret;
483
Maxime Riparde3641852012-05-11 15:35:37 +0200484 st->registers = devm_kzalloc(&idev->dev,
485 sizeof(struct at91_adc_reg_desc),
486 GFP_KERNEL);
487 if (!st->registers) {
488 dev_err(&idev->dev, "Could not allocate register memory.\n");
489 ret = -ENOMEM;
490 goto error_ret;
491 }
492
493 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
494 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
495 ret = -EINVAL;
496 goto error_ret;
497 }
498 st->registers->channel_base = prop;
499
500 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
501 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
502 ret = -EINVAL;
503 goto error_ret;
504 }
505 st->registers->drdy_mask = prop;
506
507 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
508 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
509 ret = -EINVAL;
510 goto error_ret;
511 }
512 st->registers->status_register = prop;
513
514 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
515 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
516 ret = -EINVAL;
517 goto error_ret;
518 }
519 st->registers->trigger_register = prop;
520
521 st->trigger_number = of_get_child_count(node);
Axel Lin6b3aa312012-10-29 08:25:00 +0000522 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
523 sizeof(struct at91_adc_trigger),
524 GFP_KERNEL);
Maxime Riparde3641852012-05-11 15:35:37 +0200525 if (!st->trigger_list) {
526 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
527 ret = -ENOMEM;
528 goto error_ret;
529 }
530
531 for_each_child_of_node(node, trig_node) {
532 struct at91_adc_trigger *trig = st->trigger_list + i;
533 const char *name;
534
535 if (of_property_read_string(trig_node, "trigger-name", &name)) {
536 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
537 ret = -EINVAL;
538 goto error_ret;
539 }
540 trig->name = name;
541
542 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
543 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
544 ret = -EINVAL;
545 goto error_ret;
546 }
547 trig->value = prop;
548 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
549 i++;
550 }
551
552 return 0;
553
554error_ret:
555 return ret;
556}
557
Maxime Ripard0e589d52012-05-11 15:35:33 +0200558static int at91_adc_probe_pdata(struct at91_adc_state *st,
559 struct platform_device *pdev)
560{
561 struct at91_adc_data *pdata = pdev->dev.platform_data;
562
563 if (!pdata)
564 return -EINVAL;
565
566 st->use_external = pdata->use_external_triggers;
567 st->vref_mv = pdata->vref;
568 st->channels_mask = pdata->channels_used;
569 st->num_channels = pdata->num_channels;
570 st->startup_time = pdata->startup_time;
571 st->trigger_number = pdata->trigger_number;
572 st->trigger_list = pdata->trigger_list;
573 st->registers = pdata->registers;
574
575 return 0;
576}
577
578static const struct iio_info at91_adc_info = {
579 .driver_module = THIS_MODULE,
580 .read_raw = &at91_adc_read_raw,
581};
582
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800583static int at91_adc_probe(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200584{
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000585 unsigned int prsc, mstrclk, ticks, adc_clk, shtim;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200586 int ret;
587 struct iio_dev *idev;
588 struct at91_adc_state *st;
589 struct resource *res;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000590 u32 reg;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200591
592 idev = iio_device_alloc(sizeof(struct at91_adc_state));
593 if (idev == NULL) {
594 ret = -ENOMEM;
595 goto error_ret;
596 }
597
598 st = iio_priv(idev);
599
Maxime Riparde3641852012-05-11 15:35:37 +0200600 if (pdev->dev.of_node)
601 ret = at91_adc_probe_dt(st, pdev);
602 else
603 ret = at91_adc_probe_pdata(st, pdev);
604
Maxime Ripard0e589d52012-05-11 15:35:33 +0200605 if (ret) {
606 dev_err(&pdev->dev, "No platform data available.\n");
607 ret = -EINVAL;
608 goto error_free_device;
609 }
610
Maxime Ripard0e589d52012-05-11 15:35:33 +0200611 platform_set_drvdata(pdev, idev);
612
613 idev->dev.parent = &pdev->dev;
614 idev->name = dev_name(&pdev->dev);
615 idev->modes = INDIO_DIRECT_MODE;
616 idev->info = &at91_adc_info;
617
618 st->irq = platform_get_irq(pdev, 0);
619 if (st->irq < 0) {
620 dev_err(&pdev->dev, "No IRQ ID is designated\n");
621 ret = -ENODEV;
622 goto error_free_device;
623 }
624
Julia Lawall390d75c2012-07-31 14:09:00 +0100625 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200626
Thierry Reding5fd98462013-01-21 11:09:04 +0100627 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
628 if (IS_ERR(st->reg_base)) {
629 ret = PTR_ERR(st->reg_base);
Julia Lawall390d75c2012-07-31 14:09:00 +0100630 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200631 }
632
633 /*
634 * Disable all IRQs before setting up the handler
635 */
636 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
637 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
638 ret = request_irq(st->irq,
639 at91_adc_eoc_trigger,
640 0,
641 pdev->dev.driver->name,
642 idev);
643 if (ret) {
644 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100645 goto error_free_device;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200646 }
647
Julia Lawall390d75c2012-07-31 14:09:00 +0100648 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200649 if (IS_ERR(st->clk)) {
650 dev_err(&pdev->dev, "Failed to get the clock.\n");
651 ret = PTR_ERR(st->clk);
652 goto error_free_irq;
653 }
654
Julia Lawall00062a9c2012-08-26 17:00:00 +0100655 ret = clk_prepare_enable(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200656 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100657 dev_err(&pdev->dev,
658 "Could not prepare or enable the clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100659 goto error_free_irq;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200660 }
661
Julia Lawall390d75c2012-07-31 14:09:00 +0100662 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
Maxime Ripard0e589d52012-05-11 15:35:33 +0200663 if (IS_ERR(st->adc_clk)) {
664 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
Julia Lawallf755bbb2012-08-25 21:57:09 +0200665 ret = PTR_ERR(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200666 goto error_disable_clk;
667 }
668
Julia Lawall00062a9c2012-08-26 17:00:00 +0100669 ret = clk_prepare_enable(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200670 if (ret) {
Julia Lawall00062a9c2012-08-26 17:00:00 +0100671 dev_err(&pdev->dev,
672 "Could not prepare or enable the ADC clock.\n");
Julia Lawall390d75c2012-07-31 14:09:00 +0100673 goto error_disable_clk;
Maxime Ripard0e589d52012-05-11 15:35:33 +0200674 }
675
Maxime Ripard0e589d52012-05-11 15:35:33 +0200676 /*
677 * Prescaler rate computation using the formula from the Atmel's
678 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
679 * specified by the electrical characteristics of the board.
680 */
681 mstrclk = clk_get_rate(st->clk);
682 adc_clk = clk_get_rate(st->adc_clk);
683 prsc = (mstrclk / (2 * adc_clk)) - 1;
684
685 if (!st->startup_time) {
686 dev_err(&pdev->dev, "No startup time available.\n");
687 ret = -EINVAL;
688 goto error_disable_adc_clk;
689 }
690
691 /*
692 * Number of ticks needed to cover the startup time of the ADC as
693 * defined in the electrical characteristics of the board, divided by 8.
694 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
695 */
696 ticks = round_up((st->startup_time * adc_clk /
697 1000000) - 1, 8) / 8;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000698 /*
699 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
700 * the best converted final value between two channels selection
701 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
702 */
703 shtim = round_up((st->sample_hold_time * adc_clk /
704 1000000) - 1, 1);
705
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000706 reg = AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL;
707 reg |= AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP;
Ludovic Desroches47be16b2013-03-29 14:54:00 +0000708 if (st->low_res)
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000709 reg |= AT91_ADC_LOWRES;
710 if (st->sleep_mode)
711 reg |= AT91_ADC_SLEEP;
Jean-Christophe PLAGNIOL-VILLARDbeca9e72013-03-29 14:54:00 +0000712 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
Jean-Christophe PLAGNIOL-VILLARDe7487832013-03-29 14:54:00 +0000713 at91_adc_writel(st, AT91_ADC_MR, reg);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200714
715 /* Setup the ADC channels available on the board */
716 ret = at91_adc_channel_init(idev);
717 if (ret < 0) {
718 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
719 goto error_disable_adc_clk;
720 }
721
722 init_waitqueue_head(&st->wq_data_avail);
723 mutex_init(&st->lock);
724
725 ret = at91_adc_buffer_init(idev);
726 if (ret < 0) {
727 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
728 goto error_disable_adc_clk;
729 }
730
731 ret = at91_adc_trigger_init(idev);
732 if (ret < 0) {
733 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
734 goto error_unregister_buffer;
735 }
736
737 ret = iio_device_register(idev);
738 if (ret < 0) {
739 dev_err(&pdev->dev, "Couldn't register the device.\n");
740 goto error_remove_triggers;
741 }
742
743 return 0;
744
745error_remove_triggers:
746 at91_adc_trigger_remove(idev);
747error_unregister_buffer:
748 at91_adc_buffer_remove(idev);
749error_disable_adc_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100750 clk_disable_unprepare(st->adc_clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200751error_disable_clk:
Julia Lawall00062a9c2012-08-26 17:00:00 +0100752 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200753error_free_irq:
754 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200755error_free_device:
756 iio_device_free(idev);
757error_ret:
758 return ret;
759}
760
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800761static int at91_adc_remove(struct platform_device *pdev)
Maxime Ripard0e589d52012-05-11 15:35:33 +0200762{
763 struct iio_dev *idev = platform_get_drvdata(pdev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200764 struct at91_adc_state *st = iio_priv(idev);
765
766 iio_device_unregister(idev);
767 at91_adc_trigger_remove(idev);
768 at91_adc_buffer_remove(idev);
769 clk_disable_unprepare(st->adc_clk);
Julia Lawall00062a9c2012-08-26 17:00:00 +0100770 clk_disable_unprepare(st->clk);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200771 free_irq(st->irq, idev);
Maxime Ripard0e589d52012-05-11 15:35:33 +0200772 iio_device_free(idev);
773
774 return 0;
775}
776
Maxime Riparde3641852012-05-11 15:35:37 +0200777static const struct of_device_id at91_adc_dt_ids[] = {
778 { .compatible = "atmel,at91sam9260-adc" },
779 {},
780};
781MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
782
Maxime Ripard0e589d52012-05-11 15:35:33 +0200783static struct platform_driver at91_adc_driver = {
784 .probe = at91_adc_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800785 .remove = at91_adc_remove,
Maxime Ripard0e589d52012-05-11 15:35:33 +0200786 .driver = {
787 .name = "at91_adc",
Maxime Riparde3641852012-05-11 15:35:37 +0200788 .of_match_table = of_match_ptr(at91_adc_dt_ids),
Maxime Ripard0e589d52012-05-11 15:35:33 +0200789 },
790};
791
792module_platform_driver(at91_adc_driver);
793
794MODULE_LICENSE("GPL");
795MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
796MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");