9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/wait.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))
64 static irqreturn_t at91_adc_trigger_handler(
int irq,
void *
p)
85 buffer->access->store_to(buffer, (
u8 *)st->
buffer);
98 static irqreturn_t at91_adc_eoc_trigger(
int irq,
void *
private)
100 struct iio_dev *idev =
private;
104 if (!(status & st->
registers->drdy_mask))
107 if (iio_buffer_enabled(idev)) {
120 static int at91_adc_channel_init(
struct iio_dev *idev)
151 timestamp = chan_array +
idx;
164 static u8 at91_adc_get_trigger_value_by_name(
struct iio_dev *idev,
166 const char *trigger_name)
181 if (
strcmp(trigger_name, name) == 0) {
182 value = triggers[
i].
value;
197 struct iio_buffer *buffer = idev->
buffer;
203 value = at91_adc_get_trigger_value_by_name(idev,
246 .set_trigger_state = &at91_adc_configure_trigger,
260 trig->
dev.parent = idev->
dev.parent;
262 trig->
ops = &at91_adc_trigger_ops;
271 static int at91_adc_trigger_init(
struct iio_dev *idev)
289 st->
trig[
i] = at91_adc_allocate_trigger(idev,
293 "Could not allocate trigger %d\n", i);
302 for (i--; i >= 0; i--) {
310 static void at91_adc_trigger_remove(
struct iio_dev *idev)
321 static int at91_adc_buffer_init(
struct iio_dev *idev)
324 &at91_adc_trigger_handler,
NULL);
327 static void at91_adc_buffer_remove(
struct iio_dev *idev)
332 static int at91_adc_read_raw(
struct iio_dev *idev,
334 int *
val,
int *val2,
long mask)
382 struct iio_dev *idev = iio_priv_to_dev(st);
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");
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");
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");
415 if (of_property_read_u32(node,
"atmel,adc-vref", &prop)) {
416 dev_err(&idev->
dev,
"Missing adc-vref property in the DT.\n");
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");
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");
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");
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");
464 dev_err(&idev->
dev,
"Could not allocate trigger list memory.\n");
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");
516 static const struct iio_info at91_adc_info = {
518 .read_raw = &at91_adc_read_raw,
523 unsigned int prsc, mstrclk,
ticks, adc_clk;
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 platform_set_drvdata(pdev, idev);
550 idev->
dev.parent = &pdev->
dev;
551 idev->
name = dev_name(&pdev->
dev);
553 idev->
info = &at91_adc_info;
557 dev_err(&pdev->
dev,
"No IRQ ID is designated\n");
559 goto error_free_device;
567 goto error_free_device;
576 at91_adc_eoc_trigger,
578 pdev->
dev.driver->name,
581 dev_err(&pdev->
dev,
"Failed to allocate IRQ.\n");
582 goto error_free_device;
586 if (IS_ERR(st->
clk)) {
587 dev_err(&pdev->
dev,
"Failed to get the clock.\n");
588 ret = PTR_ERR(st->
clk);
592 ret = clk_prepare_enable(st->
clk);
595 "Could not prepare or enable the clock.\n");
601 dev_err(&pdev->
dev,
"Failed to get the ADC clock.\n");
603 goto error_disable_clk;
606 ret = clk_prepare_enable(st->
adc_clk);
609 "Could not prepare or enable the ADC clock.\n");
610 goto error_disable_clk;
620 prsc = (mstrclk / (2 * adc_clk)) - 1;
623 dev_err(&pdev->
dev,
"No startup time available.\n");
625 goto error_disable_adc_clk;
634 1000000) - 1, 8) / 8;
640 ret = at91_adc_channel_init(idev);
642 dev_err(&pdev->
dev,
"Couldn't initialize the channels.\n");
643 goto error_disable_adc_clk;
649 ret = at91_adc_buffer_init(idev);
651 dev_err(&pdev->
dev,
"Couldn't initialize the buffer.\n");
652 goto error_disable_adc_clk;
655 ret = at91_adc_trigger_init(idev);
657 dev_err(&pdev->
dev,
"Couldn't setup the triggers.\n");
658 goto error_unregister_buffer;
663 dev_err(&pdev->
dev,
"Couldn't register the device.\n");
664 goto error_remove_triggers;
669 error_remove_triggers:
670 at91_adc_trigger_remove(idev);
671 error_unregister_buffer:
672 at91_adc_buffer_remove(idev);
673 error_disable_adc_clk:
674 clk_disable_unprepare(st->
adc_clk);
676 clk_disable_unprepare(st->
clk);
687 struct iio_dev *idev = platform_get_drvdata(pdev);
691 at91_adc_trigger_remove(idev);
692 at91_adc_buffer_remove(idev);
693 clk_disable_unprepare(st->
adc_clk);
694 clk_disable_unprepare(st->
clk);
702 { .compatible =
"atmel,at91sam9260-adc" },
708 .probe = at91_adc_probe,