| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Atmel ADC driver for SAMA5D2 devices and compatible. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2015 Atmel, |
|---|
| 5 | 6 | * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 8 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 9 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | | - * GNU General Public License for more details. |
|---|
| 15 | 7 | */ |
|---|
| 16 | 8 | |
|---|
| 17 | 9 | #include <linux/bitops.h> |
|---|
| 18 | 10 | #include <linux/clk.h> |
|---|
| 11 | +#include <linux/delay.h> |
|---|
| 19 | 12 | #include <linux/dma-mapping.h> |
|---|
| 20 | 13 | #include <linux/dmaengine.h> |
|---|
| 21 | 14 | #include <linux/interrupt.h> |
|---|
| .. | .. |
|---|
| 81 | 74 | #define AT91_SAMA5D2_MR_ANACH BIT(23) |
|---|
| 82 | 75 | /* Tracking Time */ |
|---|
| 83 | 76 | #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24) |
|---|
| 84 | | -#define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff |
|---|
| 77 | +#define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xf |
|---|
| 85 | 78 | /* Transfer Time */ |
|---|
| 86 | 79 | #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28) |
|---|
| 87 | 80 | #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3 |
|---|
| .. | .. |
|---|
| 108 | 101 | #define AT91_SAMA5D2_IER_YRDY BIT(21) |
|---|
| 109 | 102 | /* Interrupt Enable Register - TS pressure measurement ready */ |
|---|
| 110 | 103 | #define AT91_SAMA5D2_IER_PRDY BIT(22) |
|---|
| 104 | +/* Interrupt Enable Register - Data ready */ |
|---|
| 105 | +#define AT91_SAMA5D2_IER_DRDY BIT(24) |
|---|
| 111 | 106 | /* Interrupt Enable Register - general overrun error */ |
|---|
| 112 | 107 | #define AT91_SAMA5D2_IER_GOVRE BIT(25) |
|---|
| 113 | 108 | /* Interrupt Enable Register - Pen detect */ |
|---|
| .. | .. |
|---|
| 352 | 347 | }; |
|---|
| 353 | 348 | |
|---|
| 354 | 349 | /** |
|---|
| 355 | | - * at91_adc_dma - at91-sama5d2 dma information struct |
|---|
| 350 | + * struct at91_adc_dma - at91-sama5d2 dma information struct |
|---|
| 356 | 351 | * @dma_chan: the dma channel acquired |
|---|
| 357 | 352 | * @rx_buf: dma coherent allocated area |
|---|
| 358 | 353 | * @rx_dma_buf: dma handler for the buffer |
|---|
| .. | .. |
|---|
| 374 | 369 | }; |
|---|
| 375 | 370 | |
|---|
| 376 | 371 | /** |
|---|
| 377 | | - * at91_adc_touch - at91-sama5d2 touchscreen information struct |
|---|
| 372 | + * struct at91_adc_touch - at91-sama5d2 touchscreen information struct |
|---|
| 378 | 373 | * @sample_period_val: the value for periodic trigger interval |
|---|
| 379 | 374 | * @touching: is the pen touching the screen or not |
|---|
| 380 | 375 | * @x_pos: temporary placeholder for pressure computation |
|---|
| .. | .. |
|---|
| 407 | 402 | wait_queue_head_t wq_data_available; |
|---|
| 408 | 403 | struct at91_adc_dma dma_st; |
|---|
| 409 | 404 | struct at91_adc_touch touch_st; |
|---|
| 410 | | - u16 buffer[AT91_BUFFER_MAX_HWORDS]; |
|---|
| 405 | + struct iio_dev *indio_dev; |
|---|
| 406 | + /* Ensure naturally aligned timestamp */ |
|---|
| 407 | + u16 buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8); |
|---|
| 411 | 408 | /* |
|---|
| 412 | 409 | * lock to prevent concurrent 'single conversion' requests through |
|---|
| 413 | 410 | * sysfs. |
|---|
| .. | .. |
|---|
| 492 | 489 | const struct of_phandle_args *iiospec) |
|---|
| 493 | 490 | { |
|---|
| 494 | 491 | return at91_adc_chan_xlate(indio_dev, iiospec->args[0]); |
|---|
| 492 | +} |
|---|
| 493 | + |
|---|
| 494 | +static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev *indio_dev) |
|---|
| 495 | +{ |
|---|
| 496 | + u32 mask = 0; |
|---|
| 497 | + u8 bit; |
|---|
| 498 | + |
|---|
| 499 | + for_each_set_bit(bit, indio_dev->active_scan_mask, |
|---|
| 500 | + indio_dev->num_channels) { |
|---|
| 501 | + struct iio_chan_spec const *chan = |
|---|
| 502 | + at91_adc_chan_get(indio_dev, bit); |
|---|
| 503 | + mask |= BIT(chan->channel); |
|---|
| 504 | + } |
|---|
| 505 | + |
|---|
| 506 | + return mask & GENMASK(11, 0); |
|---|
| 495 | 507 | } |
|---|
| 496 | 508 | |
|---|
| 497 | 509 | static void at91_adc_config_emr(struct at91_adc_state *st) |
|---|
| .. | .. |
|---|
| 632 | 644 | /* first half of register is the x or y, second half is the scale */ |
|---|
| 633 | 645 | val = at91_adc_readl(st, reg); |
|---|
| 634 | 646 | if (!val) |
|---|
| 635 | | - dev_dbg(&iio_priv_to_dev(st)->dev, "pos is 0\n"); |
|---|
| 647 | + dev_dbg(&st->indio_dev->dev, "pos is 0\n"); |
|---|
| 636 | 648 | |
|---|
| 637 | 649 | pos = val & AT91_SAMA5D2_XYZ_MASK; |
|---|
| 638 | 650 | result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos; |
|---|
| 639 | 651 | scale = (val >> 16) & AT91_SAMA5D2_XYZ_MASK; |
|---|
| 640 | 652 | if (scale == 0) { |
|---|
| 641 | | - dev_err(&iio_priv_to_dev(st)->dev, "scale is 0\n"); |
|---|
| 653 | + dev_err(&st->indio_dev->dev, "scale is 0\n"); |
|---|
| 642 | 654 | return 0; |
|---|
| 643 | 655 | } |
|---|
| 644 | 656 | result /= scale; |
|---|
| .. | .. |
|---|
| 718 | 730 | struct iio_dev *indio = iio_trigger_get_drvdata(trig); |
|---|
| 719 | 731 | struct at91_adc_state *st = iio_priv(indio); |
|---|
| 720 | 732 | u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR); |
|---|
| 721 | | - u8 bit; |
|---|
| 722 | 733 | |
|---|
| 723 | 734 | /* clear TRGMOD */ |
|---|
| 724 | 735 | status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK; |
|---|
| .. | .. |
|---|
| 728 | 739 | |
|---|
| 729 | 740 | /* set/unset hw trigger */ |
|---|
| 730 | 741 | at91_adc_writel(st, AT91_SAMA5D2_TRGR, status); |
|---|
| 731 | | - |
|---|
| 732 | | - for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) { |
|---|
| 733 | | - struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit); |
|---|
| 734 | | - u32 cor; |
|---|
| 735 | | - |
|---|
| 736 | | - if (!chan) |
|---|
| 737 | | - continue; |
|---|
| 738 | | - /* these channel types cannot be handled by this trigger */ |
|---|
| 739 | | - if (chan->type == IIO_POSITIONRELATIVE || |
|---|
| 740 | | - chan->type == IIO_PRESSURE) |
|---|
| 741 | | - continue; |
|---|
| 742 | | - |
|---|
| 743 | | - if (state) { |
|---|
| 744 | | - cor = at91_adc_readl(st, AT91_SAMA5D2_COR); |
|---|
| 745 | | - |
|---|
| 746 | | - if (chan->differential) |
|---|
| 747 | | - cor |= (BIT(chan->channel) | |
|---|
| 748 | | - BIT(chan->channel2)) << |
|---|
| 749 | | - AT91_SAMA5D2_COR_DIFF_OFFSET; |
|---|
| 750 | | - else |
|---|
| 751 | | - cor &= ~(BIT(chan->channel) << |
|---|
| 752 | | - AT91_SAMA5D2_COR_DIFF_OFFSET); |
|---|
| 753 | | - |
|---|
| 754 | | - at91_adc_writel(st, AT91_SAMA5D2_COR, cor); |
|---|
| 755 | | - } |
|---|
| 756 | | - |
|---|
| 757 | | - if (state) { |
|---|
| 758 | | - at91_adc_writel(st, AT91_SAMA5D2_CHER, |
|---|
| 759 | | - BIT(chan->channel)); |
|---|
| 760 | | - /* enable irq only if not using DMA */ |
|---|
| 761 | | - if (!st->dma_st.dma_chan) { |
|---|
| 762 | | - at91_adc_writel(st, AT91_SAMA5D2_IER, |
|---|
| 763 | | - BIT(chan->channel)); |
|---|
| 764 | | - } |
|---|
| 765 | | - } else { |
|---|
| 766 | | - /* disable irq only if not using DMA */ |
|---|
| 767 | | - if (!st->dma_st.dma_chan) { |
|---|
| 768 | | - at91_adc_writel(st, AT91_SAMA5D2_IDR, |
|---|
| 769 | | - BIT(chan->channel)); |
|---|
| 770 | | - } |
|---|
| 771 | | - at91_adc_writel(st, AT91_SAMA5D2_CHDR, |
|---|
| 772 | | - BIT(chan->channel)); |
|---|
| 773 | | - } |
|---|
| 774 | | - } |
|---|
| 775 | 742 | |
|---|
| 776 | 743 | return 0; |
|---|
| 777 | 744 | } |
|---|
| .. | .. |
|---|
| 789 | 756 | |
|---|
| 790 | 757 | /* Needed to ACK the DRDY interruption */ |
|---|
| 791 | 758 | at91_adc_readl(st, AT91_SAMA5D2_LCDR); |
|---|
| 759 | + |
|---|
| 792 | 760 | return 0; |
|---|
| 793 | 761 | } |
|---|
| 794 | 762 | |
|---|
| .. | .. |
|---|
| 896 | 864 | return 0; |
|---|
| 897 | 865 | } |
|---|
| 898 | 866 | |
|---|
| 899 | | -static int at91_adc_buffer_postenable(struct iio_dev *indio_dev) |
|---|
| 867 | +static bool at91_adc_buffer_check_use_irq(struct iio_dev *indio, |
|---|
| 868 | + struct at91_adc_state *st) |
|---|
| 869 | +{ |
|---|
| 870 | + /* if using DMA, we do not use our own IRQ (we use DMA-controller) */ |
|---|
| 871 | + if (st->dma_st.dma_chan) |
|---|
| 872 | + return false; |
|---|
| 873 | + /* if the trigger is not ours, then it has its own IRQ */ |
|---|
| 874 | + if (iio_trigger_validate_own_device(indio->trig, indio)) |
|---|
| 875 | + return false; |
|---|
| 876 | + return true; |
|---|
| 877 | +} |
|---|
| 878 | + |
|---|
| 879 | +static bool at91_adc_current_chan_is_touch(struct iio_dev *indio_dev) |
|---|
| 880 | +{ |
|---|
| 881 | + struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 882 | + |
|---|
| 883 | + return !!bitmap_subset(indio_dev->active_scan_mask, |
|---|
| 884 | + &st->touch_st.channels_bitmask, |
|---|
| 885 | + AT91_SAMA5D2_MAX_CHAN_IDX + 1); |
|---|
| 886 | +} |
|---|
| 887 | + |
|---|
| 888 | +static int at91_adc_buffer_prepare(struct iio_dev *indio_dev) |
|---|
| 900 | 889 | { |
|---|
| 901 | 890 | int ret; |
|---|
| 891 | + u8 bit; |
|---|
| 902 | 892 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 903 | 893 | |
|---|
| 904 | 894 | /* check if we are enabling triggered buffer or the touchscreen */ |
|---|
| 905 | | - if (bitmap_subset(indio_dev->active_scan_mask, |
|---|
| 906 | | - &st->touch_st.channels_bitmask, |
|---|
| 907 | | - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { |
|---|
| 908 | | - /* touchscreen enabling */ |
|---|
| 895 | + if (at91_adc_current_chan_is_touch(indio_dev)) |
|---|
| 909 | 896 | return at91_adc_configure_touch(st, true); |
|---|
| 910 | | - } |
|---|
| 897 | + |
|---|
| 911 | 898 | /* if we are not in triggered mode, we cannot enable the buffer. */ |
|---|
| 912 | 899 | if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) |
|---|
| 913 | 900 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 915 | 902 | /* we continue with the triggered buffer */ |
|---|
| 916 | 903 | ret = at91_adc_dma_start(indio_dev); |
|---|
| 917 | 904 | if (ret) { |
|---|
| 918 | | - dev_err(&indio_dev->dev, "buffer postenable failed\n"); |
|---|
| 905 | + dev_err(&indio_dev->dev, "buffer prepare failed\n"); |
|---|
| 919 | 906 | return ret; |
|---|
| 920 | 907 | } |
|---|
| 921 | 908 | |
|---|
| 922 | | - return iio_triggered_buffer_postenable(indio_dev); |
|---|
| 909 | + for_each_set_bit(bit, indio_dev->active_scan_mask, |
|---|
| 910 | + indio_dev->num_channels) { |
|---|
| 911 | + struct iio_chan_spec const *chan = |
|---|
| 912 | + at91_adc_chan_get(indio_dev, bit); |
|---|
| 913 | + u32 cor; |
|---|
| 914 | + |
|---|
| 915 | + if (!chan) |
|---|
| 916 | + continue; |
|---|
| 917 | + /* these channel types cannot be handled by this trigger */ |
|---|
| 918 | + if (chan->type == IIO_POSITIONRELATIVE || |
|---|
| 919 | + chan->type == IIO_PRESSURE) |
|---|
| 920 | + continue; |
|---|
| 921 | + |
|---|
| 922 | + cor = at91_adc_readl(st, AT91_SAMA5D2_COR); |
|---|
| 923 | + |
|---|
| 924 | + if (chan->differential) |
|---|
| 925 | + cor |= (BIT(chan->channel) | BIT(chan->channel2)) << |
|---|
| 926 | + AT91_SAMA5D2_COR_DIFF_OFFSET; |
|---|
| 927 | + else |
|---|
| 928 | + cor &= ~(BIT(chan->channel) << |
|---|
| 929 | + AT91_SAMA5D2_COR_DIFF_OFFSET); |
|---|
| 930 | + |
|---|
| 931 | + at91_adc_writel(st, AT91_SAMA5D2_COR, cor); |
|---|
| 932 | + |
|---|
| 933 | + at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); |
|---|
| 934 | + } |
|---|
| 935 | + |
|---|
| 936 | + if (at91_adc_buffer_check_use_irq(indio_dev, st)) |
|---|
| 937 | + at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_DRDY); |
|---|
| 938 | + |
|---|
| 939 | + return 0; |
|---|
| 923 | 940 | } |
|---|
| 924 | 941 | |
|---|
| 925 | | -static int at91_adc_buffer_predisable(struct iio_dev *indio_dev) |
|---|
| 942 | +static int at91_adc_buffer_postdisable(struct iio_dev *indio_dev) |
|---|
| 926 | 943 | { |
|---|
| 927 | 944 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 928 | | - int ret; |
|---|
| 929 | 945 | u8 bit; |
|---|
| 930 | 946 | |
|---|
| 931 | 947 | /* check if we are disabling triggered buffer or the touchscreen */ |
|---|
| 932 | | - if (bitmap_subset(indio_dev->active_scan_mask, |
|---|
| 933 | | - &st->touch_st.channels_bitmask, |
|---|
| 934 | | - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { |
|---|
| 935 | | - /* touchscreen disable */ |
|---|
| 948 | + if (at91_adc_current_chan_is_touch(indio_dev)) |
|---|
| 936 | 949 | return at91_adc_configure_touch(st, false); |
|---|
| 937 | | - } |
|---|
| 950 | + |
|---|
| 938 | 951 | /* if we are not in triggered mode, nothing to do here */ |
|---|
| 939 | 952 | if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) |
|---|
| 940 | 953 | return -EINVAL; |
|---|
| 941 | 954 | |
|---|
| 942 | | - /* continue with the triggered buffer */ |
|---|
| 943 | | - ret = iio_triggered_buffer_predisable(indio_dev); |
|---|
| 944 | | - if (ret < 0) |
|---|
| 945 | | - dev_err(&indio_dev->dev, "buffer predisable failed\n"); |
|---|
| 946 | | - |
|---|
| 947 | | - if (!st->dma_st.dma_chan) |
|---|
| 948 | | - return ret; |
|---|
| 949 | | - |
|---|
| 950 | | - /* if we are using DMA we must clear registers and end DMA */ |
|---|
| 951 | | - dmaengine_terminate_sync(st->dma_st.dma_chan); |
|---|
| 952 | | - |
|---|
| 953 | 955 | /* |
|---|
| 954 | | - * For each enabled channel we must read the last converted value |
|---|
| 956 | + * For each enable channel we must disable it in hardware. |
|---|
| 957 | + * In the case of DMA, we must read the last converted value |
|---|
| 955 | 958 | * to clear EOC status and not get a possible interrupt later. |
|---|
| 956 | | - * This value is being read by DMA from LCDR anyway |
|---|
| 959 | + * This value is being read by DMA from LCDR anyway, so it's not lost. |
|---|
| 957 | 960 | */ |
|---|
| 958 | 961 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
|---|
| 959 | 962 | indio_dev->num_channels) { |
|---|
| .. | .. |
|---|
| 966 | 969 | if (chan->type == IIO_POSITIONRELATIVE || |
|---|
| 967 | 970 | chan->type == IIO_PRESSURE) |
|---|
| 968 | 971 | continue; |
|---|
| 972 | + |
|---|
| 973 | + at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel)); |
|---|
| 974 | + |
|---|
| 969 | 975 | if (st->dma_st.dma_chan) |
|---|
| 970 | 976 | at91_adc_readl(st, chan->address); |
|---|
| 971 | 977 | } |
|---|
| 972 | 978 | |
|---|
| 979 | + if (at91_adc_buffer_check_use_irq(indio_dev, st)) |
|---|
| 980 | + at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_DRDY); |
|---|
| 981 | + |
|---|
| 973 | 982 | /* read overflow register to clear possible overflow status */ |
|---|
| 974 | 983 | at91_adc_readl(st, AT91_SAMA5D2_OVER); |
|---|
| 975 | | - return ret; |
|---|
| 984 | + |
|---|
| 985 | + /* if we are using DMA we must clear registers and end DMA */ |
|---|
| 986 | + if (st->dma_st.dma_chan) |
|---|
| 987 | + dmaengine_terminate_sync(st->dma_st.dma_chan); |
|---|
| 988 | + |
|---|
| 989 | + return 0; |
|---|
| 976 | 990 | } |
|---|
| 977 | 991 | |
|---|
| 978 | 992 | static const struct iio_buffer_setup_ops at91_buffer_setup_ops = { |
|---|
| 979 | | - .postenable = &at91_adc_buffer_postenable, |
|---|
| 980 | | - .predisable = &at91_adc_buffer_predisable, |
|---|
| 993 | + .postdisable = &at91_adc_buffer_postdisable, |
|---|
| 981 | 994 | }; |
|---|
| 982 | 995 | |
|---|
| 983 | 996 | static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio, |
|---|
| .. | .. |
|---|
| 989 | 1002 | trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name, |
|---|
| 990 | 1003 | indio->id, trigger_name); |
|---|
| 991 | 1004 | if (!trig) |
|---|
| 992 | | - return NULL; |
|---|
| 1005 | + return ERR_PTR(-ENOMEM); |
|---|
| 993 | 1006 | |
|---|
| 994 | 1007 | trig->dev.parent = indio->dev.parent; |
|---|
| 995 | 1008 | iio_trigger_set_drvdata(trig, indio); |
|---|
| .. | .. |
|---|
| 1023 | 1036 | int i = 0; |
|---|
| 1024 | 1037 | int val; |
|---|
| 1025 | 1038 | u8 bit; |
|---|
| 1039 | + u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev); |
|---|
| 1040 | + unsigned int timeout = 50; |
|---|
| 1041 | + |
|---|
| 1042 | + /* |
|---|
| 1043 | + * Check if the conversion is ready. If not, wait a little bit, and |
|---|
| 1044 | + * in case of timeout exit with an error. |
|---|
| 1045 | + */ |
|---|
| 1046 | + while ((at91_adc_readl(st, AT91_SAMA5D2_ISR) & mask) != mask && |
|---|
| 1047 | + timeout) { |
|---|
| 1048 | + usleep_range(50, 100); |
|---|
| 1049 | + timeout--; |
|---|
| 1050 | + } |
|---|
| 1051 | + |
|---|
| 1052 | + /* Cannot read data, not ready. Continue without reporting data */ |
|---|
| 1053 | + if (!timeout) |
|---|
| 1054 | + return; |
|---|
| 1026 | 1055 | |
|---|
| 1027 | 1056 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
|---|
| 1028 | 1057 | indio_dev->num_channels) { |
|---|
| .. | .. |
|---|
| 1110 | 1139 | struct iio_dev *indio_dev = pf->indio_dev; |
|---|
| 1111 | 1140 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1112 | 1141 | |
|---|
| 1142 | + /* |
|---|
| 1143 | + * If it's not our trigger, start a conversion now, as we are |
|---|
| 1144 | + * actually polling the trigger now. |
|---|
| 1145 | + */ |
|---|
| 1146 | + if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev)) |
|---|
| 1147 | + at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); |
|---|
| 1148 | + |
|---|
| 1113 | 1149 | if (st->dma_st.dma_chan) |
|---|
| 1114 | 1150 | at91_adc_trigger_handler_dma(indio_dev); |
|---|
| 1115 | 1151 | else |
|---|
| .. | .. |
|---|
| 1122 | 1158 | |
|---|
| 1123 | 1159 | static int at91_adc_buffer_init(struct iio_dev *indio) |
|---|
| 1124 | 1160 | { |
|---|
| 1125 | | - struct at91_adc_state *st = iio_priv(indio); |
|---|
| 1126 | | - |
|---|
| 1127 | | - if (st->selected_trig->hw_trig) { |
|---|
| 1128 | | - return devm_iio_triggered_buffer_setup(&indio->dev, indio, |
|---|
| 1129 | | - &iio_pollfunc_store_time, |
|---|
| 1130 | | - &at91_adc_trigger_handler, &at91_buffer_setup_ops); |
|---|
| 1131 | | - } |
|---|
| 1132 | | - /* |
|---|
| 1133 | | - * we need to prepare the buffer ops in case we will get |
|---|
| 1134 | | - * another buffer attached (like a callback buffer for the touchscreen) |
|---|
| 1135 | | - */ |
|---|
| 1136 | | - indio->setup_ops = &at91_buffer_setup_ops; |
|---|
| 1137 | | - |
|---|
| 1138 | | - return 0; |
|---|
| 1161 | + return devm_iio_triggered_buffer_setup(&indio->dev, indio, |
|---|
| 1162 | + &iio_pollfunc_store_time, |
|---|
| 1163 | + &at91_adc_trigger_handler, &at91_buffer_setup_ops); |
|---|
| 1139 | 1164 | } |
|---|
| 1140 | 1165 | |
|---|
| 1141 | 1166 | static unsigned at91_adc_startup_time(unsigned startup_time_min, |
|---|
| .. | .. |
|---|
| 1162 | 1187 | return i; |
|---|
| 1163 | 1188 | } |
|---|
| 1164 | 1189 | |
|---|
| 1165 | | -static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq) |
|---|
| 1190 | +static void at91_adc_setup_samp_freq(struct iio_dev *indio_dev, unsigned freq) |
|---|
| 1166 | 1191 | { |
|---|
| 1167 | | - struct iio_dev *indio_dev = iio_priv_to_dev(st); |
|---|
| 1192 | + struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1168 | 1193 | unsigned f_per, prescal, startup, mr; |
|---|
| 1169 | 1194 | |
|---|
| 1170 | 1195 | f_per = clk_get_rate(st->per_clk); |
|---|
| .. | .. |
|---|
| 1233 | 1258 | st->touch_st.touching = true; |
|---|
| 1234 | 1259 | } |
|---|
| 1235 | 1260 | |
|---|
| 1236 | | -static void at91_adc_no_pen_detect_interrupt(struct at91_adc_state *st) |
|---|
| 1261 | +static void at91_adc_no_pen_detect_interrupt(struct iio_dev *indio_dev) |
|---|
| 1237 | 1262 | { |
|---|
| 1238 | | - struct iio_dev *indio_dev = iio_priv_to_dev(st); |
|---|
| 1263 | + struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1239 | 1264 | |
|---|
| 1240 | 1265 | at91_adc_writel(st, AT91_SAMA5D2_TRGR, |
|---|
| 1241 | 1266 | AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER); |
|---|
| .. | .. |
|---|
| 1255 | 1280 | struct at91_adc_touch, workq); |
|---|
| 1256 | 1281 | struct at91_adc_state *st = container_of(touch_st, |
|---|
| 1257 | 1282 | struct at91_adc_state, touch_st); |
|---|
| 1258 | | - struct iio_dev *indio_dev = iio_priv_to_dev(st); |
|---|
| 1283 | + struct iio_dev *indio_dev = st->indio_dev; |
|---|
| 1259 | 1284 | |
|---|
| 1260 | 1285 | iio_push_to_buffers(indio_dev, st->buffer); |
|---|
| 1261 | 1286 | } |
|---|
| .. | .. |
|---|
| 1276 | 1301 | at91_adc_pen_detect_interrupt(st); |
|---|
| 1277 | 1302 | } else if ((status & AT91_SAMA5D2_IER_NOPEN)) { |
|---|
| 1278 | 1303 | /* nopen detected IRQ */ |
|---|
| 1279 | | - at91_adc_no_pen_detect_interrupt(st); |
|---|
| 1304 | + at91_adc_no_pen_detect_interrupt(indio); |
|---|
| 1280 | 1305 | } else if ((status & AT91_SAMA5D2_ISR_PENS) && |
|---|
| 1281 | 1306 | ((status & rdy_mask) == rdy_mask)) { |
|---|
| 1282 | 1307 | /* periodic trigger IRQ - during pen sense */ |
|---|
| .. | .. |
|---|
| 1289 | 1314 | status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR); |
|---|
| 1290 | 1315 | status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR); |
|---|
| 1291 | 1316 | status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR); |
|---|
| 1292 | | - } else if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) { |
|---|
| 1317 | + } else if (iio_buffer_enabled(indio) && |
|---|
| 1318 | + (status & AT91_SAMA5D2_IER_DRDY)) { |
|---|
| 1293 | 1319 | /* triggered buffer without DMA */ |
|---|
| 1294 | 1320 | disable_irq_nosync(irq); |
|---|
| 1295 | 1321 | iio_trigger_poll(indio->trig); |
|---|
| .. | .. |
|---|
| 1327 | 1353 | ret = at91_adc_read_position(st, chan->channel, |
|---|
| 1328 | 1354 | &tmp_val); |
|---|
| 1329 | 1355 | *val = tmp_val; |
|---|
| 1356 | + if (ret > 0) |
|---|
| 1357 | + ret = at91_adc_adjust_val_osr(st, val); |
|---|
| 1330 | 1358 | mutex_unlock(&st->lock); |
|---|
| 1331 | 1359 | iio_device_release_direct_mode(indio_dev); |
|---|
| 1332 | 1360 | |
|---|
| 1333 | | - return at91_adc_adjust_val_osr(st, val); |
|---|
| 1361 | + return ret; |
|---|
| 1334 | 1362 | } |
|---|
| 1335 | 1363 | if (chan->type == IIO_PRESSURE) { |
|---|
| 1336 | 1364 | ret = iio_device_claim_direct_mode(indio_dev); |
|---|
| .. | .. |
|---|
| 1341 | 1369 | ret = at91_adc_read_pressure(st, chan->channel, |
|---|
| 1342 | 1370 | &tmp_val); |
|---|
| 1343 | 1371 | *val = tmp_val; |
|---|
| 1372 | + if (ret > 0) |
|---|
| 1373 | + ret = at91_adc_adjust_val_osr(st, val); |
|---|
| 1344 | 1374 | mutex_unlock(&st->lock); |
|---|
| 1345 | 1375 | iio_device_release_direct_mode(indio_dev); |
|---|
| 1346 | 1376 | |
|---|
| 1347 | | - return at91_adc_adjust_val_osr(st, val); |
|---|
| 1377 | + return ret; |
|---|
| 1348 | 1378 | } |
|---|
| 1349 | 1379 | |
|---|
| 1350 | 1380 | /* in this case we have a voltage channel */ |
|---|
| .. | .. |
|---|
| 1435 | 1465 | /* if no change, optimize out */ |
|---|
| 1436 | 1466 | if (val == st->oversampling_ratio) |
|---|
| 1437 | 1467 | return 0; |
|---|
| 1468 | + mutex_lock(&st->lock); |
|---|
| 1438 | 1469 | st->oversampling_ratio = val; |
|---|
| 1439 | 1470 | /* update ratio */ |
|---|
| 1440 | 1471 | at91_adc_config_emr(st); |
|---|
| 1472 | + mutex_unlock(&st->lock); |
|---|
| 1441 | 1473 | return 0; |
|---|
| 1442 | 1474 | case IIO_CHAN_INFO_SAMP_FREQ: |
|---|
| 1443 | 1475 | if (val < st->soc_info.min_sample_rate || |
|---|
| 1444 | 1476 | val > st->soc_info.max_sample_rate) |
|---|
| 1445 | 1477 | return -EINVAL; |
|---|
| 1446 | 1478 | |
|---|
| 1447 | | - at91_adc_setup_samp_freq(st, val); |
|---|
| 1479 | + mutex_lock(&st->lock); |
|---|
| 1480 | + at91_adc_setup_samp_freq(indio_dev, val); |
|---|
| 1481 | + mutex_unlock(&st->lock); |
|---|
| 1448 | 1482 | return 0; |
|---|
| 1449 | 1483 | default: |
|---|
| 1450 | 1484 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1468 | 1502 | if (st->dma_st.dma_chan) |
|---|
| 1469 | 1503 | return; |
|---|
| 1470 | 1504 | |
|---|
| 1471 | | - st->dma_st.dma_chan = dma_request_slave_channel(&pdev->dev, "rx"); |
|---|
| 1472 | | - |
|---|
| 1473 | | - if (!st->dma_st.dma_chan) { |
|---|
| 1505 | + st->dma_st.dma_chan = dma_request_chan(&pdev->dev, "rx"); |
|---|
| 1506 | + if (IS_ERR(st->dma_st.dma_chan)) { |
|---|
| 1474 | 1507 | dev_info(&pdev->dev, "can't get DMA channel\n"); |
|---|
| 1508 | + st->dma_st.dma_chan = NULL; |
|---|
| 1475 | 1509 | goto dma_exit; |
|---|
| 1476 | 1510 | } |
|---|
| 1477 | 1511 | |
|---|
| .. | .. |
|---|
| 1507 | 1541 | st->dma_st.rx_buf, st->dma_st.rx_dma_buf); |
|---|
| 1508 | 1542 | dma_chan_disable: |
|---|
| 1509 | 1543 | dma_release_channel(st->dma_st.dma_chan); |
|---|
| 1510 | | - st->dma_st.dma_chan = 0; |
|---|
| 1544 | + st->dma_st.dma_chan = NULL; |
|---|
| 1511 | 1545 | dma_exit: |
|---|
| 1512 | 1546 | dev_info(&pdev->dev, "continuing without DMA support\n"); |
|---|
| 1513 | 1547 | } |
|---|
| .. | .. |
|---|
| 1530 | 1564 | dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE, |
|---|
| 1531 | 1565 | st->dma_st.rx_buf, st->dma_st.rx_dma_buf); |
|---|
| 1532 | 1566 | dma_release_channel(st->dma_st.dma_chan); |
|---|
| 1533 | | - st->dma_st.dma_chan = 0; |
|---|
| 1567 | + st->dma_st.dma_chan = NULL; |
|---|
| 1534 | 1568 | |
|---|
| 1535 | 1569 | dev_info(&pdev->dev, "continuing without DMA support\n"); |
|---|
| 1536 | 1570 | } |
|---|
| .. | .. |
|---|
| 1538 | 1572 | static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val) |
|---|
| 1539 | 1573 | { |
|---|
| 1540 | 1574 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1575 | + int ret; |
|---|
| 1541 | 1576 | |
|---|
| 1542 | 1577 | if (val > AT91_HWFIFO_MAX_SIZE) |
|---|
| 1543 | 1578 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 1561 | 1596 | else if (val > 1) |
|---|
| 1562 | 1597 | at91_adc_dma_init(to_platform_device(&indio_dev->dev)); |
|---|
| 1563 | 1598 | |
|---|
| 1564 | | - return 0; |
|---|
| 1599 | + /* |
|---|
| 1600 | + * We can start the DMA only after setting the watermark and |
|---|
| 1601 | + * having the DMA initialization completed |
|---|
| 1602 | + */ |
|---|
| 1603 | + ret = at91_adc_buffer_prepare(indio_dev); |
|---|
| 1604 | + if (ret) |
|---|
| 1605 | + at91_adc_dma_disable(to_platform_device(&indio_dev->dev)); |
|---|
| 1606 | + |
|---|
| 1607 | + return ret; |
|---|
| 1565 | 1608 | } |
|---|
| 1566 | 1609 | |
|---|
| 1567 | 1610 | static int at91_adc_update_scan_mode(struct iio_dev *indio_dev, |
|---|
| .. | .. |
|---|
| 1582 | 1625 | return 0; |
|---|
| 1583 | 1626 | } |
|---|
| 1584 | 1627 | |
|---|
| 1585 | | -static void at91_adc_hw_init(struct at91_adc_state *st) |
|---|
| 1628 | +static void at91_adc_hw_init(struct iio_dev *indio_dev) |
|---|
| 1586 | 1629 | { |
|---|
| 1630 | + struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1631 | + |
|---|
| 1587 | 1632 | at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST); |
|---|
| 1588 | 1633 | at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff); |
|---|
| 1589 | 1634 | /* |
|---|
| .. | .. |
|---|
| 1593 | 1638 | at91_adc_writel(st, AT91_SAMA5D2_MR, |
|---|
| 1594 | 1639 | AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH); |
|---|
| 1595 | 1640 | |
|---|
| 1596 | | - at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate); |
|---|
| 1641 | + at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate); |
|---|
| 1597 | 1642 | |
|---|
| 1598 | 1643 | /* configure extended mode register */ |
|---|
| 1599 | 1644 | at91_adc_config_emr(st); |
|---|
| .. | .. |
|---|
| 1602 | 1647 | static ssize_t at91_adc_get_fifo_state(struct device *dev, |
|---|
| 1603 | 1648 | struct device_attribute *attr, char *buf) |
|---|
| 1604 | 1649 | { |
|---|
| 1605 | | - struct iio_dev *indio_dev = |
|---|
| 1606 | | - platform_get_drvdata(to_platform_device(dev)); |
|---|
| 1650 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
|---|
| 1607 | 1651 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1608 | 1652 | |
|---|
| 1609 | 1653 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!st->dma_st.dma_chan); |
|---|
| .. | .. |
|---|
| 1612 | 1656 | static ssize_t at91_adc_get_watermark(struct device *dev, |
|---|
| 1613 | 1657 | struct device_attribute *attr, char *buf) |
|---|
| 1614 | 1658 | { |
|---|
| 1615 | | - struct iio_dev *indio_dev = |
|---|
| 1616 | | - platform_get_drvdata(to_platform_device(dev)); |
|---|
| 1659 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
|---|
| 1617 | 1660 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1618 | 1661 | |
|---|
| 1619 | 1662 | return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark); |
|---|
| .. | .. |
|---|
| 1670 | 1713 | if (!indio_dev) |
|---|
| 1671 | 1714 | return -ENOMEM; |
|---|
| 1672 | 1715 | |
|---|
| 1673 | | - indio_dev->dev.parent = &pdev->dev; |
|---|
| 1674 | 1716 | indio_dev->name = dev_name(&pdev->dev); |
|---|
| 1675 | 1717 | indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; |
|---|
| 1676 | 1718 | indio_dev->info = &at91_adc_info; |
|---|
| .. | .. |
|---|
| 1678 | 1720 | indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels); |
|---|
| 1679 | 1721 | |
|---|
| 1680 | 1722 | st = iio_priv(indio_dev); |
|---|
| 1723 | + st->indio_dev = indio_dev; |
|---|
| 1681 | 1724 | |
|---|
| 1682 | 1725 | bitmap_set(&st->touch_st.channels_bitmask, |
|---|
| 1683 | 1726 | AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 1); |
|---|
| .. | .. |
|---|
| 1739 | 1782 | mutex_init(&st->lock); |
|---|
| 1740 | 1783 | INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler); |
|---|
| 1741 | 1784 | |
|---|
| 1742 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 1743 | | - if (!res) |
|---|
| 1744 | | - return -EINVAL; |
|---|
| 1785 | + st->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); |
|---|
| 1786 | + if (IS_ERR(st->base)) |
|---|
| 1787 | + return PTR_ERR(st->base); |
|---|
| 1745 | 1788 | |
|---|
| 1746 | 1789 | /* if we plan to use DMA, we need the physical address of the regs */ |
|---|
| 1747 | 1790 | st->dma_st.phys_addr = res->start; |
|---|
| 1748 | | - |
|---|
| 1749 | | - st->base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 1750 | | - if (IS_ERR(st->base)) |
|---|
| 1751 | | - return PTR_ERR(st->base); |
|---|
| 1752 | 1791 | |
|---|
| 1753 | 1792 | st->irq = platform_get_irq(pdev, 0); |
|---|
| 1754 | 1793 | if (st->irq <= 0) { |
|---|
| .. | .. |
|---|
| 1789 | 1828 | goto vref_disable; |
|---|
| 1790 | 1829 | } |
|---|
| 1791 | 1830 | |
|---|
| 1792 | | - at91_adc_hw_init(st); |
|---|
| 1831 | + at91_adc_hw_init(indio_dev); |
|---|
| 1793 | 1832 | |
|---|
| 1794 | 1833 | ret = clk_prepare_enable(st->per_clk); |
|---|
| 1795 | 1834 | if (ret) |
|---|
| .. | .. |
|---|
| 1865 | 1904 | |
|---|
| 1866 | 1905 | static __maybe_unused int at91_adc_suspend(struct device *dev) |
|---|
| 1867 | 1906 | { |
|---|
| 1868 | | - struct iio_dev *indio_dev = |
|---|
| 1869 | | - platform_get_drvdata(to_platform_device(dev)); |
|---|
| 1907 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
|---|
| 1870 | 1908 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1909 | + |
|---|
| 1910 | + if (iio_buffer_enabled(indio_dev)) |
|---|
| 1911 | + at91_adc_buffer_postdisable(indio_dev); |
|---|
| 1871 | 1912 | |
|---|
| 1872 | 1913 | /* |
|---|
| 1873 | 1914 | * Do a sofware reset of the ADC before we go to suspend. |
|---|
| .. | .. |
|---|
| 1886 | 1927 | |
|---|
| 1887 | 1928 | static __maybe_unused int at91_adc_resume(struct device *dev) |
|---|
| 1888 | 1929 | { |
|---|
| 1889 | | - struct iio_dev *indio_dev = |
|---|
| 1890 | | - platform_get_drvdata(to_platform_device(dev)); |
|---|
| 1930 | + struct iio_dev *indio_dev = dev_get_drvdata(dev); |
|---|
| 1891 | 1931 | struct at91_adc_state *st = iio_priv(indio_dev); |
|---|
| 1892 | 1932 | int ret; |
|---|
| 1893 | 1933 | |
|---|
| .. | .. |
|---|
| 1907 | 1947 | if (ret) |
|---|
| 1908 | 1948 | goto vref_disable_resume; |
|---|
| 1909 | 1949 | |
|---|
| 1910 | | - at91_adc_hw_init(st); |
|---|
| 1950 | + at91_adc_hw_init(indio_dev); |
|---|
| 1911 | 1951 | |
|---|
| 1912 | 1952 | /* reconfiguring trigger hardware state */ |
|---|
| 1913 | 1953 | if (!iio_buffer_enabled(indio_dev)) |
|---|
| 1914 | 1954 | return 0; |
|---|
| 1915 | 1955 | |
|---|
| 1916 | | - /* check if we are enabling triggered buffer or the touchscreen */ |
|---|
| 1917 | | - if (bitmap_subset(indio_dev->active_scan_mask, |
|---|
| 1918 | | - &st->touch_st.channels_bitmask, |
|---|
| 1919 | | - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { |
|---|
| 1920 | | - /* touchscreen enabling */ |
|---|
| 1921 | | - return at91_adc_configure_touch(st, true); |
|---|
| 1922 | | - } else { |
|---|
| 1923 | | - return at91_adc_configure_trigger(st->trig, true); |
|---|
| 1924 | | - } |
|---|
| 1956 | + ret = at91_adc_buffer_prepare(indio_dev); |
|---|
| 1957 | + if (ret) |
|---|
| 1958 | + goto vref_disable_resume; |
|---|
| 1925 | 1959 | |
|---|
| 1926 | | - /* not needed but more explicit */ |
|---|
| 1927 | | - return 0; |
|---|
| 1960 | + return at91_adc_configure_trigger(st->trig, true); |
|---|
| 1928 | 1961 | |
|---|
| 1929 | 1962 | vref_disable_resume: |
|---|
| 1930 | 1963 | regulator_disable(st->vref); |
|---|