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_caps
{
43 struct at91_adc_reg_desc registers
;
46 struct at91_adc_state
{
49 unsigned long channels_mask
;
56 void __iomem
*reg_base
;
57 struct at91_adc_reg_desc
*registers
;
61 struct iio_trigger
**trig
;
62 struct at91_adc_trigger
*trigger_list
;
66 u32 res
; /* resolution used for convertions */
67 bool low_res
; /* the resolution corresponds to the lowest one */
68 wait_queue_head_t wq_data_avail
;
69 struct at91_adc_caps
*caps
;
72 static irqreturn_t
at91_adc_trigger_handler(int irq
, void *p
)
74 struct iio_poll_func
*pf
= p
;
75 struct iio_dev
*idev
= pf
->indio_dev
;
76 struct at91_adc_state
*st
= iio_priv(idev
);
79 for (i
= 0; i
< idev
->masklength
; i
++) {
80 if (!test_bit(i
, idev
->active_scan_mask
))
82 st
->buffer
[j
] = at91_adc_readl(st
, AT91_ADC_CHAN(st
, i
));
86 iio_push_to_buffers_with_timestamp(idev
, st
->buffer
, pf
->timestamp
);
88 iio_trigger_notify_done(idev
->trig
);
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 iio_trigger_poll(idev
->trig
, iio_get_time_ns());
111 st
->last_value
= at91_adc_readl(st
, AT91_ADC_LCDR
);
113 wake_up_interruptible(&st
->wq_data_avail
);
119 static int at91_adc_channel_init(struct iio_dev
*idev
)
121 struct at91_adc_state
*st
= iio_priv(idev
);
122 struct iio_chan_spec
*chan_array
, *timestamp
;
125 idev
->num_channels
= bitmap_weight(&st
->channels_mask
,
126 st
->num_channels
) + 1;
128 chan_array
= devm_kzalloc(&idev
->dev
,
129 ((idev
->num_channels
+ 1) *
130 sizeof(struct iio_chan_spec
)),
136 for_each_set_bit(bit
, &st
->channels_mask
, st
->num_channels
) {
137 struct iio_chan_spec
*chan
= chan_array
+ idx
;
139 chan
->type
= IIO_VOLTAGE
;
142 chan
->scan_index
= idx
;
143 chan
->scan_type
.sign
= 'u';
144 chan
->scan_type
.realbits
= st
->res
;
145 chan
->scan_type
.storagebits
= 16;
146 chan
->info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
);
147 chan
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
150 timestamp
= chan_array
+ idx
;
152 timestamp
->type
= IIO_TIMESTAMP
;
153 timestamp
->channel
= -1;
154 timestamp
->scan_index
= idx
;
155 timestamp
->scan_type
.sign
= 's';
156 timestamp
->scan_type
.realbits
= 64;
157 timestamp
->scan_type
.storagebits
= 64;
159 idev
->channels
= chan_array
;
160 return idev
->num_channels
;
163 static u8
at91_adc_get_trigger_value_by_name(struct iio_dev
*idev
,
164 struct at91_adc_trigger
*triggers
,
165 const char *trigger_name
)
167 struct at91_adc_state
*st
= iio_priv(idev
);
171 for (i
= 0; i
< st
->trigger_number
; i
++) {
172 char *name
= kasprintf(GFP_KERNEL
,
180 if (strcmp(trigger_name
, name
) == 0) {
181 value
= triggers
[i
].value
;
192 static int at91_adc_configure_trigger(struct iio_trigger
*trig
, bool state
)
194 struct iio_dev
*idev
= iio_trigger_get_drvdata(trig
);
195 struct at91_adc_state
*st
= iio_priv(idev
);
196 struct iio_buffer
*buffer
= idev
->buffer
;
197 struct at91_adc_reg_desc
*reg
= st
->registers
;
198 u32 status
= at91_adc_readl(st
, reg
->trigger_register
);
202 value
= at91_adc_get_trigger_value_by_name(idev
,
209 st
->buffer
= kmalloc(idev
->scan_bytes
, GFP_KERNEL
);
210 if (st
->buffer
== NULL
)
213 at91_adc_writel(st
, reg
->trigger_register
,
216 for_each_set_bit(bit
, buffer
->scan_mask
,
218 struct iio_chan_spec
const *chan
= idev
->channels
+ bit
;
219 at91_adc_writel(st
, AT91_ADC_CHER
,
220 AT91_ADC_CH(chan
->channel
));
223 at91_adc_writel(st
, AT91_ADC_IER
, reg
->drdy_mask
);
226 at91_adc_writel(st
, AT91_ADC_IDR
, reg
->drdy_mask
);
228 at91_adc_writel(st
, reg
->trigger_register
,
231 for_each_set_bit(bit
, buffer
->scan_mask
,
233 struct iio_chan_spec
const *chan
= idev
->channels
+ bit
;
234 at91_adc_writel(st
, AT91_ADC_CHDR
,
235 AT91_ADC_CH(chan
->channel
));
243 static const struct iio_trigger_ops at91_adc_trigger_ops
= {
244 .owner
= THIS_MODULE
,
245 .set_trigger_state
= &at91_adc_configure_trigger
,
248 static struct iio_trigger
*at91_adc_allocate_trigger(struct iio_dev
*idev
,
249 struct at91_adc_trigger
*trigger
)
251 struct iio_trigger
*trig
;
254 trig
= iio_trigger_alloc("%s-dev%d-%s", idev
->name
,
255 idev
->id
, trigger
->name
);
259 trig
->dev
.parent
= idev
->dev
.parent
;
260 iio_trigger_set_drvdata(trig
, idev
);
261 trig
->ops
= &at91_adc_trigger_ops
;
263 ret
= iio_trigger_register(trig
);
270 static int at91_adc_trigger_init(struct iio_dev
*idev
)
272 struct at91_adc_state
*st
= iio_priv(idev
);
275 st
->trig
= devm_kzalloc(&idev
->dev
,
276 st
->trigger_number
* sizeof(*st
->trig
),
279 if (st
->trig
== NULL
) {
284 for (i
= 0; i
< st
->trigger_number
; i
++) {
285 if (st
->trigger_list
[i
].is_external
&& !(st
->use_external
))
288 st
->trig
[i
] = at91_adc_allocate_trigger(idev
,
289 st
->trigger_list
+ i
);
290 if (st
->trig
[i
] == NULL
) {
292 "Could not allocate trigger %d\n", i
);
301 for (i
--; i
>= 0; i
--) {
302 iio_trigger_unregister(st
->trig
[i
]);
303 iio_trigger_free(st
->trig
[i
]);
309 static void at91_adc_trigger_remove(struct iio_dev
*idev
)
311 struct at91_adc_state
*st
= iio_priv(idev
);
314 for (i
= 0; i
< st
->trigger_number
; i
++) {
315 iio_trigger_unregister(st
->trig
[i
]);
316 iio_trigger_free(st
->trig
[i
]);
320 static int at91_adc_buffer_init(struct iio_dev
*idev
)
322 return iio_triggered_buffer_setup(idev
, &iio_pollfunc_store_time
,
323 &at91_adc_trigger_handler
, NULL
);
326 static void at91_adc_buffer_remove(struct iio_dev
*idev
)
328 iio_triggered_buffer_cleanup(idev
);
331 static int at91_adc_read_raw(struct iio_dev
*idev
,
332 struct iio_chan_spec
const *chan
,
333 int *val
, int *val2
, long mask
)
335 struct at91_adc_state
*st
= iio_priv(idev
);
339 case IIO_CHAN_INFO_RAW
:
340 mutex_lock(&st
->lock
);
342 at91_adc_writel(st
, AT91_ADC_CHER
,
343 AT91_ADC_CH(chan
->channel
));
344 at91_adc_writel(st
, AT91_ADC_IER
, st
->registers
->drdy_mask
);
345 at91_adc_writel(st
, AT91_ADC_CR
, AT91_ADC_START
);
347 ret
= wait_event_interruptible_timeout(st
->wq_data_avail
,
349 msecs_to_jiffies(1000));
353 mutex_unlock(&st
->lock
);
357 *val
= st
->last_value
;
359 at91_adc_writel(st
, AT91_ADC_CHDR
,
360 AT91_ADC_CH(chan
->channel
));
361 at91_adc_writel(st
, AT91_ADC_IDR
, st
->registers
->drdy_mask
);
365 mutex_unlock(&st
->lock
);
368 case IIO_CHAN_INFO_SCALE
:
370 *val2
= chan
->scan_type
.realbits
;
371 return IIO_VAL_FRACTIONAL_LOG2
;
378 static int at91_adc_of_get_resolution(struct at91_adc_state
*st
,
379 struct platform_device
*pdev
)
381 struct iio_dev
*idev
= iio_priv_to_dev(st
);
382 struct device_node
*np
= pdev
->dev
.of_node
;
383 int count
, i
, ret
= 0;
387 count
= of_property_count_strings(np
, "atmel,adc-res-names");
389 dev_err(&idev
->dev
, "You must specified at least two resolution names for "
390 "adc-res-names property in the DT\n");
394 resolutions
= kmalloc(count
* sizeof(*resolutions
), GFP_KERNEL
);
398 if (of_property_read_u32_array(np
, "atmel,adc-res", resolutions
, count
)) {
399 dev_err(&idev
->dev
, "Missing adc-res property in the DT.\n");
404 if (of_property_read_string(np
, "atmel,adc-use-res", (const char **)&res_name
))
405 res_name
= "highres";
407 for (i
= 0; i
< count
; i
++) {
408 if (of_property_read_string_index(np
, "atmel,adc-res-names", i
, (const char **)&s
))
411 if (strcmp(res_name
, s
))
414 st
->res
= resolutions
[i
];
415 if (!strcmp(res_name
, "lowres"))
420 dev_info(&idev
->dev
, "Resolution used: %u bits\n", st
->res
);
424 dev_err(&idev
->dev
, "There is no resolution for %s\n", res_name
);
431 static const struct of_device_id at91_adc_dt_ids
[];
433 static int at91_adc_probe_dt(struct at91_adc_state
*st
,
434 struct platform_device
*pdev
)
436 struct iio_dev
*idev
= iio_priv_to_dev(st
);
437 struct device_node
*node
= pdev
->dev
.of_node
;
438 struct device_node
*trig_node
;
445 st
->caps
= (struct at91_adc_caps
*)
446 of_match_device(at91_adc_dt_ids
, &pdev
->dev
)->data
;
448 st
->use_external
= of_property_read_bool(node
, "atmel,adc-use-external-triggers");
450 if (of_property_read_u32(node
, "atmel,adc-channels-used", &prop
)) {
451 dev_err(&idev
->dev
, "Missing adc-channels-used property in the DT.\n");
455 st
->channels_mask
= prop
;
457 if (of_property_read_u32(node
, "atmel,adc-num-channels", &prop
)) {
458 dev_err(&idev
->dev
, "Missing adc-num-channels property in the DT.\n");
462 st
->num_channels
= prop
;
464 st
->sleep_mode
= of_property_read_bool(node
, "atmel,adc-sleep-mode");
466 if (of_property_read_u32(node
, "atmel,adc-startup-time", &prop
)) {
467 dev_err(&idev
->dev
, "Missing adc-startup-time property in the DT.\n");
471 st
->startup_time
= prop
;
474 of_property_read_u32(node
, "atmel,adc-sample-hold-time", &prop
);
475 st
->sample_hold_time
= prop
;
477 if (of_property_read_u32(node
, "atmel,adc-vref", &prop
)) {
478 dev_err(&idev
->dev
, "Missing adc-vref property in the DT.\n");
484 ret
= at91_adc_of_get_resolution(st
, pdev
);
488 st
->registers
= &st
->caps
->registers
;
489 st
->trigger_number
= of_get_child_count(node
);
490 st
->trigger_list
= devm_kzalloc(&idev
->dev
, st
->trigger_number
*
491 sizeof(struct at91_adc_trigger
),
493 if (!st
->trigger_list
) {
494 dev_err(&idev
->dev
, "Could not allocate trigger list memory.\n");
499 for_each_child_of_node(node
, trig_node
) {
500 struct at91_adc_trigger
*trig
= st
->trigger_list
+ i
;
503 if (of_property_read_string(trig_node
, "trigger-name", &name
)) {
504 dev_err(&idev
->dev
, "Missing trigger-name property in the DT.\n");
510 if (of_property_read_u32(trig_node
, "trigger-value", &prop
)) {
511 dev_err(&idev
->dev
, "Missing trigger-value property in the DT.\n");
516 trig
->is_external
= of_property_read_bool(trig_node
, "trigger-external");
526 static int at91_adc_probe_pdata(struct at91_adc_state
*st
,
527 struct platform_device
*pdev
)
529 struct at91_adc_data
*pdata
= pdev
->dev
.platform_data
;
534 st
->use_external
= pdata
->use_external_triggers
;
535 st
->vref_mv
= pdata
->vref
;
536 st
->channels_mask
= pdata
->channels_used
;
537 st
->num_channels
= pdata
->num_channels
;
538 st
->startup_time
= pdata
->startup_time
;
539 st
->trigger_number
= pdata
->trigger_number
;
540 st
->trigger_list
= pdata
->trigger_list
;
541 st
->registers
= pdata
->registers
;
546 static const struct iio_info at91_adc_info
= {
547 .driver_module
= THIS_MODULE
,
548 .read_raw
= &at91_adc_read_raw
,
551 static int at91_adc_probe(struct platform_device
*pdev
)
553 unsigned int prsc
, mstrclk
, ticks
, adc_clk
, adc_clk_khz
, shtim
;
555 struct iio_dev
*idev
;
556 struct at91_adc_state
*st
;
557 struct resource
*res
;
560 idev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct at91_adc_state
));
566 if (pdev
->dev
.of_node
)
567 ret
= at91_adc_probe_dt(st
, pdev
);
569 ret
= at91_adc_probe_pdata(st
, pdev
);
572 dev_err(&pdev
->dev
, "No platform data available.\n");
576 platform_set_drvdata(pdev
, idev
);
578 idev
->dev
.parent
= &pdev
->dev
;
579 idev
->name
= dev_name(&pdev
->dev
);
580 idev
->modes
= INDIO_DIRECT_MODE
;
581 idev
->info
= &at91_adc_info
;
583 st
->irq
= platform_get_irq(pdev
, 0);
585 dev_err(&pdev
->dev
, "No IRQ ID is designated\n");
589 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
591 st
->reg_base
= devm_ioremap_resource(&pdev
->dev
, res
);
592 if (IS_ERR(st
->reg_base
)) {
593 return PTR_ERR(st
->reg_base
);
597 * Disable all IRQs before setting up the handler
599 at91_adc_writel(st
, AT91_ADC_CR
, AT91_ADC_SWRST
);
600 at91_adc_writel(st
, AT91_ADC_IDR
, 0xFFFFFFFF);
601 ret
= request_irq(st
->irq
,
602 at91_adc_eoc_trigger
,
604 pdev
->dev
.driver
->name
,
607 dev_err(&pdev
->dev
, "Failed to allocate IRQ.\n");
611 st
->clk
= devm_clk_get(&pdev
->dev
, "adc_clk");
612 if (IS_ERR(st
->clk
)) {
613 dev_err(&pdev
->dev
, "Failed to get the clock.\n");
614 ret
= PTR_ERR(st
->clk
);
618 ret
= clk_prepare_enable(st
->clk
);
621 "Could not prepare or enable the clock.\n");
625 st
->adc_clk
= devm_clk_get(&pdev
->dev
, "adc_op_clk");
626 if (IS_ERR(st
->adc_clk
)) {
627 dev_err(&pdev
->dev
, "Failed to get the ADC clock.\n");
628 ret
= PTR_ERR(st
->adc_clk
);
629 goto error_disable_clk
;
632 ret
= clk_prepare_enable(st
->adc_clk
);
635 "Could not prepare or enable the ADC clock.\n");
636 goto error_disable_clk
;
640 * Prescaler rate computation using the formula from the Atmel's
641 * datasheet : ADC Clock = MCK / ((Prescaler + 1) * 2), ADC Clock being
642 * specified by the electrical characteristics of the board.
644 mstrclk
= clk_get_rate(st
->clk
);
645 adc_clk
= clk_get_rate(st
->adc_clk
);
646 adc_clk_khz
= adc_clk
/ 1000;
647 prsc
= (mstrclk
/ (2 * adc_clk
)) - 1;
649 if (!st
->startup_time
) {
650 dev_err(&pdev
->dev
, "No startup time available.\n");
652 goto error_disable_adc_clk
;
656 * Number of ticks needed to cover the startup time of the ADC as
657 * defined in the electrical characteristics of the board, divided by 8.
658 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock
660 ticks
= round_up((st
->startup_time
* adc_clk_khz
/
663 * a minimal Sample and Hold Time is necessary for the ADC to guarantee
664 * the best converted final value between two channels selection
665 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock
667 shtim
= round_up((st
->sample_hold_time
* adc_clk_khz
/
670 reg
= AT91_ADC_PRESCAL_(prsc
) & st
->registers
->mr_prescal_mask
;
671 reg
|= AT91_ADC_STARTUP_(ticks
) & st
->registers
->mr_startup_mask
;
673 reg
|= AT91_ADC_LOWRES
;
675 reg
|= AT91_ADC_SLEEP
;
676 reg
|= AT91_ADC_SHTIM_(shtim
) & AT91_ADC_SHTIM
;
677 at91_adc_writel(st
, AT91_ADC_MR
, reg
);
679 /* Setup the ADC channels available on the board */
680 ret
= at91_adc_channel_init(idev
);
682 dev_err(&pdev
->dev
, "Couldn't initialize the channels.\n");
683 goto error_disable_adc_clk
;
686 init_waitqueue_head(&st
->wq_data_avail
);
687 mutex_init(&st
->lock
);
689 ret
= at91_adc_buffer_init(idev
);
691 dev_err(&pdev
->dev
, "Couldn't initialize the buffer.\n");
692 goto error_disable_adc_clk
;
695 ret
= at91_adc_trigger_init(idev
);
697 dev_err(&pdev
->dev
, "Couldn't setup the triggers.\n");
698 goto error_unregister_buffer
;
701 ret
= iio_device_register(idev
);
703 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
704 goto error_remove_triggers
;
709 error_remove_triggers
:
710 at91_adc_trigger_remove(idev
);
711 error_unregister_buffer
:
712 at91_adc_buffer_remove(idev
);
713 error_disable_adc_clk
:
714 clk_disable_unprepare(st
->adc_clk
);
716 clk_disable_unprepare(st
->clk
);
718 free_irq(st
->irq
, idev
);
722 static int at91_adc_remove(struct platform_device
*pdev
)
724 struct iio_dev
*idev
= platform_get_drvdata(pdev
);
725 struct at91_adc_state
*st
= iio_priv(idev
);
727 iio_device_unregister(idev
);
728 at91_adc_trigger_remove(idev
);
729 at91_adc_buffer_remove(idev
);
730 clk_disable_unprepare(st
->adc_clk
);
731 clk_disable_unprepare(st
->clk
);
732 free_irq(st
->irq
, idev
);
738 static struct at91_adc_caps at91sam9260_caps
= {
740 .channel_base
= AT91_ADC_CHR(0),
741 .drdy_mask
= AT91_ADC_DRDY
,
742 .status_register
= AT91_ADC_SR
,
743 .trigger_register
= AT91_ADC_TRGR_9260
,
744 .mr_prescal_mask
= AT91_ADC_PRESCAL_9260
,
745 .mr_startup_mask
= AT91_ADC_STARTUP_9260
,
749 static struct at91_adc_caps at91sam9g45_caps
= {
751 .channel_base
= AT91_ADC_CHR(0),
752 .drdy_mask
= AT91_ADC_DRDY
,
753 .status_register
= AT91_ADC_SR
,
754 .trigger_register
= AT91_ADC_TRGR_9G45
,
755 .mr_prescal_mask
= AT91_ADC_PRESCAL_9G45
,
756 .mr_startup_mask
= AT91_ADC_STARTUP_9G45
,
760 static struct at91_adc_caps at91sam9x5_caps
= {
762 .channel_base
= AT91_ADC_CDR0_9X5
,
763 .drdy_mask
= AT91_ADC_SR_DRDY_9X5
,
764 .status_register
= AT91_ADC_SR_9X5
,
765 .trigger_register
= AT91_ADC_TRGR_9X5
,
766 /* prescal mask is same as 9G45 */
767 .mr_prescal_mask
= AT91_ADC_PRESCAL_9G45
,
768 .mr_startup_mask
= AT91_ADC_STARTUP_9X5
,
772 static const struct of_device_id at91_adc_dt_ids
[] = {
773 { .compatible
= "atmel,at91sam9260-adc", .data
= &at91sam9260_caps
},
774 { .compatible
= "atmel,at91sam9g45-adc", .data
= &at91sam9g45_caps
},
775 { .compatible
= "atmel,at91sam9x5-adc", .data
= &at91sam9x5_caps
},
778 MODULE_DEVICE_TABLE(of
, at91_adc_dt_ids
);
781 static struct platform_driver at91_adc_driver
= {
782 .probe
= at91_adc_probe
,
783 .remove
= at91_adc_remove
,
786 .of_match_table
= of_match_ptr(at91_adc_dt_ids
),
790 module_platform_driver(at91_adc_driver
);
792 MODULE_LICENSE("GPL");
793 MODULE_DESCRIPTION("Atmel AT91 ADC Driver");
794 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");