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;
52 void __iomem *reg_base;
53 struct at91_adc_reg_desc *registers;
55 struct iio_trigger **trig;
56 struct at91_adc_trigger *trigger_list;
60 wait_queue_head_t wq_data_avail;
63 static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
65 struct iio_poll_func *pf = p;
66 struct iio_dev *idev = pf->indio_dev;
67 struct at91_adc_state *st = iio_priv(idev);
70 for (i = 0; i < idev->masklength; i++) {
71 if (!test_bit(i, idev->active_scan_mask))
73 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
77 if (idev->scan_timestamp) {
78 s64 *timestamp = (s64 *)((u8 *)st->buffer +
79 ALIGN(j, sizeof(s64)));
80 *timestamp = pf->timestamp;
83 iio_push_to_buffers(indio_dev, (u8 *)st->buffer);
85 iio_trigger_notify_done(idev->trig);
87 /* Needed to ACK the DRDY interruption */
88 at91_adc_readl(st, AT91_ADC_LCDR);
95 static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
97 struct iio_dev *idev = private;
98 struct at91_adc_state *st = iio_priv(idev);
99 u32 status = at91_adc_readl(st, st->registers->status_register);
101 if (!(status & st->registers->drdy_mask))
104 if (iio_buffer_enabled(idev)) {
105 disable_irq_nosync(irq);
106 iio_trigger_poll(idev->trig, iio_get_time_ns());
108 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
110 wake_up_interruptible(&st->wq_data_avail);
116 static int at91_adc_channel_init(struct iio_dev *idev)
118 struct at91_adc_state *st = iio_priv(idev);
119 struct iio_chan_spec *chan_array, *timestamp;
122 idev->num_channels = bitmap_weight(&st->channels_mask,
123 st->num_channels) + 1;
125 chan_array = devm_kzalloc(&idev->dev,
126 ((idev->num_channels + 1) *
127 sizeof(struct iio_chan_spec)),
133 for_each_set_bit(bit, &st->channels_mask, st->num_channels) {
134 struct iio_chan_spec *chan = chan_array + idx;
136 chan->type = IIO_VOLTAGE;
139 chan->scan_index = idx;
140 chan->scan_type.sign = 'u';
141 chan->scan_type.realbits = 10;
142 chan->scan_type.storagebits = 16;
143 chan->info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT |
144 IIO_CHAN_INFO_RAW_SEPARATE_BIT;
147 timestamp = chan_array + idx;
149 timestamp->type = IIO_TIMESTAMP;
150 timestamp->channel = -1;
151 timestamp->scan_index = idx;
152 timestamp->scan_type.sign = 's';
153 timestamp->scan_type.realbits = 64;
154 timestamp->scan_type.storagebits = 64;
156 idev->channels = chan_array;
157 return idev->num_channels;
160 static u8 at91_adc_get_trigger_value_by_name(struct iio_dev *idev,
161 struct at91_adc_trigger *triggers,
162 const char *trigger_name)
164 struct at91_adc_state *st = iio_priv(idev);
168 for (i = 0; i < st->trigger_number; i++) {
169 char *name = kasprintf(GFP_KERNEL,
177 if (strcmp(trigger_name, name) == 0) {
178 value = triggers[i].value;
189 static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
191 struct iio_dev *idev = trig->private_data;
192 struct at91_adc_state *st = iio_priv(idev);
193 struct iio_buffer *buffer = idev->buffer;
194 struct at91_adc_reg_desc *reg = st->registers;
195 u32 status = at91_adc_readl(st, reg->trigger_register);
199 value = at91_adc_get_trigger_value_by_name(idev,
206 st->buffer = kmalloc(idev->scan_bytes, GFP_KERNEL);
207 if (st->buffer == NULL)
210 at91_adc_writel(st, reg->trigger_register,
213 for_each_set_bit(bit, buffer->scan_mask,
215 struct iio_chan_spec const *chan = idev->channels + bit;
216 at91_adc_writel(st, AT91_ADC_CHER,
217 AT91_ADC_CH(chan->channel));
220 at91_adc_writel(st, AT91_ADC_IER, reg->drdy_mask);
223 at91_adc_writel(st, AT91_ADC_IDR, reg->drdy_mask);
225 at91_adc_writel(st, reg->trigger_register,
228 for_each_set_bit(bit, buffer->scan_mask,
230 struct iio_chan_spec const *chan = idev->channels + bit;
231 at91_adc_writel(st, AT91_ADC_CHDR,
232 AT91_ADC_CH(chan->channel));
240 static const struct iio_trigger_ops at91_adc_trigger_ops = {
241 .owner = THIS_MODULE,
242 .set_trigger_state = &at91_adc_configure_trigger,
245 static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
246 struct at91_adc_trigger *trigger)
248 struct iio_trigger *trig;
251 trig = iio_trigger_alloc("%s-dev%d-%s", idev->name,
252 idev->id, trigger->name);
256 trig->dev.parent = idev->dev.parent;
257 trig->private_data = idev;
258 trig->ops = &at91_adc_trigger_ops;
260 ret = iio_trigger_register(trig);
267 static int at91_adc_trigger_init(struct iio_dev *idev)
269 struct at91_adc_state *st = iio_priv(idev);
272 st->trig = devm_kzalloc(&idev->dev,
273 st->trigger_number * sizeof(st->trig),
276 if (st->trig == NULL) {
281 for (i = 0; i < st->trigger_number; i++) {
282 if (st->trigger_list[i].is_external && !(st->use_external))
285 st->trig[i] = at91_adc_allocate_trigger(idev,
286 st->trigger_list + i);
287 if (st->trig[i] == NULL) {
289 "Could not allocate trigger %d\n", i);
298 for (i--; i >= 0; i--) {
299 iio_trigger_unregister(st->trig[i]);
300 iio_trigger_free(st->trig[i]);
306 static void at91_adc_trigger_remove(struct iio_dev *idev)
308 struct at91_adc_state *st = iio_priv(idev);
311 for (i = 0; i < st->trigger_number; i++) {
312 iio_trigger_unregister(st->trig[i]);
313 iio_trigger_free(st->trig[i]);
317 static int at91_adc_buffer_init(struct iio_dev *idev)
319 return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
320 &at91_adc_trigger_handler, NULL);
323 static void at91_adc_buffer_remove(struct iio_dev *idev)
325 iio_triggered_buffer_cleanup(idev);
328 static int at91_adc_read_raw(struct iio_dev *idev,
329 struct iio_chan_spec const *chan,
330 int *val, int *val2, long mask)
332 struct at91_adc_state *st = iio_priv(idev);
336 case IIO_CHAN_INFO_RAW:
337 mutex_lock(&st->lock);
339 at91_adc_writel(st, AT91_ADC_CHER,
340 AT91_ADC_CH(chan->channel));
341 at91_adc_writel(st, AT91_ADC_IER, st->registers->drdy_mask);
342 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_START);
344 ret = wait_event_interruptible_timeout(st->wq_data_avail,
346 msecs_to_jiffies(1000));
350 mutex_unlock(&st->lock);
354 *val = st->last_value;
356 at91_adc_writel(st, AT91_ADC_CHDR,
357 AT91_ADC_CH(chan->channel));
358 at91_adc_writel(st, AT91_ADC_IDR, st->registers->drdy_mask);
362 mutex_unlock(&st->lock);
365 case IIO_CHAN_INFO_SCALE:
366 *val = (st->vref_mv * 1000) >> chan->scan_type.realbits;
368 return IIO_VAL_INT_PLUS_MICRO;
375 static int at91_adc_probe_dt(struct at91_adc_state *st,
376 struct platform_device *pdev)
378 struct iio_dev *idev = iio_priv_to_dev(st);
379 struct device_node *node = pdev->dev.of_node;
380 struct device_node *trig_node;
387 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
389 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
390 dev_err(&idev->dev, "Missing adc-channels-used property in the DT.\n");
394 st->channels_mask = prop;
396 if (of_property_read_u32(node, "atmel,adc-num-channels", &prop)) {
397 dev_err(&idev->dev, "Missing adc-num-channels property in the DT.\n");
401 st->num_channels = prop;
403 if (of_property_read_u32(node, "atmel,adc-startup-time", &prop)) {
404 dev_err(&idev->dev, "Missing adc-startup-time property in the DT.\n");
408 st->startup_time = prop;
411 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
412 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
418 st->registers = devm_kzalloc(&idev->dev,
419 sizeof(struct at91_adc_reg_desc),
421 if (!st->registers) {
422 dev_err(&idev->dev, "Could not allocate register memory.\n");
427 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
428 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
432 st->registers->channel_base = prop;
434 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
435 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
439 st->registers->drdy_mask = prop;
441 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
442 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
446 st->registers->status_register = prop;
448 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
449 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
453 st->registers->trigger_register = prop;
455 st->trigger_number = of_get_child_count(node);
456 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
457 sizeof(struct at91_adc_trigger),
459 if (!st->trigger_list) {
460 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
465 for_each_child_of_node(node, trig_node) {
466 struct at91_adc_trigger *trig = st->trigger_list + i;
469 if (of_property_read_string(trig_node, "trigger-name", &name)) {
470 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
476 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
477 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
482 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
492 static int at91_adc_probe_pdata(struct at91_adc_state *st,
493 struct platform_device *pdev)
495 struct at91_adc_data *pdata = pdev->dev.platform_data;
500 st->use_external = pdata->use_external_triggers;
501 st->vref_mv = pdata->vref;
502 st->channels_mask = pdata->channels_used;
503 st->num_channels = pdata->num_channels;
504 st->startup_time = pdata->startup_time;
505 st->trigger_number = pdata->trigger_number;
506 st->trigger_list = pdata->trigger_list;
507 st->registers = pdata->registers;
512 static const struct iio_info at91_adc_info = {
513 .driver_module = THIS_MODULE,
514 .read_raw = &at91_adc_read_raw,
517 static int __devinit at91_adc_probe(struct platform_device *pdev)
519 unsigned int prsc, mstrclk, ticks, adc_clk;
521 struct iio_dev *idev;
522 struct at91_adc_state *st;
523 struct resource *res;
525 idev = iio_device_alloc(sizeof(struct at91_adc_state));
533 if (pdev->dev.of_node)
534 ret = at91_adc_probe_dt(st, pdev);
536 ret = at91_adc_probe_pdata(st, pdev);
539 dev_err(&pdev->dev, "No platform data available.\n");
541 goto error_free_device;
544 platform_set_drvdata(pdev, idev);
546 idev->dev.parent = &pdev->dev;
547 idev->name = dev_name(&pdev->dev);
548 idev->modes = INDIO_DIRECT_MODE;
549 idev->info = &at91_adc_info;
551 st->irq = platform_get_irq(pdev, 0);
553 dev_err(&pdev->dev, "No IRQ ID is designated\n");
555 goto error_free_device;
558 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
560 st->reg_base = devm_request_and_ioremap(&pdev->dev, res);
563 goto error_free_device;
567 * Disable all IRQs before setting up the handler
569 at91_adc_writel(st, AT91_ADC_CR, AT91_ADC_SWRST);
570 at91_adc_writel(st, AT91_ADC_IDR, 0xFFFFFFFF);
571 ret = request_irq(st->irq,
572 at91_adc_eoc_trigger,
574 pdev->dev.driver->name,
577 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
578 goto error_free_device;
581 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
582 if (IS_ERR(st->clk)) {
583 dev_err(&pdev->dev, "Failed to get the clock.\n");
584 ret = PTR_ERR(st->clk);
588 ret = clk_prepare_enable(st->clk);
591 "Could not prepare or enable the clock.\n");
595 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
596 if (IS_ERR(st->adc_clk)) {
597 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
598 ret = PTR_ERR(st->adc_clk);
599 goto error_disable_clk;
602 ret = clk_prepare_enable(st->adc_clk);
605 "Could not prepare or enable the ADC clock.\n");
606 goto error_disable_clk;
610 * Prescaler rate computation using the formula from the Atmel's
611 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
612 * specified by the electrical characteristics of the board.
614 mstrclk = clk_get_rate(st->clk);
615 adc_clk = clk_get_rate(st->adc_clk);
616 prsc = (mstrclk / (2 * adc_clk)) - 1;
618 if (!st->startup_time) {
619 dev_err(&pdev->dev, "No startup time available.\n");
621 goto error_disable_adc_clk;
625 * Number of ticks needed to cover the startup time of the ADC as
626 * defined in the electrical characteristics of the board, divided by 8.
627 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
629 ticks = round_up((st->startup_time * adc_clk /
630 1000000) - 1, 8) / 8;
631 at91_adc_writel(st, AT91_ADC_MR,
632 (AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL) |
633 (AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP));
635 /* Setup the ADC channels available on the board */
636 ret = at91_adc_channel_init(idev);
638 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
639 goto error_disable_adc_clk;
642 init_waitqueue_head(&st->wq_data_avail);
643 mutex_init(&st->lock);
645 ret = at91_adc_buffer_init(idev);
647 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
648 goto error_disable_adc_clk;
651 ret = at91_adc_trigger_init(idev);
653 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
654 goto error_unregister_buffer;
657 ret = iio_device_register(idev);
659 dev_err(&pdev->dev, "Couldn't register the device.\n");
660 goto error_remove_triggers;
665 error_remove_triggers:
666 at91_adc_trigger_remove(idev);
667 error_unregister_buffer:
668 at91_adc_buffer_remove(idev);
669 error_disable_adc_clk:
670 clk_disable_unprepare(st->adc_clk);
672 clk_disable_unprepare(st->clk);
674 free_irq(st->irq, idev);
676 iio_device_free(idev);
681 static int __devexit at91_adc_remove(struct platform_device *pdev)
683 struct iio_dev *idev = platform_get_drvdata(pdev);
684 struct at91_adc_state *st = iio_priv(idev);
686 iio_device_unregister(idev);
687 at91_adc_trigger_remove(idev);
688 at91_adc_buffer_remove(idev);
689 clk_disable_unprepare(st->adc_clk);
690 clk_disable_unprepare(st->clk);
691 free_irq(st->irq, idev);
692 iio_device_free(idev);
697 static const struct of_device_id at91_adc_dt_ids[] = {
698 { .compatible = "atmel,at91sam9260-adc" },
701 MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
703 static struct platform_driver at91_adc_driver = {
704 .probe = at91_adc_probe,
705 .remove = __devexit_p(at91_adc_remove),
708 .of_match_table = of_match_ptr(at91_adc_dt_ids),
712 module_platform_driver(at91_adc_driver);
714 MODULE_LICENSE("GPL");
715 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
716 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");