Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Atmel ADC driver for SAMA5D2 devices and compatible. |
| 4 | * |
| 5 | * Copyright (C) 2015 Atmel, |
| 6 | * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> |
Eugen Hristev | 874b491 | 2021-09-01 15:30:11 +0300 | [diff] [blame] | 7 | * 2021 Microchip Technology, Inc. and its subsidiaries |
| 8 | * 2021 Eugen Hristev <eugen.hristev@microchip.com> |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/bitops.h> |
| 12 | #include <linux/clk.h> |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 13 | #include <linux/delay.h> |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/dmaengine.h> |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/wait.h> |
| 23 | #include <linux/iio/iio.h> |
| 24 | #include <linux/iio/sysfs.h> |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 25 | #include <linux/iio/buffer.h> |
| 26 | #include <linux/iio/trigger.h> |
| 27 | #include <linux/iio/trigger_consumer.h> |
| 28 | #include <linux/iio/triggered_buffer.h> |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 29 | #include <linux/pinctrl/consumer.h> |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 30 | #include <linux/regulator/consumer.h> |
| 31 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 32 | struct at91_adc_reg_layout { |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 33 | /* Control Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 34 | u16 CR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 35 | /* Software Reset */ |
| 36 | #define AT91_SAMA5D2_CR_SWRST BIT(0) |
| 37 | /* Start Conversion */ |
| 38 | #define AT91_SAMA5D2_CR_START BIT(1) |
| 39 | /* Touchscreen Calibration */ |
| 40 | #define AT91_SAMA5D2_CR_TSCALIB BIT(2) |
| 41 | /* Comparison Restart */ |
| 42 | #define AT91_SAMA5D2_CR_CMPRST BIT(4) |
| 43 | |
| 44 | /* Mode Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 45 | u16 MR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 46 | /* Trigger Selection */ |
| 47 | #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1) |
| 48 | /* ADTRG */ |
| 49 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0 |
| 50 | /* TIOA0 */ |
| 51 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1 |
| 52 | /* TIOA1 */ |
| 53 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2 |
| 54 | /* TIOA2 */ |
| 55 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3 |
| 56 | /* PWM event line 0 */ |
| 57 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4 |
| 58 | /* PWM event line 1 */ |
| 59 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5 |
| 60 | /* TIOA3 */ |
| 61 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6 |
| 62 | /* RTCOUT0 */ |
| 63 | #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7 |
| 64 | /* Sleep Mode */ |
| 65 | #define AT91_SAMA5D2_MR_SLEEP BIT(5) |
| 66 | /* Fast Wake Up */ |
| 67 | #define AT91_SAMA5D2_MR_FWUP BIT(6) |
| 68 | /* Prescaler Rate Selection */ |
| 69 | #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET) |
| 70 | #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8 |
| 71 | #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff |
Ludovic Desroches | 94b2423 | 2016-03-22 17:08:45 +0100 | [diff] [blame] | 72 | #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 73 | /* Startup Time */ |
| 74 | #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16) |
Ludovic Desroches | 94b2423 | 2016-03-22 17:08:45 +0100 | [diff] [blame] | 75 | #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 76 | /* Analog Change */ |
| 77 | #define AT91_SAMA5D2_MR_ANACH BIT(23) |
| 78 | /* Tracking Time */ |
| 79 | #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24) |
| 80 | #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff |
| 81 | /* Transfer Time */ |
| 82 | #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28) |
| 83 | #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3 |
| 84 | /* Use Sequence Enable */ |
| 85 | #define AT91_SAMA5D2_MR_USEQ BIT(31) |
| 86 | |
| 87 | /* Channel Sequence Register 1 */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 88 | u16 SEQR1; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 89 | /* Channel Sequence Register 2 */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 90 | u16 SEQR2; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 91 | /* Channel Enable Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 92 | u16 CHER; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 93 | /* Channel Disable Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 94 | u16 CHDR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 95 | /* Channel Status Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 96 | u16 CHSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 97 | /* Last Converted Data Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 98 | u16 LCDR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 99 | /* Interrupt Enable Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 100 | u16 IER; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 101 | /* Interrupt Enable Register - TS X measurement ready */ |
| 102 | #define AT91_SAMA5D2_IER_XRDY BIT(20) |
| 103 | /* Interrupt Enable Register - TS Y measurement ready */ |
| 104 | #define AT91_SAMA5D2_IER_YRDY BIT(21) |
| 105 | /* Interrupt Enable Register - TS pressure measurement ready */ |
| 106 | #define AT91_SAMA5D2_IER_PRDY BIT(22) |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 107 | /* Interrupt Enable Register - Data ready */ |
| 108 | #define AT91_SAMA5D2_IER_DRDY BIT(24) |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 109 | /* Interrupt Enable Register - general overrun error */ |
| 110 | #define AT91_SAMA5D2_IER_GOVRE BIT(25) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 111 | /* Interrupt Enable Register - Pen detect */ |
| 112 | #define AT91_SAMA5D2_IER_PEN BIT(29) |
| 113 | /* Interrupt Enable Register - No pen detect */ |
| 114 | #define AT91_SAMA5D2_IER_NOPEN BIT(30) |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 115 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 116 | /* Interrupt Disable Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 117 | u16 IDR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 118 | /* Interrupt Mask Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 119 | u16 IMR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 120 | /* Interrupt Status Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 121 | u16 ISR; |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 122 | /* End of Conversion Interrupt Enable Register */ |
| 123 | u16 EOC_IER; |
| 124 | /* End of Conversion Interrupt Disable Register */ |
| 125 | u16 EOC_IDR; |
| 126 | /* End of Conversion Interrupt Mask Register */ |
| 127 | u16 EOC_IMR; |
| 128 | /* End of Conversion Interrupt Status Register */ |
| 129 | u16 EOC_ISR; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 130 | /* Interrupt Status Register - Pen touching sense status */ |
| 131 | #define AT91_SAMA5D2_ISR_PENS BIT(31) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 132 | /* Last Channel Trigger Mode Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 133 | u16 LCTMR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 134 | /* Last Channel Compare Window Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 135 | u16 LCCWR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 136 | /* Overrun Status Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 137 | u16 OVER; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 138 | /* Extended Mode Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 139 | u16 EMR; |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 140 | /* Extended Mode Register - Oversampling rate */ |
| 141 | #define AT91_SAMA5D2_EMR_OSR(V) ((V) << 16) |
| 142 | #define AT91_SAMA5D2_EMR_OSR_MASK GENMASK(17, 16) |
| 143 | #define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0 |
| 144 | #define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1 |
| 145 | #define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2 |
| 146 | |
| 147 | /* Extended Mode Register - Averaging on single trigger event */ |
| 148 | #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20) |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 149 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 150 | /* Compare Window Register */ |
| 151 | u16 CWR; |
| 152 | /* Channel Gain Register */ |
| 153 | u16 CGR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 154 | /* Channel Offset Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 155 | u16 COR; |
Eugen Hristev | d8004c5 | 2021-09-01 15:30:09 +0300 | [diff] [blame] | 156 | /* Channel Offset Register differential offset - constant, not a register */ |
| 157 | u16 COR_diff_offset; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 158 | /* Analog Control Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 159 | u16 ACR; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 160 | /* Analog Control Register - Pen detect sensitivity mask */ |
| 161 | #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0) |
| 162 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 163 | /* Touchscreen Mode Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 164 | u16 TSMR; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 165 | /* Touchscreen Mode Register - No touch mode */ |
| 166 | #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0 |
| 167 | /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */ |
| 168 | #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1 |
| 169 | /* Touchscreen Mode Register - 4 wire screen, pressure measurement */ |
| 170 | #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS 2 |
| 171 | /* Touchscreen Mode Register - 5 wire screen */ |
| 172 | #define AT91_SAMA5D2_TSMR_TSMODE_5WIRE 3 |
| 173 | /* Touchscreen Mode Register - Average samples mask */ |
| 174 | #define AT91_SAMA5D2_TSMR_TSAV_MASK GENMASK(5, 4) |
| 175 | /* Touchscreen Mode Register - Average samples */ |
| 176 | #define AT91_SAMA5D2_TSMR_TSAV(x) ((x) << 4) |
| 177 | /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */ |
| 178 | #define AT91_SAMA5D2_TSMR_TSFREQ_MASK GENMASK(11, 8) |
| 179 | /* Touchscreen Mode Register - Touch/trigger frequency ratio */ |
| 180 | #define AT91_SAMA5D2_TSMR_TSFREQ(x) ((x) << 8) |
| 181 | /* Touchscreen Mode Register - Pen Debounce Time mask */ |
| 182 | #define AT91_SAMA5D2_TSMR_PENDBC_MASK GENMASK(31, 28) |
| 183 | /* Touchscreen Mode Register - Pen Debounce Time */ |
| 184 | #define AT91_SAMA5D2_TSMR_PENDBC(x) ((x) << 28) |
| 185 | /* Touchscreen Mode Register - No DMA for touch measurements */ |
| 186 | #define AT91_SAMA5D2_TSMR_NOTSDMA BIT(22) |
| 187 | /* Touchscreen Mode Register - Disable pen detection */ |
| 188 | #define AT91_SAMA5D2_TSMR_PENDET_DIS (0 << 24) |
| 189 | /* Touchscreen Mode Register - Enable pen detection */ |
| 190 | #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24) |
| 191 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 192 | /* Touchscreen X Position Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 193 | u16 XPOSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 194 | /* Touchscreen Y Position Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 195 | u16 YPOSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 196 | /* Touchscreen Pressure Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 197 | u16 PRESSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 198 | /* Trigger Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 199 | u16 TRGR; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 200 | /* Mask for TRGMOD field of TRGR register */ |
| 201 | #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0) |
| 202 | /* No trigger, only software trigger can start conversions */ |
| 203 | #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0 |
| 204 | /* Trigger Mode external trigger rising edge */ |
| 205 | #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1 |
| 206 | /* Trigger Mode external trigger falling edge */ |
| 207 | #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2 |
| 208 | /* Trigger Mode external trigger any edge */ |
| 209 | #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3 |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 210 | /* Trigger Mode internal periodic */ |
| 211 | #define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5 |
| 212 | /* Trigger Mode - trigger period mask */ |
| 213 | #define AT91_SAMA5D2_TRGR_TRGPER_MASK GENMASK(31, 16) |
| 214 | /* Trigger Mode - trigger period */ |
| 215 | #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16) |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 216 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 217 | /* Correction Select Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 218 | u16 COSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 219 | /* Correction Value Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 220 | u16 CVR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 221 | /* Channel Error Correction Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 222 | u16 CECR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 223 | /* Write Protection Mode Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 224 | u16 WPMR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 225 | /* Write Protection Status Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 226 | u16 WPSR; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 227 | /* Version Register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 228 | u16 VERSION; |
| 229 | }; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 230 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 231 | static const struct at91_adc_reg_layout sama5d2_layout = { |
| 232 | .CR = 0x00, |
| 233 | .MR = 0x04, |
| 234 | .SEQR1 = 0x08, |
| 235 | .SEQR2 = 0x0c, |
| 236 | .CHER = 0x10, |
| 237 | .CHDR = 0x14, |
| 238 | .CHSR = 0x18, |
| 239 | .LCDR = 0x20, |
| 240 | .IER = 0x24, |
| 241 | .IDR = 0x28, |
| 242 | .IMR = 0x2c, |
| 243 | .ISR = 0x30, |
| 244 | .LCTMR = 0x34, |
| 245 | .LCCWR = 0x38, |
| 246 | .OVER = 0x3c, |
| 247 | .EMR = 0x40, |
| 248 | .CWR = 0x44, |
| 249 | .CGR = 0x48, |
| 250 | .COR = 0x4c, |
Eugen Hristev | d8004c5 | 2021-09-01 15:30:09 +0300 | [diff] [blame] | 251 | .COR_diff_offset = 16, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 252 | .ACR = 0x94, |
| 253 | .TSMR = 0xb0, |
| 254 | .XPOSR = 0xb4, |
| 255 | .YPOSR = 0xb8, |
| 256 | .PRESSR = 0xbc, |
| 257 | .TRGR = 0xc0, |
| 258 | .COSR = 0xd0, |
| 259 | .CVR = 0xd4, |
| 260 | .CECR = 0xd8, |
| 261 | .WPMR = 0xe4, |
| 262 | .WPSR = 0xe8, |
| 263 | .VERSION = 0xfc, |
| 264 | }; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 265 | |
Eugen Hristev | 840bf6c | 2021-09-01 15:30:10 +0300 | [diff] [blame] | 266 | static const struct at91_adc_reg_layout sama7g5_layout = { |
| 267 | .CR = 0x00, |
| 268 | .MR = 0x04, |
| 269 | .SEQR1 = 0x08, |
| 270 | .SEQR2 = 0x0c, |
| 271 | .CHER = 0x10, |
| 272 | .CHDR = 0x14, |
| 273 | .CHSR = 0x18, |
| 274 | .LCDR = 0x20, |
| 275 | .IER = 0x24, |
| 276 | .IDR = 0x28, |
| 277 | .IMR = 0x2c, |
| 278 | .ISR = 0x30, |
| 279 | .EOC_IER = 0x34, |
| 280 | .EOC_IDR = 0x38, |
| 281 | .EOC_IMR = 0x3c, |
| 282 | .EOC_ISR = 0x40, |
| 283 | .OVER = 0x4c, |
| 284 | .EMR = 0x50, |
| 285 | .CWR = 0x54, |
| 286 | .COR = 0x5c, |
| 287 | .COR_diff_offset = 0, |
| 288 | .ACR = 0xe0, |
| 289 | .TRGR = 0x100, |
| 290 | .COSR = 0x104, |
| 291 | .CVR = 0x108, |
| 292 | .CECR = 0x10c, |
| 293 | .WPMR = 0x118, |
| 294 | .WPSR = 0x11c, |
| 295 | .VERSION = 0x130, |
| 296 | }; |
| 297 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 298 | #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */ |
| 299 | #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200 |
| 300 | |
| 301 | #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0) |
| 302 | |
| 303 | #define AT91_SAMA5D2_MAX_POS_BITS 12 |
| 304 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 305 | #define AT91_HWFIFO_MAX_SIZE_STR "128" |
| 306 | #define AT91_HWFIFO_MAX_SIZE 128 |
| 307 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 308 | /* Possible values for oversampling ratio */ |
| 309 | #define AT91_OSR_1SAMPLES 1 |
| 310 | #define AT91_OSR_4SAMPLES 4 |
| 311 | #define AT91_OSR_16SAMPLES 16 |
| 312 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 313 | #define AT91_SAMA5D2_CHAN_SINGLE(index, num, addr) \ |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 314 | { \ |
| 315 | .type = IIO_VOLTAGE, \ |
| 316 | .channel = num, \ |
| 317 | .address = addr, \ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 318 | .scan_index = index, \ |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 319 | .scan_type = { \ |
| 320 | .sign = 'u', \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 321 | .realbits = 14, \ |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 322 | .storagebits = 16, \ |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 323 | }, \ |
| 324 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 325 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 326 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\ |
| 327 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 328 | .datasheet_name = "CH"#num, \ |
| 329 | .indexed = 1, \ |
| 330 | } |
| 331 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 332 | #define AT91_SAMA5D2_CHAN_DIFF(index, num, num2, addr) \ |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 333 | { \ |
| 334 | .type = IIO_VOLTAGE, \ |
| 335 | .differential = 1, \ |
| 336 | .channel = num, \ |
| 337 | .channel2 = num2, \ |
| 338 | .address = addr, \ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 339 | .scan_index = index, \ |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 340 | .scan_type = { \ |
| 341 | .sign = 's', \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 342 | .realbits = 14, \ |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 343 | .storagebits = 16, \ |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 344 | }, \ |
| 345 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 346 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 347 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\ |
| 348 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 349 | .datasheet_name = "CH"#num"-CH"#num2, \ |
| 350 | .indexed = 1, \ |
| 351 | } |
| 352 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 353 | #define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod) \ |
| 354 | { \ |
| 355 | .type = IIO_POSITIONRELATIVE, \ |
| 356 | .modified = 1, \ |
| 357 | .channel = num, \ |
| 358 | .channel2 = mod, \ |
| 359 | .scan_index = num, \ |
| 360 | .scan_type = { \ |
| 361 | .sign = 'u', \ |
| 362 | .realbits = 12, \ |
| 363 | .storagebits = 16, \ |
| 364 | }, \ |
| 365 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 366 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\ |
| 367 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 368 | .datasheet_name = name, \ |
| 369 | } |
| 370 | #define AT91_SAMA5D2_CHAN_PRESSURE(num, name) \ |
| 371 | { \ |
| 372 | .type = IIO_PRESSURE, \ |
| 373 | .channel = num, \ |
| 374 | .scan_index = num, \ |
| 375 | .scan_type = { \ |
| 376 | .sign = 'u', \ |
| 377 | .realbits = 12, \ |
| 378 | .storagebits = 16, \ |
| 379 | }, \ |
| 380 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 381 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\ |
| 382 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 383 | .datasheet_name = name, \ |
| 384 | } |
| 385 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 386 | #define at91_adc_readl(st, reg) \ |
| 387 | readl_relaxed((st)->base + (st)->soc_info.platform->layout->reg) |
| 388 | #define at91_adc_read_chan(st, reg) \ |
| 389 | readl_relaxed((st)->base + reg) |
| 390 | #define at91_adc_writel(st, reg, val) \ |
| 391 | writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 392 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 393 | /** |
| 394 | * struct at91_adc_platform - at91-sama5d2 platform information struct |
| 395 | * @layout: pointer to the reg layout struct |
| 396 | * @adc_channels: pointer to an array of channels for registering in |
| 397 | * the iio subsystem |
| 398 | * @nr_channels: number of physical channels available |
| 399 | * @touch_chan_x: index of the touchscreen X channel |
| 400 | * @touch_chan_y: index of the touchscreen Y channel |
| 401 | * @touch_chan_p: index of the touchscreen P channel |
| 402 | * @max_channels: number of total channels |
| 403 | * @max_index: highest channel index (highest index may be higher |
| 404 | * than the total channel number) |
| 405 | * @hw_trig_cnt: number of possible hardware triggers |
| 406 | */ |
| 407 | struct at91_adc_platform { |
| 408 | const struct at91_adc_reg_layout *layout; |
| 409 | const struct iio_chan_spec (*adc_channels)[]; |
| 410 | unsigned int nr_channels; |
| 411 | unsigned int touch_chan_x; |
| 412 | unsigned int touch_chan_y; |
| 413 | unsigned int touch_chan_p; |
| 414 | unsigned int max_channels; |
| 415 | unsigned int max_index; |
| 416 | unsigned int hw_trig_cnt; |
| 417 | }; |
| 418 | |
| 419 | /** |
| 420 | * struct at91_adc_soc_info - at91-sama5d2 soc information struct |
| 421 | * @startup_time: device startup time |
| 422 | * @min_sample_rate: minimum sample rate in Hz |
| 423 | * @max_sample_rate: maximum sample rate in Hz |
| 424 | * @platform: pointer to the platform structure |
| 425 | */ |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 426 | struct at91_adc_soc_info { |
| 427 | unsigned startup_time; |
| 428 | unsigned min_sample_rate; |
| 429 | unsigned max_sample_rate; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 430 | const struct at91_adc_platform *platform; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 431 | }; |
| 432 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 433 | struct at91_adc_trigger { |
| 434 | char *name; |
| 435 | unsigned int trgmod_value; |
| 436 | unsigned int edge_type; |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 437 | bool hw_trig; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 438 | }; |
| 439 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 440 | /** |
Lee Jones | 62eebcb | 2020-07-17 17:55:22 +0100 | [diff] [blame] | 441 | * struct at91_adc_dma - at91-sama5d2 dma information struct |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 442 | * @dma_chan: the dma channel acquired |
| 443 | * @rx_buf: dma coherent allocated area |
| 444 | * @rx_dma_buf: dma handler for the buffer |
| 445 | * @phys_addr: physical address of the ADC base register |
| 446 | * @buf_idx: index inside the dma buffer where reading was last done |
| 447 | * @rx_buf_sz: size of buffer used by DMA operation |
| 448 | * @watermark: number of conversions to copy before DMA triggers irq |
| 449 | * @dma_ts: hold the start timestamp of dma operation |
| 450 | */ |
| 451 | struct at91_adc_dma { |
| 452 | struct dma_chan *dma_chan; |
| 453 | u8 *rx_buf; |
| 454 | dma_addr_t rx_dma_buf; |
| 455 | phys_addr_t phys_addr; |
| 456 | int buf_idx; |
| 457 | int rx_buf_sz; |
| 458 | int watermark; |
| 459 | s64 dma_ts; |
| 460 | }; |
| 461 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 462 | /** |
Lee Jones | 62eebcb | 2020-07-17 17:55:22 +0100 | [diff] [blame] | 463 | * struct at91_adc_touch - at91-sama5d2 touchscreen information struct |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 464 | * @sample_period_val: the value for periodic trigger interval |
| 465 | * @touching: is the pen touching the screen or not |
| 466 | * @x_pos: temporary placeholder for pressure computation |
| 467 | * @channels_bitmask: bitmask with the touchscreen channels enabled |
| 468 | * @workq: workqueue for buffer data pushing |
| 469 | */ |
| 470 | struct at91_adc_touch { |
| 471 | u16 sample_period_val; |
| 472 | bool touching; |
| 473 | u16 x_pos; |
| 474 | unsigned long channels_bitmask; |
| 475 | struct work_struct workq; |
| 476 | }; |
| 477 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 478 | /* |
| 479 | * Buffer size requirements: |
| 480 | * No channels * bytes_per_channel(2) + timestamp bytes (8) |
| 481 | * Divided by 2 because we need half words. |
| 482 | * We assume 32 channels for now, has to be increased if needed. |
| 483 | * Nobody minds a buffer being too big. |
| 484 | */ |
| 485 | #define AT91_BUFFER_MAX_HWORDS ((32 * 2 + 8) / 2) |
| 486 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 487 | struct at91_adc_state { |
| 488 | void __iomem *base; |
| 489 | int irq; |
| 490 | struct clk *per_clk; |
| 491 | struct regulator *reg; |
| 492 | struct regulator *vref; |
Ludovic Desroches | d7bdcc3 | 2016-01-18 09:41:55 +0100 | [diff] [blame] | 493 | int vref_uv; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 494 | unsigned int current_sample_rate; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 495 | struct iio_trigger *trig; |
| 496 | const struct at91_adc_trigger *selected_trig; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 497 | const struct iio_chan_spec *chan; |
| 498 | bool conversion_done; |
| 499 | u32 conversion_value; |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 500 | unsigned int oversampling_ratio; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 501 | struct at91_adc_soc_info soc_info; |
| 502 | wait_queue_head_t wq_data_available; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 503 | struct at91_adc_dma dma_st; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 504 | struct at91_adc_touch touch_st; |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 505 | struct iio_dev *indio_dev; |
Jonathan Cameron | 8f88475 | 2021-06-13 16:22:54 +0100 | [diff] [blame] | 506 | /* Ensure naturally aligned timestamp */ |
| 507 | u16 buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 508 | /* |
| 509 | * lock to prevent concurrent 'single conversion' requests through |
| 510 | * sysfs. |
| 511 | */ |
| 512 | struct mutex lock; |
| 513 | }; |
| 514 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 515 | static const struct at91_adc_trigger at91_adc_trigger_list[] = { |
| 516 | { |
| 517 | .name = "external_rising", |
| 518 | .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE, |
| 519 | .edge_type = IRQ_TYPE_EDGE_RISING, |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 520 | .hw_trig = true, |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 521 | }, |
| 522 | { |
| 523 | .name = "external_falling", |
| 524 | .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL, |
| 525 | .edge_type = IRQ_TYPE_EDGE_FALLING, |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 526 | .hw_trig = true, |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 527 | }, |
| 528 | { |
| 529 | .name = "external_any", |
| 530 | .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY, |
| 531 | .edge_type = IRQ_TYPE_EDGE_BOTH, |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 532 | .hw_trig = true, |
| 533 | }, |
| 534 | { |
| 535 | .name = "software", |
| 536 | .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER, |
| 537 | .edge_type = IRQ_TYPE_NONE, |
| 538 | .hw_trig = false, |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 539 | }, |
| 540 | }; |
| 541 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 542 | static const struct iio_chan_spec at91_sama5d2_adc_channels[] = { |
| 543 | AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x50), |
| 544 | AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x54), |
| 545 | AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x58), |
| 546 | AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x5c), |
| 547 | AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x60), |
| 548 | AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x64), |
| 549 | AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x68), |
| 550 | AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x6c), |
| 551 | AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x70), |
| 552 | AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x74), |
| 553 | AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x78), |
| 554 | AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x7c), |
| 555 | /* original ABI has the differential channels with a gap in between */ |
| 556 | AT91_SAMA5D2_CHAN_DIFF(12, 0, 1, 0x50), |
| 557 | AT91_SAMA5D2_CHAN_DIFF(14, 2, 3, 0x58), |
| 558 | AT91_SAMA5D2_CHAN_DIFF(16, 4, 5, 0x60), |
| 559 | AT91_SAMA5D2_CHAN_DIFF(18, 6, 7, 0x68), |
| 560 | AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x70), |
| 561 | AT91_SAMA5D2_CHAN_DIFF(22, 10, 11, 0x78), |
| 562 | IIO_CHAN_SOFT_TIMESTAMP(23), |
| 563 | AT91_SAMA5D2_CHAN_TOUCH(24, "x", IIO_MOD_X), |
| 564 | AT91_SAMA5D2_CHAN_TOUCH(25, "y", IIO_MOD_Y), |
| 565 | AT91_SAMA5D2_CHAN_PRESSURE(26, "pressure"), |
| 566 | }; |
| 567 | |
Eugen Hristev | 840bf6c | 2021-09-01 15:30:10 +0300 | [diff] [blame] | 568 | static const struct iio_chan_spec at91_sama7g5_adc_channels[] = { |
| 569 | AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x60), |
| 570 | AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x64), |
| 571 | AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x68), |
| 572 | AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x6c), |
| 573 | AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x70), |
| 574 | AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x74), |
| 575 | AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x78), |
| 576 | AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x7c), |
| 577 | AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x80), |
| 578 | AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x84), |
| 579 | AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x88), |
| 580 | AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x8c), |
| 581 | AT91_SAMA5D2_CHAN_SINGLE(12, 12, 0x90), |
| 582 | AT91_SAMA5D2_CHAN_SINGLE(13, 13, 0x94), |
| 583 | AT91_SAMA5D2_CHAN_SINGLE(14, 14, 0x98), |
| 584 | AT91_SAMA5D2_CHAN_SINGLE(15, 15, 0x9c), |
| 585 | AT91_SAMA5D2_CHAN_DIFF(16, 0, 1, 0x60), |
| 586 | AT91_SAMA5D2_CHAN_DIFF(17, 2, 3, 0x68), |
| 587 | AT91_SAMA5D2_CHAN_DIFF(18, 4, 5, 0x70), |
| 588 | AT91_SAMA5D2_CHAN_DIFF(19, 6, 7, 0x78), |
| 589 | AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x80), |
| 590 | AT91_SAMA5D2_CHAN_DIFF(21, 10, 11, 0x88), |
| 591 | AT91_SAMA5D2_CHAN_DIFF(22, 12, 13, 0x90), |
| 592 | AT91_SAMA5D2_CHAN_DIFF(23, 14, 15, 0x98), |
| 593 | IIO_CHAN_SOFT_TIMESTAMP(24), |
| 594 | }; |
| 595 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 596 | static const struct at91_adc_platform sama5d2_platform = { |
| 597 | .layout = &sama5d2_layout, |
| 598 | .adc_channels = &at91_sama5d2_adc_channels, |
| 599 | #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12 |
| 600 | #define AT91_SAMA5D2_DIFF_CHAN_CNT 6 |
| 601 | .nr_channels = AT91_SAMA5D2_SINGLE_CHAN_CNT + |
| 602 | AT91_SAMA5D2_DIFF_CHAN_CNT, |
| 603 | #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \ |
| 604 | AT91_SAMA5D2_DIFF_CHAN_CNT * 2) |
| 605 | .touch_chan_x = AT91_SAMA5D2_TOUCH_X_CHAN_IDX, |
| 606 | #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1) |
| 607 | .touch_chan_y = AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, |
| 608 | #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1) |
| 609 | .touch_chan_p = AT91_SAMA5D2_TOUCH_P_CHAN_IDX, |
| 610 | #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX |
| 611 | .max_channels = ARRAY_SIZE(at91_sama5d2_adc_channels), |
| 612 | .max_index = AT91_SAMA5D2_MAX_CHAN_IDX, |
| 613 | #define AT91_SAMA5D2_HW_TRIG_CNT 3 |
| 614 | .hw_trig_cnt = AT91_SAMA5D2_HW_TRIG_CNT, |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 615 | }; |
| 616 | |
Eugen Hristev | 840bf6c | 2021-09-01 15:30:10 +0300 | [diff] [blame] | 617 | static const struct at91_adc_platform sama7g5_platform = { |
| 618 | .layout = &sama7g5_layout, |
| 619 | .adc_channels = &at91_sama7g5_adc_channels, |
| 620 | #define AT91_SAMA7G5_SINGLE_CHAN_CNT 16 |
| 621 | #define AT91_SAMA7G5_DIFF_CHAN_CNT 8 |
| 622 | .nr_channels = AT91_SAMA7G5_SINGLE_CHAN_CNT + |
| 623 | AT91_SAMA7G5_DIFF_CHAN_CNT, |
| 624 | #define AT91_SAMA7G5_MAX_CHAN_IDX (AT91_SAMA7G5_SINGLE_CHAN_CNT + \ |
| 625 | AT91_SAMA7G5_DIFF_CHAN_CNT) |
| 626 | .max_channels = ARRAY_SIZE(at91_sama7g5_adc_channels), |
| 627 | .max_index = AT91_SAMA7G5_MAX_CHAN_IDX, |
| 628 | #define AT91_SAMA7G5_HW_TRIG_CNT 3 |
| 629 | .hw_trig_cnt = AT91_SAMA7G5_HW_TRIG_CNT, |
| 630 | }; |
| 631 | |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 632 | static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan) |
| 633 | { |
| 634 | int i; |
| 635 | |
| 636 | for (i = 0; i < indio_dev->num_channels; i++) { |
| 637 | if (indio_dev->channels[i].scan_index == chan) |
| 638 | return i; |
| 639 | } |
| 640 | return -EINVAL; |
| 641 | } |
| 642 | |
| 643 | static inline struct iio_chan_spec const * |
| 644 | at91_adc_chan_get(struct iio_dev *indio_dev, int chan) |
| 645 | { |
| 646 | int index = at91_adc_chan_xlate(indio_dev, chan); |
| 647 | |
| 648 | if (index < 0) |
| 649 | return NULL; |
| 650 | return indio_dev->channels + index; |
| 651 | } |
| 652 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 653 | static inline int at91_adc_of_xlate(struct iio_dev *indio_dev, |
| 654 | const struct of_phandle_args *iiospec) |
| 655 | { |
| 656 | return at91_adc_chan_xlate(indio_dev, iiospec->args[0]); |
| 657 | } |
| 658 | |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 659 | static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev *indio_dev) |
| 660 | { |
| 661 | u32 mask = 0; |
| 662 | u8 bit; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 663 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 664 | |
| 665 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 666 | indio_dev->num_channels) { |
| 667 | struct iio_chan_spec const *chan = |
| 668 | at91_adc_chan_get(indio_dev, bit); |
| 669 | mask |= BIT(chan->channel); |
| 670 | } |
| 671 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 672 | return mask & GENMASK(st->soc_info.platform->nr_channels, 0); |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Eugen Hristev | d8004c5 | 2021-09-01 15:30:09 +0300 | [diff] [blame] | 675 | static void at91_adc_cor(struct at91_adc_state *st, |
| 676 | struct iio_chan_spec const *chan) |
| 677 | { |
| 678 | u32 cor, cur_cor; |
| 679 | |
| 680 | cor = BIT(chan->channel) | BIT(chan->channel2); |
| 681 | |
| 682 | cur_cor = at91_adc_readl(st, COR); |
| 683 | cor <<= st->soc_info.platform->layout->COR_diff_offset; |
| 684 | if (chan->differential) |
| 685 | at91_adc_writel(st, COR, cur_cor | cor); |
| 686 | else |
| 687 | at91_adc_writel(st, COR, cur_cor & ~cor); |
| 688 | } |
| 689 | |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 690 | static void at91_adc_irq_status(struct at91_adc_state *st, u32 *status, |
| 691 | u32 *eoc) |
| 692 | { |
| 693 | *status = at91_adc_readl(st, ISR); |
| 694 | if (st->soc_info.platform->layout->EOC_ISR) |
| 695 | *eoc = at91_adc_readl(st, EOC_ISR); |
| 696 | else |
| 697 | *eoc = *status; |
| 698 | } |
| 699 | |
| 700 | static void at91_adc_irq_mask(struct at91_adc_state *st, u32 *status, u32 *eoc) |
| 701 | { |
| 702 | *status = at91_adc_readl(st, IMR); |
| 703 | if (st->soc_info.platform->layout->EOC_IMR) |
| 704 | *eoc = at91_adc_readl(st, EOC_IMR); |
| 705 | else |
| 706 | *eoc = *status; |
| 707 | } |
| 708 | |
| 709 | static void at91_adc_eoc_dis(struct at91_adc_state *st, unsigned int channel) |
| 710 | { |
| 711 | /* |
| 712 | * On some products having the EOC bits in a separate register, |
| 713 | * errata recommends not writing this register (EOC_IDR). |
| 714 | * On products having the EOC bits in the IDR register, it's fine to write it. |
| 715 | */ |
| 716 | if (!st->soc_info.platform->layout->EOC_IDR) |
| 717 | at91_adc_writel(st, IDR, BIT(channel)); |
| 718 | } |
| 719 | |
| 720 | static void at91_adc_eoc_ena(struct at91_adc_state *st, unsigned int channel) |
| 721 | { |
| 722 | if (!st->soc_info.platform->layout->EOC_IDR) |
| 723 | at91_adc_writel(st, IER, BIT(channel)); |
| 724 | else |
| 725 | at91_adc_writel(st, EOC_IER, BIT(channel)); |
| 726 | } |
| 727 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 728 | static void at91_adc_config_emr(struct at91_adc_state *st) |
| 729 | { |
| 730 | /* configure the extended mode register */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 731 | unsigned int emr = at91_adc_readl(st, EMR); |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 732 | |
| 733 | /* select oversampling per single trigger event */ |
| 734 | emr |= AT91_SAMA5D2_EMR_ASTE(1); |
| 735 | |
| 736 | /* delete leftover content if it's the case */ |
| 737 | emr &= ~AT91_SAMA5D2_EMR_OSR_MASK; |
| 738 | |
| 739 | /* select oversampling ratio from configuration */ |
| 740 | switch (st->oversampling_ratio) { |
| 741 | case AT91_OSR_1SAMPLES: |
| 742 | emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_1SAMPLES) & |
| 743 | AT91_SAMA5D2_EMR_OSR_MASK; |
| 744 | break; |
| 745 | case AT91_OSR_4SAMPLES: |
| 746 | emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_4SAMPLES) & |
| 747 | AT91_SAMA5D2_EMR_OSR_MASK; |
| 748 | break; |
| 749 | case AT91_OSR_16SAMPLES: |
| 750 | emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_16SAMPLES) & |
| 751 | AT91_SAMA5D2_EMR_OSR_MASK; |
| 752 | break; |
| 753 | } |
| 754 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 755 | at91_adc_writel(st, EMR, emr); |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val) |
| 759 | { |
| 760 | if (st->oversampling_ratio == AT91_OSR_1SAMPLES) { |
| 761 | /* |
| 762 | * in this case we only have 12 bits of real data, but channel |
| 763 | * is registered as 14 bits, so shift left two bits |
| 764 | */ |
| 765 | *val <<= 2; |
| 766 | } else if (st->oversampling_ratio == AT91_OSR_4SAMPLES) { |
| 767 | /* |
| 768 | * in this case we have 13 bits of real data, but channel |
| 769 | * is registered as 14 bits, so left shift one bit |
| 770 | */ |
| 771 | *val <<= 1; |
| 772 | } |
| 773 | |
| 774 | return IIO_VAL_INT; |
| 775 | } |
| 776 | |
| 777 | static void at91_adc_adjust_val_osr_array(struct at91_adc_state *st, void *buf, |
| 778 | int len) |
| 779 | { |
| 780 | int i = 0, val; |
| 781 | u16 *buf_u16 = (u16 *) buf; |
| 782 | |
| 783 | /* |
| 784 | * We are converting each two bytes (each sample). |
| 785 | * First convert the byte based array to u16, and convert each sample |
| 786 | * separately. |
| 787 | * Each value is two bytes in an array of chars, so to not shift |
| 788 | * more than we need, save the value separately. |
| 789 | * len is in bytes, so divide by two to get number of samples. |
| 790 | */ |
| 791 | while (i < len / 2) { |
| 792 | val = buf_u16[i]; |
| 793 | at91_adc_adjust_val_osr(st, &val); |
| 794 | buf_u16[i] = val; |
| 795 | i++; |
| 796 | } |
| 797 | } |
| 798 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 799 | static int at91_adc_configure_touch(struct at91_adc_state *st, bool state) |
| 800 | { |
| 801 | u32 clk_khz = st->current_sample_rate / 1000; |
| 802 | int i = 0; |
| 803 | u16 pendbc; |
| 804 | u32 tsmr, acr; |
| 805 | |
| 806 | if (!state) { |
| 807 | /* disabling touch IRQs and setting mode to no touch enabled */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 808 | at91_adc_writel(st, IDR, |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 809 | AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 810 | at91_adc_writel(st, TSMR, 0); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | /* |
| 814 | * debounce time is in microseconds, we need it in milliseconds to |
| 815 | * multiply with kilohertz, so, divide by 1000, but after the multiply. |
| 816 | * round up to make sure pendbc is at least 1 |
| 817 | */ |
| 818 | pendbc = round_up(AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US * |
| 819 | clk_khz / 1000, 1); |
| 820 | |
| 821 | /* get the required exponent */ |
| 822 | while (pendbc >> i++) |
| 823 | ; |
| 824 | |
| 825 | pendbc = i; |
| 826 | |
| 827 | tsmr = AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS; |
| 828 | |
| 829 | tsmr |= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK; |
| 830 | tsmr |= AT91_SAMA5D2_TSMR_PENDBC(pendbc) & |
| 831 | AT91_SAMA5D2_TSMR_PENDBC_MASK; |
| 832 | tsmr |= AT91_SAMA5D2_TSMR_NOTSDMA; |
| 833 | tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA; |
| 834 | tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK; |
| 835 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 836 | at91_adc_writel(st, TSMR, tsmr); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 837 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 838 | acr = at91_adc_readl(st, ACR); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 839 | acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK; |
| 840 | acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 841 | at91_adc_writel(st, ACR, acr); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 842 | |
| 843 | /* Sample Period Time = (TRGPER + 1) / ADCClock */ |
| 844 | st->touch_st.sample_period_val = |
| 845 | round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US * |
| 846 | clk_khz / 1000) - 1, 1); |
| 847 | /* enable pen detect IRQ */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 848 | at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg) |
| 854 | { |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 855 | u32 val = 0; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 856 | u32 scale, result, pos; |
| 857 | |
| 858 | /* |
| 859 | * to obtain the actual position we must divide by scale |
| 860 | * and multiply with max, where |
| 861 | * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1 |
| 862 | */ |
| 863 | /* first half of register is the x or y, second half is the scale */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 864 | if (reg == st->soc_info.platform->layout->XPOSR) |
| 865 | val = at91_adc_readl(st, XPOSR); |
| 866 | else if (reg == st->soc_info.platform->layout->YPOSR) |
| 867 | val = at91_adc_readl(st, YPOSR); |
| 868 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 869 | if (!val) |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 870 | dev_dbg(&st->indio_dev->dev, "pos is 0\n"); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 871 | |
| 872 | pos = val & AT91_SAMA5D2_XYZ_MASK; |
| 873 | result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos; |
| 874 | scale = (val >> 16) & AT91_SAMA5D2_XYZ_MASK; |
| 875 | if (scale == 0) { |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 876 | dev_err(&st->indio_dev->dev, "scale is 0\n"); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | result /= scale; |
| 880 | |
| 881 | return result; |
| 882 | } |
| 883 | |
| 884 | static u16 at91_adc_touch_x_pos(struct at91_adc_state *st) |
| 885 | { |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 886 | st->touch_st.x_pos = at91_adc_touch_pos(st, st->soc_info.platform->layout->XPOSR); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 887 | return st->touch_st.x_pos; |
| 888 | } |
| 889 | |
| 890 | static u16 at91_adc_touch_y_pos(struct at91_adc_state *st) |
| 891 | { |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 892 | return at91_adc_touch_pos(st, st->soc_info.platform->layout->YPOSR); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static u16 at91_adc_touch_pressure(struct at91_adc_state *st) |
| 896 | { |
| 897 | u32 val; |
| 898 | u32 z1, z2; |
| 899 | u32 pres; |
| 900 | u32 rxp = 1; |
| 901 | u32 factor = 1000; |
| 902 | |
| 903 | /* calculate the pressure */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 904 | val = at91_adc_readl(st, PRESSR); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 905 | z1 = val & AT91_SAMA5D2_XYZ_MASK; |
| 906 | z2 = (val >> 16) & AT91_SAMA5D2_XYZ_MASK; |
| 907 | |
| 908 | if (z1 != 0) |
| 909 | pres = rxp * (st->touch_st.x_pos * factor / 1024) * |
| 910 | (z2 * factor / z1 - factor) / |
| 911 | factor; |
| 912 | else |
| 913 | pres = 0xFFFF; /* no pen contact */ |
| 914 | |
| 915 | /* |
| 916 | * The pressure from device grows down, minimum is 0xFFFF, maximum 0x0. |
| 917 | * We compute it this way, but let's return it in the expected way, |
| 918 | * growing from 0 to 0xFFFF. |
| 919 | */ |
| 920 | return 0xFFFF - pres; |
| 921 | } |
| 922 | |
| 923 | static int at91_adc_read_position(struct at91_adc_state *st, int chan, u16 *val) |
| 924 | { |
| 925 | *val = 0; |
| 926 | if (!st->touch_st.touching) |
| 927 | return -ENODATA; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 928 | if (chan == st->soc_info.platform->touch_chan_x) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 929 | *val = at91_adc_touch_x_pos(st); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 930 | else if (chan == st->soc_info.platform->touch_chan_y) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 931 | *val = at91_adc_touch_y_pos(st); |
| 932 | else |
| 933 | return -ENODATA; |
| 934 | |
| 935 | return IIO_VAL_INT; |
| 936 | } |
| 937 | |
| 938 | static int at91_adc_read_pressure(struct at91_adc_state *st, int chan, u16 *val) |
| 939 | { |
| 940 | *val = 0; |
| 941 | if (!st->touch_st.touching) |
| 942 | return -ENODATA; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 943 | if (chan == st->soc_info.platform->touch_chan_p) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 944 | *val = at91_adc_touch_pressure(st); |
| 945 | else |
| 946 | return -ENODATA; |
| 947 | |
| 948 | return IIO_VAL_INT; |
| 949 | } |
| 950 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 951 | static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state) |
| 952 | { |
| 953 | struct iio_dev *indio = iio_trigger_get_drvdata(trig); |
| 954 | struct at91_adc_state *st = iio_priv(indio); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 955 | u32 status = at91_adc_readl(st, TRGR); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 956 | |
| 957 | /* clear TRGMOD */ |
| 958 | status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK; |
| 959 | |
| 960 | if (state) |
| 961 | status |= st->selected_trig->trgmod_value; |
| 962 | |
| 963 | /* set/unset hw trigger */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 964 | at91_adc_writel(st, TRGR, status); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 965 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | |
Jonathan Cameron | eca8523 | 2020-09-20 14:25:48 +0100 | [diff] [blame] | 969 | static void at91_adc_reenable_trigger(struct iio_trigger *trig) |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 970 | { |
| 971 | struct iio_dev *indio = iio_trigger_get_drvdata(trig); |
| 972 | struct at91_adc_state *st = iio_priv(indio); |
| 973 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 974 | /* if we are using DMA, we must not reenable irq after each trigger */ |
| 975 | if (st->dma_st.dma_chan) |
Jonathan Cameron | eca8523 | 2020-09-20 14:25:48 +0100 | [diff] [blame] | 976 | return; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 977 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 978 | enable_irq(st->irq); |
| 979 | |
| 980 | /* Needed to ACK the DRDY interruption */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 981 | at91_adc_readl(st, LCDR); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static const struct iio_trigger_ops at91_adc_trigger_ops = { |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 985 | .set_trigger_state = &at91_adc_configure_trigger, |
Jonathan Cameron | eca8523 | 2020-09-20 14:25:48 +0100 | [diff] [blame] | 986 | .reenable = &at91_adc_reenable_trigger, |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 987 | .validate_device = iio_trigger_validate_own_device, |
| 988 | }; |
| 989 | |
| 990 | static int at91_adc_dma_size_done(struct at91_adc_state *st) |
| 991 | { |
| 992 | struct dma_tx_state state; |
| 993 | enum dma_status status; |
| 994 | int i, size; |
| 995 | |
| 996 | status = dmaengine_tx_status(st->dma_st.dma_chan, |
| 997 | st->dma_st.dma_chan->cookie, |
| 998 | &state); |
| 999 | if (status != DMA_IN_PROGRESS) |
| 1000 | return 0; |
| 1001 | |
| 1002 | /* Transferred length is size in bytes from end of buffer */ |
| 1003 | i = st->dma_st.rx_buf_sz - state.residue; |
| 1004 | |
| 1005 | /* Return available bytes */ |
| 1006 | if (i >= st->dma_st.buf_idx) |
| 1007 | size = i - st->dma_st.buf_idx; |
| 1008 | else |
| 1009 | size = st->dma_st.rx_buf_sz + i - st->dma_st.buf_idx; |
| 1010 | return size; |
| 1011 | } |
| 1012 | |
| 1013 | static void at91_dma_buffer_done(void *data) |
| 1014 | { |
| 1015 | struct iio_dev *indio_dev = data; |
| 1016 | |
| 1017 | iio_trigger_poll_chained(indio_dev->trig); |
| 1018 | } |
| 1019 | |
| 1020 | static int at91_adc_dma_start(struct iio_dev *indio_dev) |
| 1021 | { |
| 1022 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1023 | struct dma_async_tx_descriptor *desc; |
| 1024 | dma_cookie_t cookie; |
| 1025 | int ret; |
| 1026 | u8 bit; |
| 1027 | |
| 1028 | if (!st->dma_st.dma_chan) |
| 1029 | return 0; |
| 1030 | |
| 1031 | /* we start a new DMA, so set buffer index to start */ |
| 1032 | st->dma_st.buf_idx = 0; |
| 1033 | |
| 1034 | /* |
| 1035 | * compute buffer size w.r.t. watermark and enabled channels. |
| 1036 | * scan_bytes is aligned so we need an exact size for DMA |
| 1037 | */ |
| 1038 | st->dma_st.rx_buf_sz = 0; |
| 1039 | |
| 1040 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 1041 | indio_dev->num_channels) { |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 1042 | struct iio_chan_spec const *chan = |
| 1043 | at91_adc_chan_get(indio_dev, bit); |
| 1044 | |
| 1045 | if (!chan) |
| 1046 | continue; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1047 | |
| 1048 | st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8; |
| 1049 | } |
| 1050 | st->dma_st.rx_buf_sz *= st->dma_st.watermark; |
| 1051 | |
| 1052 | /* Prepare a DMA cyclic transaction */ |
| 1053 | desc = dmaengine_prep_dma_cyclic(st->dma_st.dma_chan, |
| 1054 | st->dma_st.rx_dma_buf, |
| 1055 | st->dma_st.rx_buf_sz, |
| 1056 | st->dma_st.rx_buf_sz / 2, |
| 1057 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); |
| 1058 | |
| 1059 | if (!desc) { |
| 1060 | dev_err(&indio_dev->dev, "cannot prepare DMA cyclic\n"); |
| 1061 | return -EBUSY; |
| 1062 | } |
| 1063 | |
| 1064 | desc->callback = at91_dma_buffer_done; |
| 1065 | desc->callback_param = indio_dev; |
| 1066 | |
| 1067 | cookie = dmaengine_submit(desc); |
| 1068 | ret = dma_submit_error(cookie); |
| 1069 | if (ret) { |
| 1070 | dev_err(&indio_dev->dev, "cannot submit DMA cyclic\n"); |
| 1071 | dmaengine_terminate_async(st->dma_st.dma_chan); |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
| 1075 | /* enable general overrun error signaling */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1076 | at91_adc_writel(st, IER, AT91_SAMA5D2_IER_GOVRE); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1077 | /* Issue pending DMA requests */ |
| 1078 | dma_async_issue_pending(st->dma_st.dma_chan); |
| 1079 | |
| 1080 | /* consider current time as DMA start time for timestamps */ |
| 1081 | st->dma_st.dma_ts = iio_get_time_ns(indio_dev); |
| 1082 | |
| 1083 | dev_dbg(&indio_dev->dev, "DMA cyclic started\n"); |
| 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1088 | static bool at91_adc_buffer_check_use_irq(struct iio_dev *indio, |
| 1089 | struct at91_adc_state *st) |
| 1090 | { |
| 1091 | /* if using DMA, we do not use our own IRQ (we use DMA-controller) */ |
| 1092 | if (st->dma_st.dma_chan) |
| 1093 | return false; |
| 1094 | /* if the trigger is not ours, then it has its own IRQ */ |
| 1095 | if (iio_trigger_validate_own_device(indio->trig, indio)) |
| 1096 | return false; |
| 1097 | return true; |
| 1098 | } |
| 1099 | |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1100 | static bool at91_adc_current_chan_is_touch(struct iio_dev *indio_dev) |
| 1101 | { |
| 1102 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1103 | |
| 1104 | return !!bitmap_subset(indio_dev->active_scan_mask, |
| 1105 | &st->touch_st.channels_bitmask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1106 | st->soc_info.platform->max_index + 1); |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
Eugen Hristev | 1a19879 | 2020-09-23 15:17:48 +0300 | [diff] [blame] | 1109 | static int at91_adc_buffer_prepare(struct iio_dev *indio_dev) |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1110 | { |
| 1111 | int ret; |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1112 | u8 bit; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1113 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1114 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1115 | /* check if we are enabling triggered buffer or the touchscreen */ |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1116 | if (at91_adc_current_chan_is_touch(indio_dev)) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1117 | return at91_adc_configure_touch(st, true); |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1118 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1119 | /* if we are not in triggered mode, we cannot enable the buffer. */ |
| 1120 | if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | /* we continue with the triggered buffer */ |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1124 | ret = at91_adc_dma_start(indio_dev); |
| 1125 | if (ret) { |
Eugen Hristev | 1a19879 | 2020-09-23 15:17:48 +0300 | [diff] [blame] | 1126 | dev_err(&indio_dev->dev, "buffer prepare failed\n"); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
| 1129 | |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1130 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 1131 | indio_dev->num_channels) { |
| 1132 | struct iio_chan_spec const *chan = |
| 1133 | at91_adc_chan_get(indio_dev, bit); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1134 | if (!chan) |
| 1135 | continue; |
| 1136 | /* these channel types cannot be handled by this trigger */ |
| 1137 | if (chan->type == IIO_POSITIONRELATIVE || |
| 1138 | chan->type == IIO_PRESSURE) |
| 1139 | continue; |
| 1140 | |
Eugen Hristev | d8004c5 | 2021-09-01 15:30:09 +0300 | [diff] [blame] | 1141 | at91_adc_cor(st, chan); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1142 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1143 | at91_adc_writel(st, CHER, BIT(chan->channel)); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | if (at91_adc_buffer_check_use_irq(indio_dev, st)) |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1147 | at91_adc_writel(st, IER, AT91_SAMA5D2_IER_DRDY); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1148 | |
Alexandru Ardelean | f3c034f | 2020-03-04 10:42:19 +0200 | [diff] [blame] | 1149 | return 0; |
| 1150 | } |
| 1151 | |
Alexandru Ardelean | f3c034f | 2020-03-04 10:42:19 +0200 | [diff] [blame] | 1152 | static int at91_adc_buffer_postdisable(struct iio_dev *indio_dev) |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1153 | { |
| 1154 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1155 | u8 bit; |
| 1156 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1157 | /* check if we are disabling triggered buffer or the touchscreen */ |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1158 | if (at91_adc_current_chan_is_touch(indio_dev)) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1159 | return at91_adc_configure_touch(st, false); |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 1160 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1161 | /* if we are not in triggered mode, nothing to do here */ |
| 1162 | if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) |
| 1163 | return -EINVAL; |
| 1164 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1165 | /* |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1166 | * For each enable channel we must disable it in hardware. |
| 1167 | * In the case of DMA, we must read the last converted value |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1168 | * to clear EOC status and not get a possible interrupt later. |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1169 | * This value is being read by DMA from LCDR anyway, so it's not lost. |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1170 | */ |
| 1171 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 1172 | indio_dev->num_channels) { |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 1173 | struct iio_chan_spec const *chan = |
| 1174 | at91_adc_chan_get(indio_dev, bit); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1175 | |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 1176 | if (!chan) |
| 1177 | continue; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1178 | /* these channel types are virtual, no need to do anything */ |
| 1179 | if (chan->type == IIO_POSITIONRELATIVE || |
| 1180 | chan->type == IIO_PRESSURE) |
| 1181 | continue; |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1182 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1183 | at91_adc_writel(st, CHDR, BIT(chan->channel)); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1184 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1185 | if (st->dma_st.dma_chan) |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1186 | at91_adc_read_chan(st, chan->address); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1189 | if (at91_adc_buffer_check_use_irq(indio_dev, st)) |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1190 | at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_DRDY); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1191 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1192 | /* read overflow register to clear possible overflow status */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1193 | at91_adc_readl(st, OVER); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1194 | |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1195 | /* if we are using DMA we must clear registers and end DMA */ |
| 1196 | if (st->dma_st.dma_chan) |
| 1197 | dmaengine_terminate_sync(st->dma_st.dma_chan); |
| 1198 | |
Alexandru Ardelean | f3c034f | 2020-03-04 10:42:19 +0200 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1202 | static const struct iio_buffer_setup_ops at91_buffer_setup_ops = { |
Alexandru Ardelean | f3c034f | 2020-03-04 10:42:19 +0200 | [diff] [blame] | 1203 | .postdisable = &at91_adc_buffer_postdisable, |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1204 | }; |
| 1205 | |
| 1206 | static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio, |
| 1207 | char *trigger_name) |
| 1208 | { |
| 1209 | struct iio_trigger *trig; |
| 1210 | int ret; |
| 1211 | |
| 1212 | trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name, |
Jonathan Cameron | 15ea2878 | 2021-04-26 18:49:03 +0100 | [diff] [blame] | 1213 | iio_device_id(indio), trigger_name); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1214 | if (!trig) |
| 1215 | return NULL; |
| 1216 | |
| 1217 | trig->dev.parent = indio->dev.parent; |
| 1218 | iio_trigger_set_drvdata(trig, indio); |
| 1219 | trig->ops = &at91_adc_trigger_ops; |
| 1220 | |
| 1221 | ret = devm_iio_trigger_register(&indio->dev, trig); |
| 1222 | if (ret) |
| 1223 | return ERR_PTR(ret); |
| 1224 | |
| 1225 | return trig; |
| 1226 | } |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1227 | static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev, |
| 1228 | struct iio_poll_func *pf) |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1229 | { |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1230 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1231 | int i = 0; |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1232 | int val; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1233 | u8 bit; |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 1234 | u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev); |
| 1235 | unsigned int timeout = 50; |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1236 | u32 status, imr, eoc = 0, eoc_imr; |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* |
| 1239 | * Check if the conversion is ready. If not, wait a little bit, and |
| 1240 | * in case of timeout exit with an error. |
| 1241 | */ |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1242 | while (((eoc & mask) != mask) && timeout) { |
| 1243 | at91_adc_irq_status(st, &status, &eoc); |
| 1244 | at91_adc_irq_mask(st, &imr, &eoc_imr); |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 1245 | usleep_range(50, 100); |
| 1246 | timeout--; |
| 1247 | } |
| 1248 | |
| 1249 | /* Cannot read data, not ready. Continue without reporting data */ |
| 1250 | if (!timeout) |
| 1251 | return; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1252 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1253 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 1254 | indio_dev->num_channels) { |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 1255 | struct iio_chan_spec const *chan = |
| 1256 | at91_adc_chan_get(indio_dev, bit); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1257 | |
Eugen Hristev | f0c8d1f | 2018-04-10 11:57:47 +0300 | [diff] [blame] | 1258 | if (!chan) |
| 1259 | continue; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1260 | /* |
| 1261 | * Our external trigger only supports the voltage channels. |
| 1262 | * In case someone requested a different type of channel |
| 1263 | * just put zeroes to buffer. |
| 1264 | * This should not happen because we check the scan mode |
| 1265 | * and scan mask when we enable the buffer, and we don't allow |
| 1266 | * the buffer to start with a mixed mask (voltage and something |
| 1267 | * else). |
| 1268 | * Thus, emit a warning. |
| 1269 | */ |
| 1270 | if (chan->type == IIO_VOLTAGE) { |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1271 | val = at91_adc_read_chan(st, chan->address); |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1272 | at91_adc_adjust_val_osr(st, &val); |
| 1273 | st->buffer[i] = val; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1274 | } else { |
| 1275 | st->buffer[i] = 0; |
| 1276 | WARN(true, "This trigger cannot handle this type of channel"); |
| 1277 | } |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1278 | i++; |
| 1279 | } |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1280 | iio_push_to_buffers_with_timestamp(indio_dev, st->buffer, |
| 1281 | pf->timestamp); |
| 1282 | } |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1283 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1284 | static void at91_adc_trigger_handler_dma(struct iio_dev *indio_dev) |
| 1285 | { |
| 1286 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1287 | int transferred_len = at91_adc_dma_size_done(st); |
| 1288 | s64 ns = iio_get_time_ns(indio_dev); |
| 1289 | s64 interval; |
| 1290 | int sample_index = 0, sample_count, sample_size; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1291 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1292 | u32 status = at91_adc_readl(st, ISR); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1293 | /* if we reached this point, we cannot sample faster */ |
| 1294 | if (status & AT91_SAMA5D2_IER_GOVRE) |
| 1295 | pr_info_ratelimited("%s: conversion overrun detected\n", |
| 1296 | indio_dev->name); |
| 1297 | |
| 1298 | sample_size = div_s64(st->dma_st.rx_buf_sz, st->dma_st.watermark); |
| 1299 | |
| 1300 | sample_count = div_s64(transferred_len, sample_size); |
| 1301 | |
| 1302 | /* |
| 1303 | * interval between samples is total time since last transfer handling |
| 1304 | * divided by the number of samples (total size divided by sample size) |
| 1305 | */ |
| 1306 | interval = div_s64((ns - st->dma_st.dma_ts), sample_count); |
| 1307 | |
| 1308 | while (transferred_len >= sample_size) { |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1309 | /* |
| 1310 | * for all the values in the current sample, |
| 1311 | * adjust the values inside the buffer for oversampling |
| 1312 | */ |
| 1313 | at91_adc_adjust_val_osr_array(st, |
| 1314 | &st->dma_st.rx_buf[st->dma_st.buf_idx], |
| 1315 | sample_size); |
| 1316 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1317 | iio_push_to_buffers_with_timestamp(indio_dev, |
| 1318 | (st->dma_st.rx_buf + st->dma_st.buf_idx), |
| 1319 | (st->dma_st.dma_ts + interval * sample_index)); |
| 1320 | /* adjust remaining length */ |
| 1321 | transferred_len -= sample_size; |
| 1322 | /* adjust buffer index */ |
| 1323 | st->dma_st.buf_idx += sample_size; |
| 1324 | /* in case of reaching end of buffer, reset index */ |
| 1325 | if (st->dma_st.buf_idx >= st->dma_st.rx_buf_sz) |
| 1326 | st->dma_st.buf_idx = 0; |
| 1327 | sample_index++; |
| 1328 | } |
| 1329 | /* adjust saved time for next transfer handling */ |
| 1330 | st->dma_st.dma_ts = iio_get_time_ns(indio_dev); |
| 1331 | } |
| 1332 | |
| 1333 | static irqreturn_t at91_adc_trigger_handler(int irq, void *p) |
| 1334 | { |
| 1335 | struct iio_poll_func *pf = p; |
| 1336 | struct iio_dev *indio_dev = pf->indio_dev; |
| 1337 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1338 | |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1339 | /* |
| 1340 | * If it's not our trigger, start a conversion now, as we are |
| 1341 | * actually polling the trigger now. |
| 1342 | */ |
| 1343 | if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev)) |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1344 | at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START); |
Eugen Hristev | abb7e84 | 2020-01-28 12:57:41 +0000 | [diff] [blame] | 1345 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1346 | if (st->dma_st.dma_chan) |
| 1347 | at91_adc_trigger_handler_dma(indio_dev); |
| 1348 | else |
| 1349 | at91_adc_trigger_handler_nodma(indio_dev, pf); |
| 1350 | |
| 1351 | iio_trigger_notify_done(indio_dev->trig); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1352 | |
| 1353 | return IRQ_HANDLED; |
| 1354 | } |
| 1355 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1356 | static unsigned at91_adc_startup_time(unsigned startup_time_min, |
| 1357 | unsigned adc_clk_khz) |
| 1358 | { |
Colin Ian King | 2df331c | 2017-07-07 17:08:35 +0100 | [diff] [blame] | 1359 | static const unsigned int startup_lookup[] = { |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1360 | 0, 8, 16, 24, |
| 1361 | 64, 80, 96, 112, |
| 1362 | 512, 576, 640, 704, |
| 1363 | 768, 832, 896, 960 |
| 1364 | }; |
| 1365 | unsigned ticks_min, i; |
| 1366 | |
| 1367 | /* |
| 1368 | * Since the adc frequency is checked before, there is no reason |
| 1369 | * to not meet the startup time constraint. |
| 1370 | */ |
| 1371 | |
| 1372 | ticks_min = startup_time_min * adc_clk_khz / 1000; |
| 1373 | for (i = 0; i < ARRAY_SIZE(startup_lookup); i++) |
| 1374 | if (startup_lookup[i] > ticks_min) |
| 1375 | break; |
| 1376 | |
| 1377 | return i; |
| 1378 | } |
| 1379 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1380 | static void at91_adc_setup_samp_freq(struct iio_dev *indio_dev, unsigned freq) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1381 | { |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1382 | struct at91_adc_state *st = iio_priv(indio_dev); |
Ludovic Desroches | 94b2423 | 2016-03-22 17:08:45 +0100 | [diff] [blame] | 1383 | unsigned f_per, prescal, startup, mr; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1384 | |
| 1385 | f_per = clk_get_rate(st->per_clk); |
| 1386 | prescal = (f_per / (2 * freq)) - 1; |
| 1387 | |
| 1388 | startup = at91_adc_startup_time(st->soc_info.startup_time, |
| 1389 | freq / 1000); |
| 1390 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1391 | mr = at91_adc_readl(st, MR); |
Ludovic Desroches | 94b2423 | 2016-03-22 17:08:45 +0100 | [diff] [blame] | 1392 | mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK); |
| 1393 | mr |= AT91_SAMA5D2_MR_STARTUP(startup); |
| 1394 | mr |= AT91_SAMA5D2_MR_PRESCAL(prescal); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1395 | at91_adc_writel(st, MR, mr); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1396 | |
| 1397 | dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n", |
| 1398 | freq, startup, prescal); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1399 | st->current_sample_rate = freq; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1402 | static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state *st) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1403 | { |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1404 | return st->current_sample_rate; |
| 1405 | } |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1406 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1407 | static void at91_adc_touch_data_handler(struct iio_dev *indio_dev) |
| 1408 | { |
| 1409 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1410 | u8 bit; |
| 1411 | u16 val; |
| 1412 | int i = 0; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1413 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1414 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1415 | st->soc_info.platform->max_index + 1) { |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1416 | struct iio_chan_spec const *chan = |
| 1417 | at91_adc_chan_get(indio_dev, bit); |
| 1418 | |
| 1419 | if (chan->type == IIO_POSITIONRELATIVE) |
| 1420 | at91_adc_read_position(st, chan->channel, &val); |
| 1421 | else if (chan->type == IIO_PRESSURE) |
| 1422 | at91_adc_read_pressure(st, chan->channel, &val); |
| 1423 | else |
| 1424 | continue; |
| 1425 | st->buffer[i] = val; |
| 1426 | i++; |
| 1427 | } |
| 1428 | /* |
| 1429 | * Schedule work to push to buffers. |
| 1430 | * This is intended to push to the callback buffer that another driver |
| 1431 | * registered. We are still in a handler from our IRQ. If we push |
| 1432 | * directly, it means the other driver has it's callback called |
| 1433 | * from our IRQ context. Which is something we better avoid. |
| 1434 | * Let's schedule it after our IRQ is completed. |
| 1435 | */ |
| 1436 | schedule_work(&st->touch_st.workq); |
| 1437 | } |
| 1438 | |
| 1439 | static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st) |
| 1440 | { |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1441 | at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_PEN); |
| 1442 | at91_adc_writel(st, IER, AT91_SAMA5D2_IER_NOPEN | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1443 | AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | |
| 1444 | AT91_SAMA5D2_IER_PRDY); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1445 | at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1446 | AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val)); |
| 1447 | st->touch_st.touching = true; |
| 1448 | } |
| 1449 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1450 | static void at91_adc_no_pen_detect_interrupt(struct iio_dev *indio_dev) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1451 | { |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1452 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1453 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1454 | at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER); |
| 1455 | at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_NOPEN | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1456 | AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | |
| 1457 | AT91_SAMA5D2_IER_PRDY); |
| 1458 | st->touch_st.touching = false; |
| 1459 | |
| 1460 | at91_adc_touch_data_handler(indio_dev); |
| 1461 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1462 | at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | static void at91_adc_workq_handler(struct work_struct *workq) |
| 1466 | { |
| 1467 | struct at91_adc_touch *touch_st = container_of(workq, |
| 1468 | struct at91_adc_touch, workq); |
| 1469 | struct at91_adc_state *st = container_of(touch_st, |
| 1470 | struct at91_adc_state, touch_st); |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1471 | struct iio_dev *indio_dev = st->indio_dev; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1472 | |
| 1473 | iio_push_to_buffers(indio_dev, st->buffer); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static irqreturn_t at91_adc_interrupt(int irq, void *private) |
| 1477 | { |
| 1478 | struct iio_dev *indio = private; |
| 1479 | struct at91_adc_state *st = iio_priv(indio); |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1480 | u32 status, eoc, imr, eoc_imr; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1481 | u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | |
| 1482 | AT91_SAMA5D2_IER_PRDY; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1483 | |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1484 | at91_adc_irq_status(st, &status, &eoc); |
| 1485 | at91_adc_irq_mask(st, &imr, &eoc_imr); |
| 1486 | |
| 1487 | if (!(status & imr) && !(eoc & eoc_imr)) |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1488 | return IRQ_NONE; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1489 | if (status & AT91_SAMA5D2_IER_PEN) { |
| 1490 | /* pen detected IRQ */ |
| 1491 | at91_adc_pen_detect_interrupt(st); |
| 1492 | } else if ((status & AT91_SAMA5D2_IER_NOPEN)) { |
| 1493 | /* nopen detected IRQ */ |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1494 | at91_adc_no_pen_detect_interrupt(indio); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1495 | } else if ((status & AT91_SAMA5D2_ISR_PENS) && |
| 1496 | ((status & rdy_mask) == rdy_mask)) { |
| 1497 | /* periodic trigger IRQ - during pen sense */ |
| 1498 | at91_adc_touch_data_handler(indio); |
| 1499 | } else if (status & AT91_SAMA5D2_ISR_PENS) { |
| 1500 | /* |
| 1501 | * touching, but the measurements are not ready yet. |
| 1502 | * read and ignore. |
| 1503 | */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1504 | status = at91_adc_readl(st, XPOSR); |
| 1505 | status = at91_adc_readl(st, YPOSR); |
| 1506 | status = at91_adc_readl(st, PRESSR); |
Eugen Hristev | 97c54cf | 2020-01-28 12:57:40 +0000 | [diff] [blame] | 1507 | } else if (iio_buffer_enabled(indio) && |
| 1508 | (status & AT91_SAMA5D2_IER_DRDY)) { |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1509 | /* triggered buffer without DMA */ |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1510 | disable_irq_nosync(irq); |
| 1511 | iio_trigger_poll(indio->trig); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1512 | } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) { |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1513 | /* triggered buffer with DMA - should not happen */ |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1514 | disable_irq_nosync(irq); |
| 1515 | WARN(true, "Unexpected irq occurred\n"); |
| 1516 | } else if (!iio_buffer_enabled(indio)) { |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1517 | /* software requested conversion */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1518 | st->conversion_value = at91_adc_read_chan(st, st->chan->address); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1519 | st->conversion_done = true; |
| 1520 | wake_up_interruptible(&st->wq_data_available); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1521 | } |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1522 | return IRQ_HANDLED; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1525 | static int at91_adc_read_info_raw(struct iio_dev *indio_dev, |
| 1526 | struct iio_chan_spec const *chan, int *val) |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1527 | { |
| 1528 | struct at91_adc_state *st = iio_priv(indio_dev); |
Dan Carpenter | a176ba3 | 2018-07-09 14:06:59 +0300 | [diff] [blame] | 1529 | u16 tmp_val; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1530 | int ret; |
| 1531 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1532 | /* |
| 1533 | * Keep in mind that we cannot use software trigger or touchscreen |
| 1534 | * if external trigger is enabled |
| 1535 | */ |
| 1536 | if (chan->type == IIO_POSITIONRELATIVE) { |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1537 | ret = iio_device_claim_direct_mode(indio_dev); |
| 1538 | if (ret) |
| 1539 | return ret; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1540 | mutex_lock(&st->lock); |
| 1541 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1542 | ret = at91_adc_read_position(st, chan->channel, |
Dan Carpenter | a176ba3 | 2018-07-09 14:06:59 +0300 | [diff] [blame] | 1543 | &tmp_val); |
| 1544 | *val = tmp_val; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1545 | mutex_unlock(&st->lock); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1546 | iio_device_release_direct_mode(indio_dev); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1547 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1548 | return at91_adc_adjust_val_osr(st, val); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1549 | } |
| 1550 | if (chan->type == IIO_PRESSURE) { |
| 1551 | ret = iio_device_claim_direct_mode(indio_dev); |
| 1552 | if (ret) |
| 1553 | return ret; |
| 1554 | mutex_lock(&st->lock); |
| 1555 | |
| 1556 | ret = at91_adc_read_pressure(st, chan->channel, |
Dan Carpenter | a176ba3 | 2018-07-09 14:06:59 +0300 | [diff] [blame] | 1557 | &tmp_val); |
| 1558 | *val = tmp_val; |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1559 | mutex_unlock(&st->lock); |
| 1560 | iio_device_release_direct_mode(indio_dev); |
| 1561 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1562 | return at91_adc_adjust_val_osr(st, val); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /* in this case we have a voltage channel */ |
| 1566 | |
| 1567 | ret = iio_device_claim_direct_mode(indio_dev); |
| 1568 | if (ret) |
| 1569 | return ret; |
| 1570 | mutex_lock(&st->lock); |
| 1571 | |
| 1572 | st->chan = chan; |
| 1573 | |
Eugen Hristev | d8004c5 | 2021-09-01 15:30:09 +0300 | [diff] [blame] | 1574 | at91_adc_cor(st, chan); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1575 | at91_adc_writel(st, CHER, BIT(chan->channel)); |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1576 | at91_adc_eoc_ena(st, chan->channel); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1577 | at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1578 | |
| 1579 | ret = wait_event_interruptible_timeout(st->wq_data_available, |
| 1580 | st->conversion_done, |
| 1581 | msecs_to_jiffies(1000)); |
| 1582 | if (ret == 0) |
| 1583 | ret = -ETIMEDOUT; |
| 1584 | |
| 1585 | if (ret > 0) { |
| 1586 | *val = st->conversion_value; |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1587 | ret = at91_adc_adjust_val_osr(st, val); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1588 | if (chan->scan_type.sign == 's') |
Gwendal Grignou | 652e7df | 2021-11-04 01:24:08 -0700 | [diff] [blame] | 1589 | *val = sign_extend32(*val, |
| 1590 | chan->scan_type.realbits - 1); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1591 | st->conversion_done = false; |
| 1592 | } |
| 1593 | |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1594 | at91_adc_eoc_dis(st, st->chan->channel); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1595 | at91_adc_writel(st, CHDR, BIT(chan->channel)); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1596 | |
| 1597 | /* Needed to ACK the DRDY interruption */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1598 | at91_adc_readl(st, LCDR); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1599 | |
| 1600 | mutex_unlock(&st->lock); |
| 1601 | |
| 1602 | iio_device_release_direct_mode(indio_dev); |
| 1603 | return ret; |
| 1604 | } |
| 1605 | |
| 1606 | static int at91_adc_read_raw(struct iio_dev *indio_dev, |
| 1607 | struct iio_chan_spec const *chan, |
| 1608 | int *val, int *val2, long mask) |
| 1609 | { |
| 1610 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1611 | |
| 1612 | switch (mask) { |
| 1613 | case IIO_CHAN_INFO_RAW: |
| 1614 | return at91_adc_read_info_raw(indio_dev, chan, val); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1615 | case IIO_CHAN_INFO_SCALE: |
| 1616 | *val = st->vref_uv / 1000; |
Ludovic Desroches | d651132 | 2016-03-22 17:08:46 +0100 | [diff] [blame] | 1617 | if (chan->differential) |
| 1618 | *val *= 2; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1619 | *val2 = chan->scan_type.realbits; |
| 1620 | return IIO_VAL_FRACTIONAL_LOG2; |
| 1621 | |
| 1622 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 1623 | *val = at91_adc_get_sample_freq(st); |
| 1624 | return IIO_VAL_INT; |
| 1625 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1626 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 1627 | *val = st->oversampling_ratio; |
| 1628 | return IIO_VAL_INT; |
| 1629 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1630 | default: |
| 1631 | return -EINVAL; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | static int at91_adc_write_raw(struct iio_dev *indio_dev, |
| 1636 | struct iio_chan_spec const *chan, |
| 1637 | int val, int val2, long mask) |
| 1638 | { |
| 1639 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1640 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1641 | switch (mask) { |
| 1642 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 1643 | if ((val != AT91_OSR_1SAMPLES) && (val != AT91_OSR_4SAMPLES) && |
| 1644 | (val != AT91_OSR_16SAMPLES)) |
| 1645 | return -EINVAL; |
| 1646 | /* if no change, optimize out */ |
| 1647 | if (val == st->oversampling_ratio) |
| 1648 | return 0; |
| 1649 | st->oversampling_ratio = val; |
| 1650 | /* update ratio */ |
| 1651 | at91_adc_config_emr(st); |
| 1652 | return 0; |
| 1653 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 1654 | if (val < st->soc_info.min_sample_rate || |
| 1655 | val > st->soc_info.max_sample_rate) |
| 1656 | return -EINVAL; |
| 1657 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1658 | at91_adc_setup_samp_freq(indio_dev, val); |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1659 | return 0; |
| 1660 | default: |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1661 | return -EINVAL; |
Tom Rix | 7f96027 | 2020-10-27 13:08:53 -0700 | [diff] [blame] | 1662 | } |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1665 | static void at91_adc_dma_init(struct platform_device *pdev) |
| 1666 | { |
| 1667 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
| 1668 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1669 | struct dma_slave_config config = {0}; |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1670 | /* we have 2 bytes for each channel */ |
| 1671 | unsigned int sample_size = st->soc_info.platform->nr_channels * 2; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1672 | /* |
| 1673 | * We make the buffer double the size of the fifo, |
| 1674 | * such that DMA uses one half of the buffer (full fifo size) |
| 1675 | * and the software uses the other half to read/write. |
| 1676 | */ |
| 1677 | unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE * |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1678 | sample_size * 2, PAGE_SIZE); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1679 | |
| 1680 | if (st->dma_st.dma_chan) |
| 1681 | return; |
| 1682 | |
Peter Ujfalusi | 687d39d | 2020-01-07 13:37:29 +0200 | [diff] [blame] | 1683 | st->dma_st.dma_chan = dma_request_chan(&pdev->dev, "rx"); |
| 1684 | if (IS_ERR(st->dma_st.dma_chan)) { |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1685 | dev_info(&pdev->dev, "can't get DMA channel\n"); |
Peter Ujfalusi | 687d39d | 2020-01-07 13:37:29 +0200 | [diff] [blame] | 1686 | st->dma_st.dma_chan = NULL; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1687 | goto dma_exit; |
| 1688 | } |
| 1689 | |
| 1690 | st->dma_st.rx_buf = dma_alloc_coherent(st->dma_st.dma_chan->device->dev, |
| 1691 | pages * PAGE_SIZE, |
| 1692 | &st->dma_st.rx_dma_buf, |
| 1693 | GFP_KERNEL); |
| 1694 | if (!st->dma_st.rx_buf) { |
| 1695 | dev_info(&pdev->dev, "can't allocate coherent DMA area\n"); |
| 1696 | goto dma_chan_disable; |
| 1697 | } |
| 1698 | |
| 1699 | /* Configure DMA channel to read data register */ |
| 1700 | config.direction = DMA_DEV_TO_MEM; |
| 1701 | config.src_addr = (phys_addr_t)(st->dma_st.phys_addr |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1702 | + st->soc_info.platform->layout->LCDR); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1703 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 1704 | config.src_maxburst = 1; |
| 1705 | config.dst_maxburst = 1; |
| 1706 | |
| 1707 | if (dmaengine_slave_config(st->dma_st.dma_chan, &config)) { |
| 1708 | dev_info(&pdev->dev, "can't configure DMA slave\n"); |
| 1709 | goto dma_free_area; |
| 1710 | } |
| 1711 | |
| 1712 | dev_info(&pdev->dev, "using %s for rx DMA transfers\n", |
| 1713 | dma_chan_name(st->dma_st.dma_chan)); |
| 1714 | |
| 1715 | return; |
| 1716 | |
| 1717 | dma_free_area: |
| 1718 | dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE, |
| 1719 | st->dma_st.rx_buf, st->dma_st.rx_dma_buf); |
| 1720 | dma_chan_disable: |
| 1721 | dma_release_channel(st->dma_st.dma_chan); |
Jonathan Cameron | 5617f22 | 2019-10-13 09:57:23 +0100 | [diff] [blame] | 1722 | st->dma_st.dma_chan = NULL; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1723 | dma_exit: |
| 1724 | dev_info(&pdev->dev, "continuing without DMA support\n"); |
| 1725 | } |
| 1726 | |
| 1727 | static void at91_adc_dma_disable(struct platform_device *pdev) |
| 1728 | { |
| 1729 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
| 1730 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1731 | /* we have 2 bytes for each channel */ |
| 1732 | unsigned int sample_size = st->soc_info.platform->nr_channels * 2; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1733 | unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE * |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1734 | sample_size * 2, PAGE_SIZE); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1735 | |
| 1736 | /* if we are not using DMA, just return */ |
| 1737 | if (!st->dma_st.dma_chan) |
| 1738 | return; |
| 1739 | |
| 1740 | /* wait for all transactions to be terminated first*/ |
| 1741 | dmaengine_terminate_sync(st->dma_st.dma_chan); |
| 1742 | |
| 1743 | dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE, |
| 1744 | st->dma_st.rx_buf, st->dma_st.rx_dma_buf); |
| 1745 | dma_release_channel(st->dma_st.dma_chan); |
Jonathan Cameron | 5617f22 | 2019-10-13 09:57:23 +0100 | [diff] [blame] | 1746 | st->dma_st.dma_chan = NULL; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1747 | |
| 1748 | dev_info(&pdev->dev, "continuing without DMA support\n"); |
| 1749 | } |
| 1750 | |
| 1751 | static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val) |
| 1752 | { |
| 1753 | struct at91_adc_state *st = iio_priv(indio_dev); |
Eugen Hristev | 1a19879 | 2020-09-23 15:17:48 +0300 | [diff] [blame] | 1754 | int ret; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1755 | |
| 1756 | if (val > AT91_HWFIFO_MAX_SIZE) |
| 1757 | return -EINVAL; |
| 1758 | |
| 1759 | if (!st->selected_trig->hw_trig) { |
| 1760 | dev_dbg(&indio_dev->dev, "we need hw trigger for DMA\n"); |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | dev_dbg(&indio_dev->dev, "new watermark is %u\n", val); |
| 1765 | st->dma_st.watermark = val; |
| 1766 | |
| 1767 | /* |
| 1768 | * The logic here is: if we have watermark 1, it means we do |
| 1769 | * each conversion with it's own IRQ, thus we don't need DMA. |
| 1770 | * If the watermark is higher, we do DMA to do all the transfers in bulk |
| 1771 | */ |
| 1772 | |
| 1773 | if (val == 1) |
| 1774 | at91_adc_dma_disable(to_platform_device(&indio_dev->dev)); |
| 1775 | else if (val > 1) |
| 1776 | at91_adc_dma_init(to_platform_device(&indio_dev->dev)); |
| 1777 | |
Eugen Hristev | 1a19879 | 2020-09-23 15:17:48 +0300 | [diff] [blame] | 1778 | /* |
| 1779 | * We can start the DMA only after setting the watermark and |
| 1780 | * having the DMA initialization completed |
| 1781 | */ |
| 1782 | ret = at91_adc_buffer_prepare(indio_dev); |
| 1783 | if (ret) |
| 1784 | at91_adc_dma_disable(to_platform_device(&indio_dev->dev)); |
| 1785 | |
| 1786 | return ret; |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1789 | static int at91_adc_update_scan_mode(struct iio_dev *indio_dev, |
| 1790 | const unsigned long *scan_mask) |
| 1791 | { |
| 1792 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1793 | |
| 1794 | if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1795 | st->soc_info.platform->max_index + 1)) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1796 | return 0; |
| 1797 | /* |
| 1798 | * if the new bitmap is a combination of touchscreen and regular |
| 1799 | * channels, then we are not fine |
| 1800 | */ |
| 1801 | if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1802 | st->soc_info.platform->max_index + 1)) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1803 | return -EINVAL; |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1807 | static void at91_adc_hw_init(struct iio_dev *indio_dev) |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 1808 | { |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1809 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1810 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1811 | at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST); |
Eugen Hristev | e6d5eee | 2021-09-01 15:30:08 +0300 | [diff] [blame] | 1812 | if (st->soc_info.platform->layout->EOC_IDR) |
| 1813 | at91_adc_writel(st, EOC_IDR, 0xffffffff); |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1814 | at91_adc_writel(st, IDR, 0xffffffff); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 1815 | /* |
| 1816 | * Transfer field must be set to 2 according to the datasheet and |
| 1817 | * allows different analog settings for each channel. |
| 1818 | */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1819 | at91_adc_writel(st, MR, |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 1820 | AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH); |
| 1821 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1822 | at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate); |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1823 | |
| 1824 | /* configure extended mode register */ |
| 1825 | at91_adc_config_emr(st); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 1826 | } |
| 1827 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1828 | static ssize_t at91_adc_get_fifo_state(struct device *dev, |
| 1829 | struct device_attribute *attr, char *buf) |
| 1830 | { |
Kefeng Wang | e3faedf | 2019-04-23 15:50:03 +0800 | [diff] [blame] | 1831 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1832 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1833 | |
| 1834 | return scnprintf(buf, PAGE_SIZE, "%d\n", !!st->dma_st.dma_chan); |
| 1835 | } |
| 1836 | |
| 1837 | static ssize_t at91_adc_get_watermark(struct device *dev, |
| 1838 | struct device_attribute *attr, char *buf) |
| 1839 | { |
Kefeng Wang | e3faedf | 2019-04-23 15:50:03 +0800 | [diff] [blame] | 1840 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1841 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 1842 | |
| 1843 | return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark); |
| 1844 | } |
| 1845 | |
| 1846 | static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, |
| 1847 | at91_adc_get_fifo_state, NULL, 0); |
| 1848 | static IIO_DEVICE_ATTR(hwfifo_watermark, 0444, |
| 1849 | at91_adc_get_watermark, NULL, 0); |
| 1850 | |
| 1851 | static IIO_CONST_ATTR(hwfifo_watermark_min, "2"); |
| 1852 | static IIO_CONST_ATTR(hwfifo_watermark_max, AT91_HWFIFO_MAX_SIZE_STR); |
| 1853 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1854 | static IIO_CONST_ATTR(oversampling_ratio_available, |
| 1855 | __stringify(AT91_OSR_1SAMPLES) " " |
| 1856 | __stringify(AT91_OSR_4SAMPLES) " " |
| 1857 | __stringify(AT91_OSR_16SAMPLES)); |
| 1858 | |
| 1859 | static struct attribute *at91_adc_attributes[] = { |
| 1860 | &iio_const_attr_oversampling_ratio_available.dev_attr.attr, |
| 1861 | NULL, |
| 1862 | }; |
| 1863 | |
| 1864 | static const struct attribute_group at91_adc_attribute_group = { |
| 1865 | .attrs = at91_adc_attributes, |
| 1866 | }; |
| 1867 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 1868 | static const struct attribute *at91_adc_fifo_attributes[] = { |
| 1869 | &iio_const_attr_hwfifo_watermark_min.dev_attr.attr, |
| 1870 | &iio_const_attr_hwfifo_watermark_max.dev_attr.attr, |
| 1871 | &iio_dev_attr_hwfifo_watermark.dev_attr.attr, |
| 1872 | &iio_dev_attr_hwfifo_enabled.dev_attr.attr, |
| 1873 | NULL, |
| 1874 | }; |
| 1875 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1876 | static const struct iio_info at91_adc_info = { |
| 1877 | .attrs = &at91_adc_attribute_group, |
| 1878 | .read_raw = &at91_adc_read_raw, |
| 1879 | .write_raw = &at91_adc_write_raw, |
| 1880 | .update_scan_mode = &at91_adc_update_scan_mode, |
| 1881 | .of_xlate = &at91_adc_of_xlate, |
| 1882 | .hwfifo_set_watermark = &at91_adc_set_watermark, |
| 1883 | }; |
| 1884 | |
Alexandru Ardelean | 789976a | 2020-09-29 15:59:42 +0300 | [diff] [blame] | 1885 | static int at91_adc_buffer_and_trigger_init(struct device *dev, |
| 1886 | struct iio_dev *indio) |
| 1887 | { |
| 1888 | struct at91_adc_state *st = iio_priv(indio); |
Alexandru Ardelean | a2f2835 | 2020-09-29 15:59:46 +0300 | [diff] [blame] | 1889 | const struct attribute **fifo_attrs; |
Alexandru Ardelean | 789976a | 2020-09-29 15:59:42 +0300 | [diff] [blame] | 1890 | int ret; |
| 1891 | |
Alexandru Ardelean | a2f2835 | 2020-09-29 15:59:46 +0300 | [diff] [blame] | 1892 | if (st->selected_trig->hw_trig) |
| 1893 | fifo_attrs = at91_adc_fifo_attributes; |
| 1894 | else |
| 1895 | fifo_attrs = NULL; |
| 1896 | |
| 1897 | ret = devm_iio_triggered_buffer_setup_ext(&indio->dev, indio, |
Alexandru Ardelean | c02cd5c | 2021-10-07 08:00:32 +0000 | [diff] [blame] | 1898 | &iio_pollfunc_store_time, &at91_adc_trigger_handler, |
| 1899 | IIO_BUFFER_DIRECTION_IN, &at91_buffer_setup_ops, fifo_attrs); |
Alexandru Ardelean | 789976a | 2020-09-29 15:59:42 +0300 | [diff] [blame] | 1900 | if (ret < 0) { |
| 1901 | dev_err(dev, "couldn't initialize the buffer.\n"); |
| 1902 | return ret; |
| 1903 | } |
| 1904 | |
| 1905 | if (!st->selected_trig->hw_trig) |
| 1906 | return 0; |
| 1907 | |
Alexandru Ardelean | 789976a | 2020-09-29 15:59:42 +0300 | [diff] [blame] | 1908 | st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name); |
| 1909 | if (IS_ERR(st->trig)) { |
| 1910 | dev_err(dev, "could not allocate trigger\n"); |
| 1911 | return PTR_ERR(st->trig); |
| 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * Initially the iio buffer has a length of 2 and |
| 1916 | * a watermark of 1 |
| 1917 | */ |
| 1918 | st->dma_st.watermark = 1; |
| 1919 | |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1923 | static int at91_adc_probe(struct platform_device *pdev) |
| 1924 | { |
| 1925 | struct iio_dev *indio_dev; |
| 1926 | struct at91_adc_state *st; |
| 1927 | struct resource *res; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1928 | int ret, i; |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 1929 | u32 edge_type = IRQ_TYPE_NONE; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1930 | |
Ludovic Desroches | 61be8fd | 2016-01-18 09:41:56 +0100 | [diff] [blame] | 1931 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st)); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1932 | if (!indio_dev) |
| 1933 | return -ENOMEM; |
| 1934 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1935 | st = iio_priv(indio_dev); |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 1936 | st->indio_dev = indio_dev; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1937 | |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1938 | st->soc_info.platform = of_device_get_match_data(&pdev->dev); |
| 1939 | |
| 1940 | indio_dev->name = dev_name(&pdev->dev); |
| 1941 | indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; |
| 1942 | indio_dev->info = &at91_adc_info; |
| 1943 | indio_dev->channels = *st->soc_info.platform->adc_channels; |
| 1944 | indio_dev->num_channels = st->soc_info.platform->max_channels; |
| 1945 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1946 | bitmap_set(&st->touch_st.channels_bitmask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1947 | st->soc_info.platform->touch_chan_x, 1); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1948 | bitmap_set(&st->touch_st.channels_bitmask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1949 | st->soc_info.platform->touch_chan_y, 1); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1950 | bitmap_set(&st->touch_st.channels_bitmask, |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1951 | st->soc_info.platform->touch_chan_p, 1); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 1952 | |
Eugen Hristev | 6794e23 | 2018-06-21 10:56:21 +0300 | [diff] [blame] | 1953 | st->oversampling_ratio = AT91_OSR_1SAMPLES; |
| 1954 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 1955 | ret = of_property_read_u32(pdev->dev.of_node, |
| 1956 | "atmel,min-sample-rate-hz", |
| 1957 | &st->soc_info.min_sample_rate); |
| 1958 | if (ret) { |
| 1959 | dev_err(&pdev->dev, |
| 1960 | "invalid or missing value for atmel,min-sample-rate-hz\n"); |
| 1961 | return ret; |
| 1962 | } |
| 1963 | |
| 1964 | ret = of_property_read_u32(pdev->dev.of_node, |
| 1965 | "atmel,max-sample-rate-hz", |
| 1966 | &st->soc_info.max_sample_rate); |
| 1967 | if (ret) { |
| 1968 | dev_err(&pdev->dev, |
| 1969 | "invalid or missing value for atmel,max-sample-rate-hz\n"); |
| 1970 | return ret; |
| 1971 | } |
| 1972 | |
| 1973 | ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms", |
| 1974 | &st->soc_info.startup_time); |
| 1975 | if (ret) { |
| 1976 | dev_err(&pdev->dev, |
| 1977 | "invalid or missing value for atmel,startup-time-ms\n"); |
| 1978 | return ret; |
| 1979 | } |
| 1980 | |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1981 | ret = of_property_read_u32(pdev->dev.of_node, |
| 1982 | "atmel,trigger-edge-type", &edge_type); |
| 1983 | if (ret) { |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 1984 | dev_dbg(&pdev->dev, |
| 1985 | "atmel,trigger-edge-type not specified, only software trigger available\n"); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | st->selected_trig = NULL; |
| 1989 | |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 1990 | /* find the right trigger, or no trigger at all */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 1991 | for (i = 0; i < st->soc_info.platform->hw_trig_cnt + 1; i++) |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 1992 | if (at91_adc_trigger_list[i].edge_type == edge_type) { |
| 1993 | st->selected_trig = &at91_adc_trigger_list[i]; |
| 1994 | break; |
| 1995 | } |
| 1996 | |
| 1997 | if (!st->selected_trig) { |
| 1998 | dev_err(&pdev->dev, "invalid external trigger edge value\n"); |
| 1999 | return -EINVAL; |
| 2000 | } |
| 2001 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2002 | init_waitqueue_head(&st->wq_data_available); |
| 2003 | mutex_init(&st->lock); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2004 | INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2005 | |
Wang ShaoBo | cbc4ca3 | 2020-09-18 16:28:37 +0800 | [diff] [blame] | 2006 | st->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); |
| 2007 | if (IS_ERR(st->base)) |
| 2008 | return PTR_ERR(st->base); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2009 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 2010 | /* if we plan to use DMA, we need the physical address of the regs */ |
| 2011 | st->dma_st.phys_addr = res->start; |
| 2012 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2013 | st->irq = platform_get_irq(pdev, 0); |
| 2014 | if (st->irq <= 0) { |
| 2015 | if (!st->irq) |
| 2016 | st->irq = -ENXIO; |
| 2017 | |
| 2018 | return st->irq; |
| 2019 | } |
| 2020 | |
| 2021 | st->per_clk = devm_clk_get(&pdev->dev, "adc_clk"); |
| 2022 | if (IS_ERR(st->per_clk)) |
| 2023 | return PTR_ERR(st->per_clk); |
| 2024 | |
| 2025 | st->reg = devm_regulator_get(&pdev->dev, "vddana"); |
| 2026 | if (IS_ERR(st->reg)) |
| 2027 | return PTR_ERR(st->reg); |
| 2028 | |
| 2029 | st->vref = devm_regulator_get(&pdev->dev, "vref"); |
| 2030 | if (IS_ERR(st->vref)) |
| 2031 | return PTR_ERR(st->vref); |
| 2032 | |
| 2033 | ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0, |
| 2034 | pdev->dev.driver->name, indio_dev); |
| 2035 | if (ret) |
| 2036 | return ret; |
| 2037 | |
| 2038 | ret = regulator_enable(st->reg); |
| 2039 | if (ret) |
| 2040 | return ret; |
| 2041 | |
| 2042 | ret = regulator_enable(st->vref); |
| 2043 | if (ret) |
| 2044 | goto reg_disable; |
| 2045 | |
| 2046 | st->vref_uv = regulator_get_voltage(st->vref); |
| 2047 | if (st->vref_uv <= 0) { |
| 2048 | ret = -EINVAL; |
| 2049 | goto vref_disable; |
| 2050 | } |
| 2051 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2052 | ret = clk_prepare_enable(st->per_clk); |
| 2053 | if (ret) |
| 2054 | goto vref_disable; |
| 2055 | |
Eugen Hristev | eaefa15 | 2021-09-01 15:30:05 +0300 | [diff] [blame] | 2056 | at91_adc_hw_init(indio_dev); |
| 2057 | |
Marek Vasut | 8e6cb47 | 2016-04-18 18:30:05 +0200 | [diff] [blame] | 2058 | platform_set_drvdata(pdev, indio_dev); |
| 2059 | |
Alexandru Ardelean | 789976a | 2020-09-29 15:59:42 +0300 | [diff] [blame] | 2060 | ret = at91_adc_buffer_and_trigger_init(&pdev->dev, indio_dev); |
| 2061 | if (ret < 0) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2062 | goto per_clk_disable_unprepare; |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 2063 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 2064 | if (dma_coerce_mask_and_coherent(&indio_dev->dev, DMA_BIT_MASK(32))) |
| 2065 | dev_info(&pdev->dev, "cannot set DMA mask to 32-bit\n"); |
| 2066 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2067 | ret = iio_device_register(indio_dev); |
| 2068 | if (ret < 0) |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 2069 | goto dma_disable; |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2070 | |
Eugen Hristev | ca4c302 | 2017-10-11 14:21:14 +0300 | [diff] [blame] | 2071 | if (st->selected_trig->hw_trig) |
| 2072 | dev_info(&pdev->dev, "setting up trigger as %s\n", |
| 2073 | st->selected_trig->name); |
Eugen Hristev | 5e1a1da | 2017-06-15 16:24:57 +0300 | [diff] [blame] | 2074 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2075 | dev_info(&pdev->dev, "version: %x\n", |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 2076 | readl_relaxed(st->base + st->soc_info.platform->layout->VERSION)); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2077 | |
| 2078 | return 0; |
| 2079 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 2080 | dma_disable: |
| 2081 | at91_adc_dma_disable(pdev); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2082 | per_clk_disable_unprepare: |
| 2083 | clk_disable_unprepare(st->per_clk); |
| 2084 | vref_disable: |
| 2085 | regulator_disable(st->vref); |
| 2086 | reg_disable: |
| 2087 | regulator_disable(st->reg); |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | static int at91_adc_remove(struct platform_device *pdev) |
| 2092 | { |
| 2093 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
| 2094 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 2095 | |
| 2096 | iio_device_unregister(indio_dev); |
| 2097 | |
Eugen Hristev | 073c662 | 2017-11-15 14:56:47 +0200 | [diff] [blame] | 2098 | at91_adc_dma_disable(pdev); |
| 2099 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2100 | clk_disable_unprepare(st->per_clk); |
| 2101 | |
| 2102 | regulator_disable(st->vref); |
| 2103 | regulator_disable(st->reg); |
| 2104 | |
| 2105 | return 0; |
| 2106 | } |
| 2107 | |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2108 | static __maybe_unused int at91_adc_suspend(struct device *dev) |
| 2109 | { |
Kefeng Wang | e3faedf | 2019-04-23 15:50:03 +0800 | [diff] [blame] | 2110 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2111 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 2112 | |
| 2113 | /* |
| 2114 | * Do a sofware reset of the ADC before we go to suspend. |
| 2115 | * this will ensure that all pins are free from being muxed by the ADC |
| 2116 | * and can be used by for other devices. |
| 2117 | * Otherwise, ADC will hog them and we can't go to suspend mode. |
| 2118 | */ |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 2119 | at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2120 | |
| 2121 | clk_disable_unprepare(st->per_clk); |
| 2122 | regulator_disable(st->vref); |
| 2123 | regulator_disable(st->reg); |
| 2124 | |
| 2125 | return pinctrl_pm_select_sleep_state(dev); |
| 2126 | } |
| 2127 | |
| 2128 | static __maybe_unused int at91_adc_resume(struct device *dev) |
| 2129 | { |
Kefeng Wang | e3faedf | 2019-04-23 15:50:03 +0800 | [diff] [blame] | 2130 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2131 | struct at91_adc_state *st = iio_priv(indio_dev); |
| 2132 | int ret; |
| 2133 | |
| 2134 | ret = pinctrl_pm_select_default_state(dev); |
| 2135 | if (ret) |
| 2136 | goto resume_failed; |
| 2137 | |
| 2138 | ret = regulator_enable(st->reg); |
| 2139 | if (ret) |
| 2140 | goto resume_failed; |
| 2141 | |
| 2142 | ret = regulator_enable(st->vref); |
| 2143 | if (ret) |
| 2144 | goto reg_disable_resume; |
| 2145 | |
| 2146 | ret = clk_prepare_enable(st->per_clk); |
| 2147 | if (ret) |
| 2148 | goto vref_disable_resume; |
| 2149 | |
Alexandru Ardelean | ebf35aa | 2020-05-25 13:53:41 +0300 | [diff] [blame] | 2150 | at91_adc_hw_init(indio_dev); |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2151 | |
| 2152 | /* reconfiguring trigger hardware state */ |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2153 | if (!iio_buffer_enabled(indio_dev)) |
| 2154 | return 0; |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2155 | |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2156 | /* check if we are enabling triggered buffer or the touchscreen */ |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 2157 | if (at91_adc_current_chan_is_touch(indio_dev)) |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2158 | return at91_adc_configure_touch(st, true); |
Alexandru Ardelean | 065056c | 2020-03-04 10:42:18 +0200 | [diff] [blame] | 2159 | else |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2160 | return at91_adc_configure_trigger(st->trig, true); |
Eugen Hristev | 23ec277 | 2018-05-22 10:52:35 +0300 | [diff] [blame] | 2161 | |
| 2162 | /* not needed but more explicit */ |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2163 | return 0; |
| 2164 | |
| 2165 | vref_disable_resume: |
| 2166 | regulator_disable(st->vref); |
| 2167 | reg_disable_resume: |
| 2168 | regulator_disable(st->reg); |
| 2169 | resume_failed: |
| 2170 | dev_err(&indio_dev->dev, "failed to resume\n"); |
| 2171 | return ret; |
| 2172 | } |
| 2173 | |
| 2174 | static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume); |
| 2175 | |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2176 | static const struct of_device_id at91_adc_dt_match[] = { |
| 2177 | { |
| 2178 | .compatible = "atmel,sama5d2-adc", |
Eugen Hristev | 8940de2 | 2021-09-01 15:30:07 +0300 | [diff] [blame] | 2179 | .data = (const void *)&sama5d2_platform, |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2180 | }, { |
Eugen Hristev | 840bf6c | 2021-09-01 15:30:10 +0300 | [diff] [blame] | 2181 | .compatible = "microchip,sama7g5-adc", |
| 2182 | .data = (const void *)&sama7g5_platform, |
| 2183 | }, { |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2184 | /* sentinel */ |
| 2185 | } |
| 2186 | }; |
| 2187 | MODULE_DEVICE_TABLE(of, at91_adc_dt_match); |
| 2188 | |
| 2189 | static struct platform_driver at91_adc_driver = { |
| 2190 | .probe = at91_adc_probe, |
| 2191 | .remove = at91_adc_remove, |
| 2192 | .driver = { |
| 2193 | .name = "at91-sama5d2_adc", |
| 2194 | .of_match_table = at91_adc_dt_match, |
Eugen Hristev | 500a2ee | 2017-06-23 15:54:57 +0300 | [diff] [blame] | 2195 | .pm = &at91_adc_pm_ops, |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2196 | }, |
| 2197 | }; |
| 2198 | module_platform_driver(at91_adc_driver) |
| 2199 | |
Eugen Hristev | 874b491 | 2021-09-01 15:30:11 +0300 | [diff] [blame] | 2200 | MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@microchip.com>"); |
| 2201 | MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com"); |
Ludovic Desroches | 27e1771 | 2016-01-14 16:38:13 +0100 | [diff] [blame] | 2202 | MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC"); |
| 2203 | MODULE_LICENSE("GPL v2"); |