2 * Driver for the ADC present in the Atmel AT91 evaluation boards.
4 * Copyright 2011 Free Electrons
6 * Licensed under the GPLv2 or later.
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/wait.h>
25 #include <linux/platform_data/at91_adc.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
33 #include <mach/at91_adc.h>
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))
42 struct at91_adc_state {
45 unsigned long channels_mask;
53 void __iomem *reg_base;
54 struct at91_adc_reg_desc *registers;
56 struct iio_trigger **trig;
57 struct at91_adc_trigger *trigger_list;
61 wait_queue_head_t wq_data_avail;
64 static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
66 struct iio_poll_func *pf = p;
67 struct iio_dev *idev = pf->indio_dev;
68 struct at91_adc_state *st = iio_priv(idev);
69 struct iio_buffer *buffer = idev->buffer;
72 for (i = 0; i < idev->masklength; i++) {
73 if (!test_bit(i, idev->active_scan_mask))
75 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
79 if (idev->scan_timestamp) {
80 s64 *timestamp = (s64 *)((u8 *)st->buffer +
81 ALIGN(j, sizeof(s64)));
82 *timestamp = pf->timestamp;
85 buffer->access->store_to(buffer, (u8 *)st->buffer, pf->timestamp);
87 iio_trigger_notify_done(idev->trig);
88 st->irq_enabled = true;
90 /* Needed to ACK the DRDY interruption */
91 at91_adc_readl(st, AT91_ADC_LCDR);
98 static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
100 struct iio_dev *idev = private;
101 struct at91_adc_state *st = iio_priv(idev);
102 u32 status = at91_adc_readl(st, st->registers->status_register);
104 if (!(status & st->registers->drdy_mask))
107 if (iio_buffer_enabled(idev)) {
108 disable_irq_nosync(irq);
109 st->irq_enabled = false;
110 iio_trigger_poll(idev->trig, iio_get_time_ns());
112 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
114 wake_up_interruptible(&st->wq_data_avail);
120 static int at91_adc_channel_init(struct iio_dev *idev)
122 struct at91_adc_state *st = iio_priv(idev);
123 struct iio_chan_spec *chan_array, *timestamp;
126 idev->num_channels = bitmap_weight(&st->channels_mask,
127 st->num_channels) + 1;
129 chan_array = devm_kzalloc(&idev->dev,
130 ((idev->num_channels + 1) *
131 sizeof(struct iio_chan_spec)),
137 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
138 struct iio_chan_spec *chan = chan_array + idx;
140 chan->type = IIO_VOLTAGE;
143 chan->scan_index = idx;
144 chan->scan_type.sign = 'u';
145 chan->scan_type.realbits = 10;
146 chan->scan_type.storagebits = 16;
147 chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT |
148 IIO_CHAN_INFO_RAW_SEPARATE_BIT;
151 timestamp = chan_array + idx;
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;
160 idev->channels = chan_array;
161 return idev->num_channels;
164 static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
165 struct at91_adc_trigger *triggers,
166 const char *trigger_name)
168 struct at91_adc_state *st = iio_priv(idev);
172 for (i = 0; i < st->trigger_number; i++) {
173 char *name = kasprintf(GFP_KERNEL,
181 if (strcmp(trigger_name, name) == 0) {
182 value = triggers[i].value;
193 static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
195 struct iio_dev *idev = trig->private_data;
196 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);
203 value = at91_adc_get_trigger_value_by_name(idev,
210 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
211 if (st->buffer == NULL)
214 at91_adc_writel(st, reg->trigger_register,
217 for_each_set_bit(bit, buffer->scan_mask,
219 struct iio_chan_spec const *chan = idev->channels + bit;
220 at91_adc_writel(st, AT91_ADC_CHER,
221 AT91_ADC_CH(chan->channel));
224 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
227 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
229 at91_adc_writel(st, reg->trigger_register,
232 for_each_set_bit(bit, buffer->scan_mask,
234 struct iio_chan_spec const *chan = idev->channels + bit;
235 at91_adc_writel(st, AT91_ADC_CHDR,
236 AT91_ADC_CH(chan->channel));
244 static const struct iio_trigger_ops at91_adc_trigger_ops = {
245 .owner = THIS_MODULE,
246 .set_trigger_state = &at91_adc_configure_trigger,
249 static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
250 struct at91_adc_trigger *trigger)
252 struct iio_trigger *trig;
255 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
256 idev->id, trigger->name);
260 trig->dev.parent = idev->dev.parent;
261 trig->private_data = idev;
262 trig->ops = &at91_adc_trigger_ops;
264 ret = iio_trigger_register(trig);
271 static int at91_adc_trigger_init(struct iio_dev *idev)
273 struct at91_adc_state *st = iio_priv(idev);
276 st->trig = devm_kzalloc(&idev->dev,
277 st->trigger_number * sizeof(st->trig),
280 if (st->trig == NULL) {
285 for (i = 0; i < st->trigger_number; i++) {
286 if (st->trigger_list[i].is_external && !(st->use_external))
289 st->trig[i] = at91_adc_allocate_trigger(idev,
290 st->trigger_list + i);
291 if (st->trig[i] == NULL) {
293 "Could not allocate trigger %d\n", i);
302 for (i--; i >= 0; i--) {
303 iio_trigger_unregister(st->trig[i]);
304 iio_trigger_free(st->trig[i]);
310 static void at91_adc_trigger_remove(struct iio_dev *idev)
312 struct at91_adc_state *st = iio_priv(idev);
315 for (i = 0; i < st->trigger_number; i++) {
316 iio_trigger_unregister(st->trig[i]);
317 iio_trigger_free(st->trig[i]);
321 static int at91_adc_buffer_init(struct iio_dev *idev)
323 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
324 &at91_adc_trigger_handler, NULL);
327 static void at91_adc_buffer_remove(struct iio_dev *idev)
329 iio_triggered_buffer_cleanup(idev);
332 static int at91_adc_read_raw(struct iio_dev *idev,
333 struct iio_chan_spec const *chan,
334 int *val, int *val2, long mask)
336 struct at91_adc_state *st = iio_priv(idev);
340 case IIO_CHAN_INFO_RAW:
341 mutex_lock(&st->lock);
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);
348 ret = wait_event_interruptible_timeout(st->wq_data_avail,
350 msecs_to_jiffies(1000));
354 mutex_unlock(&st->lock);
358 *val = st->last_value;
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);
366 mutex_unlock(&st->lock);
369 case IIO_CHAN_INFO_SCALE:
370 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
372 return IIO_VAL_INT_PLUS_MICRO;
379 static int at91_adc_probe_dt(struct at91_adc_state *st,
380 struct platform_device *pdev)
382 struct iio_dev *idev = iio_priv_to_dev(st);
383 struct device_node *node = pdev->dev.of_node;
384 struct device_node *trig_node;
391 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
393 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
394 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
398 st->channels_mask = prop;
400 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
401 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
405 st->num_channels = prop;
407 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
408 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
412 st->startup_time = prop;
415 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
416 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
422 st->registers = devm_kzalloc(&idev->dev,
423 sizeof(struct at91_adc_reg_desc),
425 if (!st->registers) {
426 dev_err(&idev->dev, "Could not allocate register memory.\n");
431 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
432 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
436 st->registers->channel_base = prop;
438 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
439 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
443 st->registers->drdy_mask = prop;
445 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
446 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
450 st->registers->status_register = prop;
452 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
453 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
457 st->registers->trigger_register = prop;
459 st->trigger_number = of_get_child_count(node);
460 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
461 sizeof(struct at91_adc_trigger),
463 if (!st->trigger_list) {
464 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
469 for_each_child_of_node(node, trig_node) {
470 struct at91_adc_trigger *trig = st->trigger_list + i;
473 if (of_property_read_string(trig_node, "trigger-name", &name)) {
474 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
480 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
481 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
486 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
496 static int at91_adc_probe_pdata(struct at91_adc_state *st,
497 struct platform_device *pdev)
499 struct at91_adc_data *pdata = pdev->dev.platform_data;
504 st->use_external = pdata->use_external_triggers;
505 st->vref_mv = pdata->vref;
506 st->channels_mask = pdata->channels_used;
507 st->num_channels = pdata->num_channels;
508 st->startup_time = pdata->startup_time;
509 st->trigger_number = pdata->trigger_number;
510 st->trigger_list = pdata->trigger_list;
511 st->registers = pdata->registers;
516 static const struct iio_info at91_adc_info = {
517 .driver_module = THIS_MODULE,
518 .read_raw = &at91_adc_read_raw,
521 static int __devinit at91_adc_probe(struct platform_device *pdev)
523 unsigned int prsc, mstrclk, ticks, adc_clk;
525 struct iio_dev *idev;
526 struct at91_adc_state *st;
527 struct resource *res;
529 idev = iio_device_alloc(sizeof(struct at91_adc_state));
537 if (pdev->dev.of_node)
538 ret = at91_adc_probe_dt(st, pdev);
540 ret = at91_adc_probe_pdata(st, pdev);
543 dev_err(&pdev->dev, "No platform data available.\n");
545 goto error_free_device;
548 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
550 dev_err(&pdev->dev, "No resource defined\n");
555 platform_set_drvdata(pdev, idev);
557 idev->dev.parent = &pdev->dev;
558 idev->name = dev_name(&pdev->dev);
559 idev->modes = INDIO_DIRECT_MODE;
560 idev->info = &at91_adc_info;
562 st->irq = platform_get_irq(pdev, 0);
564 dev_err(&pdev->dev, "No IRQ ID is designated\n");
566 goto error_free_device;
569 if (!request_mem_region(res->start, resource_size(res),
570 "AT91 adc registers")) {
571 dev_err(&pdev->dev, "Resources are unavailable.\n");
573 goto error_free_device;
576 st->reg_base = ioremap(res->start, resource_size(res));
578 dev_err(&pdev->dev, "Failed to map registers.\n");
580 goto error_release_mem;
584 * Disable all IRQs before setting up the handler
586 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
587 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
588 ret = request_irq(st->irq,
589 at91_adc_eoc_trigger,
591 pdev->dev.driver->name,
594 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
595 goto error_unmap_reg;
598 st->clk = clk_get(&pdev->dev, "adc_clk");
599 if (IS_ERR(st->clk)) {
600 dev_err(&pdev->dev, "Failed to get the clock.\n");
601 ret = PTR_ERR(st->clk);
605 ret = clk_prepare(st->clk);
607 dev_err(&pdev->dev, "Could not prepare the clock.\n");
611 ret = clk_enable(st->clk);
613 dev_err(&pdev->dev, "Could not enable the clock.\n");
614 goto error_unprepare_clk;
617 st->adc_clk = clk_get(&pdev->dev, "adc_op_clk");
618 if (IS_ERR(st->adc_clk)) {
619 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
620 ret = PTR_ERR(st->adc_clk);
621 goto error_disable_clk;
624 ret = clk_prepare(st->adc_clk);
626 dev_err(&pdev->dev, "Could not prepare the ADC clock.\n");
627 goto error_free_adc_clk;
630 ret = clk_enable(st->adc_clk);
632 dev_err(&pdev->dev, "Could not enable the ADC clock.\n");
633 goto error_unprepare_adc_clk;
637 * Prescaler rate computation using the formula from the Atmel's
638 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
639 * specified by the electrical characteristics of the board.
641 mstrclk = clk_get_rate(st->clk);
642 adc_clk = clk_get_rate(st->adc_clk);
643 prsc = (mstrclk / (2 * adc_clk)) - 1;
645 if (!st->startup_time) {
646 dev_err(&pdev->dev, "No startup time available.\n");
648 goto error_disable_adc_clk;
652 * Number of ticks needed to cover the startup time of the ADC as
653 * defined in the electrical characteristics of the board, divided by 8.
654 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
656 ticks = round_up((st->startup_time * adc_clk /
657 1000000) - 1, 8) / 8;
658 at91_adc_writel(st, AT91_ADC_MR,
659 (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
660 (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
662 /* Setup the ADC channels available on the board */
663 ret = at91_adc_channel_init(idev);
665 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
666 goto error_disable_adc_clk;
669 init_waitqueue_head(&st->wq_data_avail);
670 mutex_init(&st->lock);
672 ret = at91_adc_buffer_init(idev);
674 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
675 goto error_disable_adc_clk;
678 ret = at91_adc_trigger_init(idev);
680 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
681 goto error_unregister_buffer;
684 ret = iio_device_register(idev);
686 dev_err(&pdev->dev, "Couldn't register the device.\n");
687 goto error_remove_triggers;
692 error_remove_triggers:
693 at91_adc_trigger_remove(idev);
694 error_unregister_buffer:
695 at91_adc_buffer_remove(idev);
696 error_disable_adc_clk:
697 clk_disable(st->adc_clk);
698 error_unprepare_adc_clk:
699 clk_unprepare(st->adc_clk);
701 clk_put(st->adc_clk);
703 clk_disable(st->clk);
705 clk_unprepare(st->clk);
709 free_irq(st->irq, idev);
711 iounmap(st->reg_base);
713 release_mem_region(res->start, resource_size(res));
715 iio_device_free(idev);
720 static int __devexit at91_adc_remove(struct platform_device *pdev)
722 struct iio_dev *idev = platform_get_drvdata(pdev);
723 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
724 struct at91_adc_state *st = iio_priv(idev);
726 iio_device_unregister(idev);
727 at91_adc_trigger_remove(idev);
728 at91_adc_buffer_remove(idev);
729 clk_disable_unprepare(st->adc_clk);
730 clk_put(st->adc_clk);
731 clk_disable(st->clk);
732 clk_unprepare(st->clk);
734 free_irq(st->irq, idev);
735 iounmap(st->reg_base);
736 release_mem_region(res->start, resource_size(res));
737 iio_device_free(idev);
742 static const struct of_device_id at91_adc_dt_ids[] = {
743 { .compatible = "atmel,at91sam9260-adc" },
746 MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
748 static struct platform_driver at91_adc_driver = {
749 .probe = at91_adc_probe,
750 .remove = __devexit_p(at91_adc_remove),
753 .of_match_table = of_match_ptr(at91_adc_dt_ids),
757 module_platform_driver(at91_adc_driver);
759 MODULE_LICENSE("GPL");
760 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
761 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");