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;
57 struct iio_trigger **trig;
58 struct at91_adc_trigger *trigger_list;
62 u32 res; /* resolution used for convertions */
63 bool low_res; /* the resolution corresponds to the lowest one */
64 wait_queue_head_t wq_data_avail;
67 static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
69 struct iio_poll_func *pf = p;
70 struct iio_dev *idev = pf->indio_dev;
71 struct at91_adc_state *st = iio_priv(idev);
74 for (i = 0; i < idev->masklength; i++) {
75 if (!test_bit(i, idev->active_scan_mask))
77 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, i));
81 if (idev->scan_timestamp) {
82 s64 *timestamp = (s64 *)((u8 *)st->buffer +
83 ALIGN(j, sizeof(s64)));
84 *timestamp = pf->timestamp;
87 iio_push_to_buffers(idev, (u8 *)st->buffer);
89 iio_trigger_notify_done(idev->trig);
91 /* Needed to ACK the DRDY interruption */
92 at91_adc_readl(st, AT91_ADC_LCDR);
99 static irqreturn_t at91_adc_eoc_trigger(int irq, void *private)
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);
105 if (!(status & st->registers->drdy_mask))
108 if (iio_buffer_enabled(idev)) {
109 disable_irq_nosync(irq);
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 = st->res;
146 chan->scan_type.storagebits = 16;
147 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
148 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
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 = iio_trigger_get_drvdata(trig);
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 iio_trigger_set_drvdata(trig, 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_of_get_resolution(struct at91_adc_state *st,
380 struct platform_device *pdev)
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;
388 count = of_property_count_strings(np, "atmel,adc-res-names");
390 dev_err(&idev->dev, "You must specified at least two resolution names for "
391 "adc-res-names property in the DT\n");
395 resolutions = kmalloc(count * sizeof(*resolutions), GFP_KERNEL);
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");
405 if (of_property_read_string(np, "atmel,adc-use-res", (const char **)&res_name))
406 res_name = "highres";
408 for (i = 0; i < count; i++) {
409 if (of_property_read_string_index(np, "atmel,adc-res-names", i, (const char **)&s))
412 if (strcmp(res_name, s))
415 st->res = resolutions[i];
416 if (!strcmp(res_name, "lowres"))
421 dev_info(&idev->dev, "Resolution used: %u bits\n", st->res);
425 dev_err(&idev->dev, "There is no resolution for %s\n", res_name);
432 static int at91_adc_probe_dt(struct at91_adc_state *st,
433 struct platform_device *pdev)
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;
444 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
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");
451 st->channels_mask = prop;
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");
458 st->num_channels = prop;
460 st->sleep_mode = of_property_read_bool(node, "atmel,adc-sleep-mode");
462 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");
467 st->startup_time = prop;
470 of_property_read_u32(node, "atmel,adc-sample-hold-time", &prop);
471 st->sample_hold_time = prop;
473 if (of_property_read_u32(node, "atmel,adc-vref", &prop)) {
474 dev_err(&idev->dev, "Missing adc-vref property in the DT.\n");
480 ret = at91_adc_of_get_resolution(st, pdev);
484 st->registers = devm_kzalloc(&idev->dev,
485 sizeof(struct at91_adc_reg_desc),
487 if (!st->registers) {
488 dev_err(&idev->dev, "Could not allocate register memory.\n");
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");
498 st->registers->channel_base = prop;
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");
505 st->registers->drdy_mask = prop;
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");
512 st->registers->status_register = prop;
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");
519 st->registers->trigger_register = prop;
521 st->trigger_number = of_get_child_count(node);
522 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
523 sizeof(struct at91_adc_trigger),
525 if (!st->trigger_list) {
526 dev_err(&idev->dev, "Could not allocate trigger list memory.\n");
531 for_each_child_of_node(node, trig_node) {
532 struct at91_adc_trigger *trig = st->trigger_list + i;
535 if (of_property_read_string(trig_node, "trigger-name", &name)) {
536 dev_err(&idev->dev, "Missing trigger-name property in the DT.\n");
542 if (of_property_read_u32(trig_node, "trigger-value", &prop)) {
543 dev_err(&idev->dev, "Missing trigger-value property in the DT.\n");
548 trig->is_external = of_property_read_bool(trig_node, "trigger-external");
558 static int at91_adc_probe_pdata(struct at91_adc_state *st,
559 struct platform_device *pdev)
561 struct at91_adc_data *pdata = pdev->dev.platform_data;
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;
578 static const struct iio_info at91_adc_info = {
579 .driver_module = THIS_MODULE,
580 .read_raw = &at91_adc_read_raw,
583 static int at91_adc_probe(struct platform_device *pdev)
585 unsigned int prsc, mstrclk, ticks, adc_clk, shtim;
587 struct iio_dev *idev;
588 struct at91_adc_state *st;
589 struct resource *res;
592 idev = iio_device_alloc(sizeof(struct at91_adc_state));
600 if (pdev->dev.of_node)
601 ret = at91_adc_probe_dt(st, pdev);
603 ret = at91_adc_probe_pdata(st, pdev);
606 dev_err(&pdev->dev, "No platform data available.\n");
608 goto error_free_device;
611 platform_set_drvdata(pdev, idev);
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;
618 st->irq = platform_get_irq(pdev, 0);
620 dev_err(&pdev->dev, "No IRQ ID is designated\n");
622 goto error_free_device;
625 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
627 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
628 if (IS_ERR(st->reg_base)) {
629 ret = PTR_ERR(st->reg_base);
630 goto error_free_device;
634 * Disable all IRQs before setting up the handler
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,
641 pdev->dev.driver->name,
644 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
645 goto error_free_device;
648 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
649 if (IS_ERR(st->clk)) {
650 dev_err(&pdev->dev, "Failed to get the clock.\n");
651 ret = PTR_ERR(st->clk);
655 ret = clk_prepare_enable(st->clk);
658 "Could not prepare or enable the clock.\n");
662 st->adc_clk = devm_clk_get(&pdev->dev, "adc_op_clk");
663 if (IS_ERR(st->adc_clk)) {
664 dev_err(&pdev->dev, "Failed to get the ADC clock.\n");
665 ret = PTR_ERR(st->adc_clk);
666 goto error_disable_clk;
669 ret = clk_prepare_enable(st->adc_clk);
672 "Could not prepare or enable the ADC clock.\n");
673 goto error_disable_clk;
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.
681 mstrclk = clk_get_rate(st->clk);
682 adc_clk = clk_get_rate(st->adc_clk);
683 prsc = (mstrclk / (2 * adc_clk)) - 1;
685 if (!st->startup_time) {
686 dev_err(&pdev->dev, "No startup time available.\n");
688 goto error_disable_adc_clk;
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
696 ticks = round_up((st->startup_time * adc_clk /
697 1000000) - 1, 8) / 8;
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
703 shtim = round_up((st->sample_hold_time * adc_clk /
706 reg = AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL;
707 reg |= AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP;
709 reg |= AT91_ADC_LOWRES;
711 reg |= AT91_ADC_SLEEP;
712 reg |= AT91_ADC_SHTIM_(shtim) & AT91_ADC_SHTIM;
713 at91_adc_writel(st, AT91_ADC_MR, reg);
715 /* Setup the ADC channels available on the board */
716 ret = at91_adc_channel_init(idev);
718 dev_err(&pdev->dev, "Couldn't initialize the channels.\n");
719 goto error_disable_adc_clk;
722 init_waitqueue_head(&st->wq_data_avail);
723 mutex_init(&st->lock);
725 ret = at91_adc_buffer_init(idev);
727 dev_err(&pdev->dev, "Couldn't initialize the buffer.\n");
728 goto error_disable_adc_clk;
731 ret = at91_adc_trigger_init(idev);
733 dev_err(&pdev->dev, "Couldn't setup the triggers.\n");
734 goto error_unregister_buffer;
737 ret = iio_device_register(idev);
739 dev_err(&pdev->dev, "Couldn't register the device.\n");
740 goto error_remove_triggers;
745 error_remove_triggers:
746 at91_adc_trigger_remove(idev);
747 error_unregister_buffer:
748 at91_adc_buffer_remove(idev);
749 error_disable_adc_clk:
750 clk_disable_unprepare(st->adc_clk);
752 clk_disable_unprepare(st->clk);
754 free_irq(st->irq, idev);
756 iio_device_free(idev);
761 static int at91_adc_remove(struct platform_device *pdev)
763 struct iio_dev *idev = platform_get_drvdata(pdev);
764 struct at91_adc_state *st = iio_priv(idev);
766 iio_device_unregister(idev);
767 at91_adc_trigger_remove(idev);
768 at91_adc_buffer_remove(idev);
769 clk_disable_unprepare(st->adc_clk);
770 clk_disable_unprepare(st->clk);
771 free_irq(st->irq, idev);
772 iio_device_free(idev);
777 static const struct of_device_id at91_adc_dt_ids[] = {
778 { .compatible = "atmel,at91sam9260-adc" },
781 MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
783 static struct platform_driver at91_adc_driver = {
784 .probe = at91_adc_probe,
785 .remove = at91_adc_remove,
788 .of_match_table = of_match_ptr(at91_adc_dt_ids),
792 module_platform_driver(at91_adc_driver);
794 MODULE_LICENSE("GPL");
795 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
796 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");