blob: 92a57cf10fba4a05d797b7308404a66255263824 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Ludovic Desroches27e17712016-01-14 16:38:13 +01002/*
3 * Atmel ADC driver for SAMA5D2 devices and compatible.
4 *
5 * Copyright (C) 2015 Atmel,
6 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
Eugen Hristev874b4912021-09-01 15:30:11 +03007 * 2021 Microchip Technology, Inc. and its subsidiaries
8 * 2021 Eugen Hristev <eugen.hristev@microchip.com>
Ludovic Desroches27e17712016-01-14 16:38:13 +01009 */
10
11#include <linux/bitops.h>
12#include <linux/clk.h>
Eugen Hristev97c54cf2020-01-28 12:57:40 +000013#include <linux/delay.h>
Eugen Hristev073c6622017-11-15 14:56:47 +020014#include <linux/dma-mapping.h>
15#include <linux/dmaengine.h>
Ludovic Desroches27e17712016-01-14 16:38:13 +010016#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 Hristev5e1a1da2017-06-15 16:24:57 +030025#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 Hristev500a2ee2017-06-23 15:54:57 +030029#include <linux/pinctrl/consumer.h>
Ludovic Desroches27e17712016-01-14 16:38:13 +010030#include <linux/regulator/consumer.h>
31
Eugen Hristev8940de22021-09-01 15:30:07 +030032struct at91_adc_reg_layout {
Ludovic Desroches27e17712016-01-14 16:38:13 +010033/* Control Register */
Eugen Hristev8940de22021-09-01 15:30:07 +030034 u16 CR;
Ludovic Desroches27e17712016-01-14 16:38:13 +010035/* 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 Hristev8940de22021-09-01 15:30:07 +030045 u16 MR;
Ludovic Desroches27e17712016-01-14 16:38:13 +010046/* 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 Desroches94b24232016-03-22 17:08:45 +010072#define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
Ludovic Desroches27e17712016-01-14 16:38:13 +010073/* Startup Time */
74#define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
Ludovic Desroches94b24232016-03-22 17:08:45 +010075#define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
Ludovic Desroches27e17712016-01-14 16:38:13 +010076/* 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 Hristev8940de22021-09-01 15:30:07 +030088 u16 SEQR1;
Ludovic Desroches27e17712016-01-14 16:38:13 +010089/* Channel Sequence Register 2 */
Eugen Hristev8940de22021-09-01 15:30:07 +030090 u16 SEQR2;
Ludovic Desroches27e17712016-01-14 16:38:13 +010091/* Channel Enable Register */
Eugen Hristev8940de22021-09-01 15:30:07 +030092 u16 CHER;
Ludovic Desroches27e17712016-01-14 16:38:13 +010093/* Channel Disable Register */
Eugen Hristev8940de22021-09-01 15:30:07 +030094 u16 CHDR;
Ludovic Desroches27e17712016-01-14 16:38:13 +010095/* Channel Status Register */
Eugen Hristev8940de22021-09-01 15:30:07 +030096 u16 CHSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +010097/* Last Converted Data Register */
Eugen Hristev8940de22021-09-01 15:30:07 +030098 u16 LCDR;
Ludovic Desroches27e17712016-01-14 16:38:13 +010099/* Interrupt Enable Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300100 u16 IER;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300101/* 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 Hristev97c54cf2020-01-28 12:57:40 +0000107/* Interrupt Enable Register - Data ready */
108#define AT91_SAMA5D2_IER_DRDY BIT(24)
Eugen Hristev073c6622017-11-15 14:56:47 +0200109/* Interrupt Enable Register - general overrun error */
110#define AT91_SAMA5D2_IER_GOVRE BIT(25)
Eugen Hristev23ec2772018-05-22 10:52:35 +0300111/* 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 Hristev8940de22021-09-01 15:30:07 +0300115
Ludovic Desroches27e17712016-01-14 16:38:13 +0100116/* Interrupt Disable Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300117 u16 IDR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100118/* Interrupt Mask Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300119 u16 IMR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100120/* Interrupt Status Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300121 u16 ISR;
Eugen Hristeve6d5eee2021-09-01 15:30:08 +0300122/* 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 Hristev23ec2772018-05-22 10:52:35 +0300130/* Interrupt Status Register - Pen touching sense status */
131#define AT91_SAMA5D2_ISR_PENS BIT(31)
Ludovic Desroches27e17712016-01-14 16:38:13 +0100132/* Last Channel Trigger Mode Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300133 u16 LCTMR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100134/* Last Channel Compare Window Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300135 u16 LCCWR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100136/* Overrun Status Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300137 u16 OVER;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100138/* Extended Mode Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300139 u16 EMR;
Eugen Hristev6794e232018-06-21 10:56:21 +0300140/* 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 Desrochesd6511322016-03-22 17:08:46 +0100149
Eugen Hristev8940de22021-09-01 15:30:07 +0300150/* Compare Window Register */
151 u16 CWR;
152/* Channel Gain Register */
153 u16 CGR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100154/* Channel Offset Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300155 u16 COR;
Eugen Hristevd8004c52021-09-01 15:30:09 +0300156/* Channel Offset Register differential offset - constant, not a register */
157 u16 COR_diff_offset;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100158/* Analog Control Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300159 u16 ACR;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300160/* Analog Control Register - Pen detect sensitivity mask */
161#define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0)
162
Ludovic Desroches27e17712016-01-14 16:38:13 +0100163/* Touchscreen Mode Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300164 u16 TSMR;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300165/* 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 Desroches27e17712016-01-14 16:38:13 +0100192/* Touchscreen X Position Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300193 u16 XPOSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100194/* Touchscreen Y Position Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300195 u16 YPOSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100196/* Touchscreen Pressure Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300197 u16 PRESSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100198/* Trigger Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300199 u16 TRGR;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300200/* 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 Hristev23ec2772018-05-22 10:52:35 +0300210/* 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 Hristev5e1a1da2017-06-15 16:24:57 +0300216
Ludovic Desroches27e17712016-01-14 16:38:13 +0100217/* Correction Select Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300218 u16 COSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100219/* Correction Value Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300220 u16 CVR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100221/* Channel Error Correction Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300222 u16 CECR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100223/* Write Protection Mode Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300224 u16 WPMR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100225/* Write Protection Status Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300226 u16 WPSR;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100227/* Version Register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300228 u16 VERSION;
229};
Ludovic Desroches27e17712016-01-14 16:38:13 +0100230
Eugen Hristev8940de22021-09-01 15:30:07 +0300231static 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 Hristevd8004c52021-09-01 15:30:09 +0300251 .COR_diff_offset = 16,
Eugen Hristev8940de22021-09-01 15:30:07 +0300252 .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 Hristev23ec2772018-05-22 10:52:35 +0300265
Eugen Hristev840bf6c2021-09-01 15:30:10 +0300266static 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 Hristev23ec2772018-05-22 10:52:35 +0300298#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 Hristev073c6622017-11-15 14:56:47 +0200305#define AT91_HWFIFO_MAX_SIZE_STR "128"
306#define AT91_HWFIFO_MAX_SIZE 128
307
Eugen Hristev6794e232018-06-21 10:56:21 +0300308/* 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 Hristev8940de22021-09-01 15:30:07 +0300313#define AT91_SAMA5D2_CHAN_SINGLE(index, num, addr) \
Ludovic Desroches27e17712016-01-14 16:38:13 +0100314 { \
315 .type = IIO_VOLTAGE, \
316 .channel = num, \
317 .address = addr, \
Eugen Hristev8940de22021-09-01 15:30:07 +0300318 .scan_index = index, \
Ludovic Desroches27e17712016-01-14 16:38:13 +0100319 .scan_type = { \
320 .sign = 'u', \
Eugen Hristev6794e232018-06-21 10:56:21 +0300321 .realbits = 14, \
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300322 .storagebits = 16, \
Ludovic Desroches27e17712016-01-14 16:38:13 +0100323 }, \
324 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
325 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Eugen Hristev6794e232018-06-21 10:56:21 +0300326 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
327 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Ludovic Desroches27e17712016-01-14 16:38:13 +0100328 .datasheet_name = "CH"#num, \
329 .indexed = 1, \
330 }
331
Eugen Hristev8940de22021-09-01 15:30:07 +0300332#define AT91_SAMA5D2_CHAN_DIFF(index, num, num2, addr) \
Ludovic Desrochesd6511322016-03-22 17:08:46 +0100333 { \
334 .type = IIO_VOLTAGE, \
335 .differential = 1, \
336 .channel = num, \
337 .channel2 = num2, \
338 .address = addr, \
Eugen Hristev8940de22021-09-01 15:30:07 +0300339 .scan_index = index, \
Ludovic Desrochesd6511322016-03-22 17:08:46 +0100340 .scan_type = { \
341 .sign = 's', \
Eugen Hristev6794e232018-06-21 10:56:21 +0300342 .realbits = 14, \
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300343 .storagebits = 16, \
Ludovic Desrochesd6511322016-03-22 17:08:46 +0100344 }, \
345 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
346 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Eugen Hristev6794e232018-06-21 10:56:21 +0300347 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
348 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Ludovic Desrochesd6511322016-03-22 17:08:46 +0100349 .datasheet_name = "CH"#num"-CH"#num2, \
350 .indexed = 1, \
351 }
352
Eugen Hristev23ec2772018-05-22 10:52:35 +0300353#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 Hristev6794e232018-06-21 10:56:21 +0300366 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
367 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Eugen Hristev23ec2772018-05-22 10:52:35 +0300368 .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 Hristev6794e232018-06-21 10:56:21 +0300381 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
382 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
Eugen Hristev23ec2772018-05-22 10:52:35 +0300383 .datasheet_name = name, \
384 }
385
Eugen Hristev8940de22021-09-01 15:30:07 +0300386#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 Desroches27e17712016-01-14 16:38:13 +0100392
Eugen Hristev8940de22021-09-01 15:30:07 +0300393/**
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 */
407struct 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 Desroches27e17712016-01-14 16:38:13 +0100426struct at91_adc_soc_info {
427 unsigned startup_time;
428 unsigned min_sample_rate;
429 unsigned max_sample_rate;
Eugen Hristev8940de22021-09-01 15:30:07 +0300430 const struct at91_adc_platform *platform;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100431};
432
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300433struct at91_adc_trigger {
434 char *name;
435 unsigned int trgmod_value;
436 unsigned int edge_type;
Eugen Hristevca4c3022017-10-11 14:21:14 +0300437 bool hw_trig;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300438};
439
Eugen Hristev073c6622017-11-15 14:56:47 +0200440/**
Lee Jones62eebcb2020-07-17 17:55:22 +0100441 * struct at91_adc_dma - at91-sama5d2 dma information struct
Eugen Hristev073c6622017-11-15 14:56:47 +0200442 * @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 */
451struct 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 Hristev23ec2772018-05-22 10:52:35 +0300462/**
Lee Jones62eebcb2020-07-17 17:55:22 +0100463 * struct at91_adc_touch - at91-sama5d2 touchscreen information struct
Eugen Hristev23ec2772018-05-22 10:52:35 +0300464 * @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 */
470struct 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 Hristev8940de22021-09-01 15:30:07 +0300478/*
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 Desroches27e17712016-01-14 16:38:13 +0100487struct at91_adc_state {
488 void __iomem *base;
489 int irq;
490 struct clk *per_clk;
491 struct regulator *reg;
492 struct regulator *vref;
Ludovic Desrochesd7bdcc32016-01-18 09:41:55 +0100493 int vref_uv;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300494 unsigned int current_sample_rate;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300495 struct iio_trigger *trig;
496 const struct at91_adc_trigger *selected_trig;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100497 const struct iio_chan_spec *chan;
498 bool conversion_done;
499 u32 conversion_value;
Eugen Hristev6794e232018-06-21 10:56:21 +0300500 unsigned int oversampling_ratio;
Ludovic Desroches27e17712016-01-14 16:38:13 +0100501 struct at91_adc_soc_info soc_info;
502 wait_queue_head_t wq_data_available;
Eugen Hristev073c6622017-11-15 14:56:47 +0200503 struct at91_adc_dma dma_st;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300504 struct at91_adc_touch touch_st;
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +0300505 struct iio_dev *indio_dev;
Jonathan Cameron8f884752021-06-13 16:22:54 +0100506 /* Ensure naturally aligned timestamp */
507 u16 buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8);
Ludovic Desroches27e17712016-01-14 16:38:13 +0100508 /*
509 * lock to prevent concurrent 'single conversion' requests through
510 * sysfs.
511 */
512 struct mutex lock;
513};
514
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300515static 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 Hristevca4c3022017-10-11 14:21:14 +0300520 .hw_trig = true,
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300521 },
522 {
523 .name = "external_falling",
524 .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
525 .edge_type = IRQ_TYPE_EDGE_FALLING,
Eugen Hristevca4c3022017-10-11 14:21:14 +0300526 .hw_trig = true,
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300527 },
528 {
529 .name = "external_any",
530 .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
531 .edge_type = IRQ_TYPE_EDGE_BOTH,
Eugen Hristevca4c3022017-10-11 14:21:14 +0300532 .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 Hristev5e1a1da2017-06-15 16:24:57 +0300539 },
540};
541
Eugen Hristev8940de22021-09-01 15:30:07 +0300542static 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 Hristev840bf6c2021-09-01 15:30:10 +0300568static 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 Hristev8940de22021-09-01 15:30:07 +0300596static 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 Desroches27e17712016-01-14 16:38:13 +0100615};
616
Eugen Hristev840bf6c2021-09-01 15:30:10 +0300617static 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 Hristevf0c8d1f2018-04-10 11:57:47 +0300632static 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
643static inline struct iio_chan_spec const *
644at91_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 Hristev23ec2772018-05-22 10:52:35 +0300653static 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 Hristev97c54cf2020-01-28 12:57:40 +0000659static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev *indio_dev)
660{
661 u32 mask = 0;
662 u8 bit;
Eugen Hristev8940de22021-09-01 15:30:07 +0300663 struct at91_adc_state *st = iio_priv(indio_dev);
Eugen Hristev97c54cf2020-01-28 12:57:40 +0000664
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 Hristev8940de22021-09-01 15:30:07 +0300672 return mask & GENMASK(st->soc_info.platform->nr_channels, 0);
Eugen Hristev97c54cf2020-01-28 12:57:40 +0000673}
674
Eugen Hristevd8004c52021-09-01 15:30:09 +0300675static 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 Hristeve6d5eee2021-09-01 15:30:08 +0300690static 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
700static 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
709static 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
720static 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 Hristev6794e232018-06-21 10:56:21 +0300728static void at91_adc_config_emr(struct at91_adc_state *st)
729{
730 /* configure the extended mode register */
Eugen Hristev8940de22021-09-01 15:30:07 +0300731 unsigned int emr = at91_adc_readl(st, EMR);
Eugen Hristev6794e232018-06-21 10:56:21 +0300732
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 Hristev8940de22021-09-01 15:30:07 +0300755 at91_adc_writel(st, EMR, emr);
Eugen Hristev6794e232018-06-21 10:56:21 +0300756}
757
758static 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
777static 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 Hristev23ec2772018-05-22 10:52:35 +0300799static 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 Hristev8940de22021-09-01 15:30:07 +0300808 at91_adc_writel(st, IDR,
Eugen Hristev23ec2772018-05-22 10:52:35 +0300809 AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN);
Eugen Hristev8940de22021-09-01 15:30:07 +0300810 at91_adc_writel(st, TSMR, 0);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300811 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 Hristev8940de22021-09-01 15:30:07 +0300836 at91_adc_writel(st, TSMR, tsmr);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300837
Eugen Hristev8940de22021-09-01 15:30:07 +0300838 acr = at91_adc_readl(st, ACR);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300839 acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK;
840 acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK;
Eugen Hristev8940de22021-09-01 15:30:07 +0300841 at91_adc_writel(st, ACR, acr);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300842
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 Hristev8940de22021-09-01 15:30:07 +0300848 at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300849
850 return 0;
851}
852
853static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg)
854{
Eugen Hristev8940de22021-09-01 15:30:07 +0300855 u32 val = 0;
Eugen Hristev23ec2772018-05-22 10:52:35 +0300856 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 Hristev8940de22021-09-01 15:30:07 +0300864 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 Hristev23ec2772018-05-22 10:52:35 +0300869 if (!val)
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +0300870 dev_dbg(&st->indio_dev->dev, "pos is 0\n");
Eugen Hristev23ec2772018-05-22 10:52:35 +0300871
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 Ardeleanebf35aa2020-05-25 13:53:41 +0300876 dev_err(&st->indio_dev->dev, "scale is 0\n");
Eugen Hristev23ec2772018-05-22 10:52:35 +0300877 return 0;
878 }
879 result /= scale;
880
881 return result;
882}
883
884static u16 at91_adc_touch_x_pos(struct at91_adc_state *st)
885{
Eugen Hristev8940de22021-09-01 15:30:07 +0300886 st->touch_st.x_pos = at91_adc_touch_pos(st, st->soc_info.platform->layout->XPOSR);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300887 return st->touch_st.x_pos;
888}
889
890static u16 at91_adc_touch_y_pos(struct at91_adc_state *st)
891{
Eugen Hristev8940de22021-09-01 15:30:07 +0300892 return at91_adc_touch_pos(st, st->soc_info.platform->layout->YPOSR);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300893}
894
895static 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 Hristev8940de22021-09-01 15:30:07 +0300904 val = at91_adc_readl(st, PRESSR);
Eugen Hristev23ec2772018-05-22 10:52:35 +0300905 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
923static 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 Hristev8940de22021-09-01 15:30:07 +0300928 if (chan == st->soc_info.platform->touch_chan_x)
Eugen Hristev23ec2772018-05-22 10:52:35 +0300929 *val = at91_adc_touch_x_pos(st);
Eugen Hristev8940de22021-09-01 15:30:07 +0300930 else if (chan == st->soc_info.platform->touch_chan_y)
Eugen Hristev23ec2772018-05-22 10:52:35 +0300931 *val = at91_adc_touch_y_pos(st);
932 else
933 return -ENODATA;
934
935 return IIO_VAL_INT;
936}
937
938static 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 Hristev8940de22021-09-01 15:30:07 +0300943 if (chan == st->soc_info.platform->touch_chan_p)
Eugen Hristev23ec2772018-05-22 10:52:35 +0300944 *val = at91_adc_touch_pressure(st);
945 else
946 return -ENODATA;
947
948 return IIO_VAL_INT;
949}
950
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300951static 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 Hristev8940de22021-09-01 15:30:07 +0300955 u32 status = at91_adc_readl(st, TRGR);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300956
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 Hristev8940de22021-09-01 15:30:07 +0300964 at91_adc_writel(st, TRGR, status);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300965
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300966 return 0;
967}
968
Jonathan Cameroneca85232020-09-20 14:25:48 +0100969static void at91_adc_reenable_trigger(struct iio_trigger *trig)
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300970{
971 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
972 struct at91_adc_state *st = iio_priv(indio);
973
Eugen Hristev073c6622017-11-15 14:56:47 +0200974 /* if we are using DMA, we must not reenable irq after each trigger */
975 if (st->dma_st.dma_chan)
Jonathan Cameroneca85232020-09-20 14:25:48 +0100976 return;
Eugen Hristev073c6622017-11-15 14:56:47 +0200977
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300978 enable_irq(st->irq);
979
980 /* Needed to ACK the DRDY interruption */
Eugen Hristev8940de22021-09-01 15:30:07 +0300981 at91_adc_readl(st, LCDR);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300982}
983
984static const struct iio_trigger_ops at91_adc_trigger_ops = {
Eugen Hristev5e1a1da2017-06-15 16:24:57 +0300985 .set_trigger_state = &at91_adc_configure_trigger,
Jonathan Cameroneca85232020-09-20 14:25:48 +0100986 .reenable = &at91_adc_reenable_trigger,
Eugen Hristev073c6622017-11-15 14:56:47 +0200987 .validate_device = iio_trigger_validate_own_device,
988};
989
990static 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
1013static 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
1020static 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 Hristevf0c8d1f2018-04-10 11:57:47 +03001042 struct iio_chan_spec const *chan =
1043 at91_adc_chan_get(indio_dev, bit);
1044
1045 if (!chan)
1046 continue;
Eugen Hristev073c6622017-11-15 14:56:47 +02001047
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 Hristev8940de22021-09-01 15:30:07 +03001076 at91_adc_writel(st, IER, AT91_SAMA5D2_IER_GOVRE);
Eugen Hristev073c6622017-11-15 14:56:47 +02001077 /* 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 Hristevabb7e842020-01-28 12:57:41 +00001088static 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 Ardelean065056c2020-03-04 10:42:18 +02001100static 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 Hristev8940de22021-09-01 15:30:07 +03001106 st->soc_info.platform->max_index + 1);
Alexandru Ardelean065056c2020-03-04 10:42:18 +02001107}
1108
Eugen Hristev1a198792020-09-23 15:17:48 +03001109static int at91_adc_buffer_prepare(struct iio_dev *indio_dev)
Eugen Hristev073c6622017-11-15 14:56:47 +02001110{
1111 int ret;
Eugen Hristevabb7e842020-01-28 12:57:41 +00001112 u8 bit;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001113 struct at91_adc_state *st = iio_priv(indio_dev);
Eugen Hristev073c6622017-11-15 14:56:47 +02001114
Eugen Hristev23ec2772018-05-22 10:52:35 +03001115 /* check if we are enabling triggered buffer or the touchscreen */
Alexandru Ardelean065056c2020-03-04 10:42:18 +02001116 if (at91_adc_current_chan_is_touch(indio_dev))
Eugen Hristev23ec2772018-05-22 10:52:35 +03001117 return at91_adc_configure_touch(st, true);
Alexandru Ardelean065056c2020-03-04 10:42:18 +02001118
Eugen Hristev23ec2772018-05-22 10:52:35 +03001119 /* 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 Hristev073c6622017-11-15 14:56:47 +02001124 ret = at91_adc_dma_start(indio_dev);
1125 if (ret) {
Eugen Hristev1a198792020-09-23 15:17:48 +03001126 dev_err(&indio_dev->dev, "buffer prepare failed\n");
Eugen Hristev073c6622017-11-15 14:56:47 +02001127 return ret;
1128 }
1129
Eugen Hristevabb7e842020-01-28 12:57:41 +00001130 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 Hristevabb7e842020-01-28 12:57:41 +00001134 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 Hristevd8004c52021-09-01 15:30:09 +03001141 at91_adc_cor(st, chan);
Eugen Hristevabb7e842020-01-28 12:57:41 +00001142
Eugen Hristev8940de22021-09-01 15:30:07 +03001143 at91_adc_writel(st, CHER, BIT(chan->channel));
Eugen Hristevabb7e842020-01-28 12:57:41 +00001144 }
1145
1146 if (at91_adc_buffer_check_use_irq(indio_dev, st))
Eugen Hristev8940de22021-09-01 15:30:07 +03001147 at91_adc_writel(st, IER, AT91_SAMA5D2_IER_DRDY);
Eugen Hristevabb7e842020-01-28 12:57:41 +00001148
Alexandru Ardeleanf3c034f2020-03-04 10:42:19 +02001149 return 0;
1150}
1151
Alexandru Ardeleanf3c034f2020-03-04 10:42:19 +02001152static int at91_adc_buffer_postdisable(struct iio_dev *indio_dev)
Eugen Hristev073c6622017-11-15 14:56:47 +02001153{
1154 struct at91_adc_state *st = iio_priv(indio_dev);
Eugen Hristev073c6622017-11-15 14:56:47 +02001155 u8 bit;
1156
Eugen Hristev23ec2772018-05-22 10:52:35 +03001157 /* check if we are disabling triggered buffer or the touchscreen */
Alexandru Ardelean065056c2020-03-04 10:42:18 +02001158 if (at91_adc_current_chan_is_touch(indio_dev))
Eugen Hristev23ec2772018-05-22 10:52:35 +03001159 return at91_adc_configure_touch(st, false);
Alexandru Ardelean065056c2020-03-04 10:42:18 +02001160
Eugen Hristev23ec2772018-05-22 10:52:35 +03001161 /* 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 Hristev073c6622017-11-15 14:56:47 +02001165 /*
Eugen Hristevabb7e842020-01-28 12:57:41 +00001166 * For each enable channel we must disable it in hardware.
1167 * In the case of DMA, we must read the last converted value
Eugen Hristev073c6622017-11-15 14:56:47 +02001168 * to clear EOC status and not get a possible interrupt later.
Eugen Hristevabb7e842020-01-28 12:57:41 +00001169 * This value is being read by DMA from LCDR anyway, so it's not lost.
Eugen Hristev073c6622017-11-15 14:56:47 +02001170 */
1171 for_each_set_bit(bit, indio_dev->active_scan_mask,
1172 indio_dev->num_channels) {
Eugen Hristevf0c8d1f2018-04-10 11:57:47 +03001173 struct iio_chan_spec const *chan =
1174 at91_adc_chan_get(indio_dev, bit);
Eugen Hristev073c6622017-11-15 14:56:47 +02001175
Eugen Hristevf0c8d1f2018-04-10 11:57:47 +03001176 if (!chan)
1177 continue;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001178 /* these channel types are virtual, no need to do anything */
1179 if (chan->type == IIO_POSITIONRELATIVE ||
1180 chan->type == IIO_PRESSURE)
1181 continue;
Eugen Hristevabb7e842020-01-28 12:57:41 +00001182
Eugen Hristev8940de22021-09-01 15:30:07 +03001183 at91_adc_writel(st, CHDR, BIT(chan->channel));
Eugen Hristevabb7e842020-01-28 12:57:41 +00001184
Eugen Hristev073c6622017-11-15 14:56:47 +02001185 if (st->dma_st.dma_chan)
Eugen Hristev8940de22021-09-01 15:30:07 +03001186 at91_adc_read_chan(st, chan->address);
Eugen Hristev073c6622017-11-15 14:56:47 +02001187 }
1188
Eugen Hristevabb7e842020-01-28 12:57:41 +00001189 if (at91_adc_buffer_check_use_irq(indio_dev, st))
Eugen Hristev8940de22021-09-01 15:30:07 +03001190 at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_DRDY);
Eugen Hristevabb7e842020-01-28 12:57:41 +00001191
Eugen Hristev073c6622017-11-15 14:56:47 +02001192 /* read overflow register to clear possible overflow status */
Eugen Hristev8940de22021-09-01 15:30:07 +03001193 at91_adc_readl(st, OVER);
Eugen Hristevabb7e842020-01-28 12:57:41 +00001194
Eugen Hristevabb7e842020-01-28 12:57:41 +00001195 /* 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 Ardeleanf3c034f2020-03-04 10:42:19 +02001199 return 0;
1200}
1201
Eugen Hristev073c6622017-11-15 14:56:47 +02001202static const struct iio_buffer_setup_ops at91_buffer_setup_ops = {
Alexandru Ardeleanf3c034f2020-03-04 10:42:19 +02001203 .postdisable = &at91_adc_buffer_postdisable,
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001204};
1205
1206static 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 Cameron15ea28782021-04-26 18:49:03 +01001213 iio_device_id(indio), trigger_name);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001214 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 Hristev073c6622017-11-15 14:56:47 +02001227static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
1228 struct iio_poll_func *pf)
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001229{
Eugen Hristev073c6622017-11-15 14:56:47 +02001230 struct at91_adc_state *st = iio_priv(indio_dev);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001231 int i = 0;
Eugen Hristev6794e232018-06-21 10:56:21 +03001232 int val;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001233 u8 bit;
Eugen Hristev97c54cf2020-01-28 12:57:40 +00001234 u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev);
1235 unsigned int timeout = 50;
Eugen Hristeve6d5eee2021-09-01 15:30:08 +03001236 u32 status, imr, eoc = 0, eoc_imr;
Eugen Hristev97c54cf2020-01-28 12:57:40 +00001237
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 Hristeve6d5eee2021-09-01 15:30:08 +03001242 while (((eoc & mask) != mask) && timeout) {
1243 at91_adc_irq_status(st, &status, &eoc);
1244 at91_adc_irq_mask(st, &imr, &eoc_imr);
Eugen Hristev97c54cf2020-01-28 12:57:40 +00001245 usleep_range(50, 100);
1246 timeout--;
1247 }
1248
1249 /* Cannot read data, not ready. Continue without reporting data */
1250 if (!timeout)
1251 return;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001252
Eugen Hristev073c6622017-11-15 14:56:47 +02001253 for_each_set_bit(bit, indio_dev->active_scan_mask,
1254 indio_dev->num_channels) {
Eugen Hristevf0c8d1f2018-04-10 11:57:47 +03001255 struct iio_chan_spec const *chan =
1256 at91_adc_chan_get(indio_dev, bit);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001257
Eugen Hristevf0c8d1f2018-04-10 11:57:47 +03001258 if (!chan)
1259 continue;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001260 /*
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 Hristev8940de22021-09-01 15:30:07 +03001271 val = at91_adc_read_chan(st, chan->address);
Eugen Hristev6794e232018-06-21 10:56:21 +03001272 at91_adc_adjust_val_osr(st, &val);
1273 st->buffer[i] = val;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001274 } else {
1275 st->buffer[i] = 0;
1276 WARN(true, "This trigger cannot handle this type of channel");
1277 }
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001278 i++;
1279 }
Eugen Hristev073c6622017-11-15 14:56:47 +02001280 iio_push_to_buffers_with_timestamp(indio_dev, st->buffer,
1281 pf->timestamp);
1282}
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001283
Eugen Hristev073c6622017-11-15 14:56:47 +02001284static 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 Hristev5e1a1da2017-06-15 16:24:57 +03001291
Eugen Hristev8940de22021-09-01 15:30:07 +03001292 u32 status = at91_adc_readl(st, ISR);
Eugen Hristev073c6622017-11-15 14:56:47 +02001293 /* 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 Hristev6794e232018-06-21 10:56:21 +03001309 /*
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 Hristev073c6622017-11-15 14:56:47 +02001317 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
1333static 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 Hristevabb7e842020-01-28 12:57:41 +00001339 /*
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 Hristev8940de22021-09-01 15:30:07 +03001344 at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START);
Eugen Hristevabb7e842020-01-28 12:57:41 +00001345
Eugen Hristev073c6622017-11-15 14:56:47 +02001346 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 Hristev5e1a1da2017-06-15 16:24:57 +03001352
1353 return IRQ_HANDLED;
1354}
1355
Ludovic Desroches27e17712016-01-14 16:38:13 +01001356static unsigned at91_adc_startup_time(unsigned startup_time_min,
1357 unsigned adc_clk_khz)
1358{
Colin Ian King2df331c2017-07-07 17:08:35 +01001359 static const unsigned int startup_lookup[] = {
Ludovic Desroches27e17712016-01-14 16:38:13 +01001360 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 Ardeleanebf35aa2020-05-25 13:53:41 +03001380static void at91_adc_setup_samp_freq(struct iio_dev *indio_dev, unsigned freq)
Ludovic Desroches27e17712016-01-14 16:38:13 +01001381{
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001382 struct at91_adc_state *st = iio_priv(indio_dev);
Ludovic Desroches94b24232016-03-22 17:08:45 +01001383 unsigned f_per, prescal, startup, mr;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001384
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 Hristev8940de22021-09-01 15:30:07 +03001391 mr = at91_adc_readl(st, MR);
Ludovic Desroches94b24232016-03-22 17:08:45 +01001392 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 Hristev8940de22021-09-01 15:30:07 +03001395 at91_adc_writel(st, MR, mr);
Ludovic Desroches27e17712016-01-14 16:38:13 +01001396
1397 dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
1398 freq, startup, prescal);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001399 st->current_sample_rate = freq;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001400}
1401
Eugen Hristev23ec2772018-05-22 10:52:35 +03001402static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
Ludovic Desroches27e17712016-01-14 16:38:13 +01001403{
Eugen Hristev23ec2772018-05-22 10:52:35 +03001404 return st->current_sample_rate;
1405}
Ludovic Desroches27e17712016-01-14 16:38:13 +01001406
Eugen Hristev23ec2772018-05-22 10:52:35 +03001407static 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 Desroches27e17712016-01-14 16:38:13 +01001413
Eugen Hristev23ec2772018-05-22 10:52:35 +03001414 for_each_set_bit(bit, indio_dev->active_scan_mask,
Eugen Hristev8940de22021-09-01 15:30:07 +03001415 st->soc_info.platform->max_index + 1) {
Eugen Hristev23ec2772018-05-22 10:52:35 +03001416 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
1439static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st)
1440{
Eugen Hristev8940de22021-09-01 15:30:07 +03001441 at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_PEN);
1442 at91_adc_writel(st, IER, AT91_SAMA5D2_IER_NOPEN |
Eugen Hristev23ec2772018-05-22 10:52:35 +03001443 AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
1444 AT91_SAMA5D2_IER_PRDY);
Eugen Hristev8940de22021-09-01 15:30:07 +03001445 at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC |
Eugen Hristev23ec2772018-05-22 10:52:35 +03001446 AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val));
1447 st->touch_st.touching = true;
1448}
1449
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001450static void at91_adc_no_pen_detect_interrupt(struct iio_dev *indio_dev)
Eugen Hristev23ec2772018-05-22 10:52:35 +03001451{
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001452 struct at91_adc_state *st = iio_priv(indio_dev);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001453
Eugen Hristev8940de22021-09-01 15:30:07 +03001454 at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER);
1455 at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_NOPEN |
Eugen Hristev23ec2772018-05-22 10:52:35 +03001456 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 Hristev8940de22021-09-01 15:30:07 +03001462 at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001463}
1464
1465static 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 Ardeleanebf35aa2020-05-25 13:53:41 +03001471 struct iio_dev *indio_dev = st->indio_dev;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001472
1473 iio_push_to_buffers(indio_dev, st->buffer);
Ludovic Desroches27e17712016-01-14 16:38:13 +01001474}
1475
1476static 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 Hristeve6d5eee2021-09-01 15:30:08 +03001480 u32 status, eoc, imr, eoc_imr;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001481 u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
1482 AT91_SAMA5D2_IER_PRDY;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001483
Eugen Hristeve6d5eee2021-09-01 15:30:08 +03001484 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 Hristev5e1a1da2017-06-15 16:24:57 +03001488 return IRQ_NONE;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001489 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 Ardeleanebf35aa2020-05-25 13:53:41 +03001494 at91_adc_no_pen_detect_interrupt(indio);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001495 } 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 Hristev8940de22021-09-01 15:30:07 +03001504 status = at91_adc_readl(st, XPOSR);
1505 status = at91_adc_readl(st, YPOSR);
1506 status = at91_adc_readl(st, PRESSR);
Eugen Hristev97c54cf2020-01-28 12:57:40 +00001507 } else if (iio_buffer_enabled(indio) &&
1508 (status & AT91_SAMA5D2_IER_DRDY)) {
Eugen Hristev23ec2772018-05-22 10:52:35 +03001509 /* triggered buffer without DMA */
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001510 disable_irq_nosync(irq);
1511 iio_trigger_poll(indio->trig);
Eugen Hristev073c6622017-11-15 14:56:47 +02001512 } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) {
Eugen Hristev23ec2772018-05-22 10:52:35 +03001513 /* triggered buffer with DMA - should not happen */
Eugen Hristev073c6622017-11-15 14:56:47 +02001514 disable_irq_nosync(irq);
1515 WARN(true, "Unexpected irq occurred\n");
1516 } else if (!iio_buffer_enabled(indio)) {
Eugen Hristev23ec2772018-05-22 10:52:35 +03001517 /* software requested conversion */
Eugen Hristev8940de22021-09-01 15:30:07 +03001518 st->conversion_value = at91_adc_read_chan(st, st->chan->address);
Ludovic Desroches27e17712016-01-14 16:38:13 +01001519 st->conversion_done = true;
1520 wake_up_interruptible(&st->wq_data_available);
Ludovic Desroches27e17712016-01-14 16:38:13 +01001521 }
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001522 return IRQ_HANDLED;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001523}
1524
Eugen Hristev23ec2772018-05-22 10:52:35 +03001525static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
1526 struct iio_chan_spec const *chan, int *val)
Ludovic Desroches27e17712016-01-14 16:38:13 +01001527{
1528 struct at91_adc_state *st = iio_priv(indio_dev);
Dan Carpentera176ba32018-07-09 14:06:59 +03001529 u16 tmp_val;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001530 int ret;
1531
Eugen Hristev23ec2772018-05-22 10:52:35 +03001532 /*
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 Hristev5e1a1da2017-06-15 16:24:57 +03001537 ret = iio_device_claim_direct_mode(indio_dev);
1538 if (ret)
1539 return ret;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001540 mutex_lock(&st->lock);
1541
Eugen Hristev23ec2772018-05-22 10:52:35 +03001542 ret = at91_adc_read_position(st, chan->channel,
Dan Carpentera176ba32018-07-09 14:06:59 +03001543 &tmp_val);
1544 *val = tmp_val;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001545 mutex_unlock(&st->lock);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001546 iio_device_release_direct_mode(indio_dev);
Ludovic Desroches27e17712016-01-14 16:38:13 +01001547
Eugen Hristev6794e232018-06-21 10:56:21 +03001548 return at91_adc_adjust_val_osr(st, val);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001549 }
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 Carpentera176ba32018-07-09 14:06:59 +03001557 &tmp_val);
1558 *val = tmp_val;
Eugen Hristev23ec2772018-05-22 10:52:35 +03001559 mutex_unlock(&st->lock);
1560 iio_device_release_direct_mode(indio_dev);
1561
Eugen Hristev6794e232018-06-21 10:56:21 +03001562 return at91_adc_adjust_val_osr(st, val);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001563 }
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 Hristevd8004c52021-09-01 15:30:09 +03001574 at91_adc_cor(st, chan);
Eugen Hristev8940de22021-09-01 15:30:07 +03001575 at91_adc_writel(st, CHER, BIT(chan->channel));
Eugen Hristeve6d5eee2021-09-01 15:30:08 +03001576 at91_adc_eoc_ena(st, chan->channel);
Eugen Hristev8940de22021-09-01 15:30:07 +03001577 at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001578
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 Hristev6794e232018-06-21 10:56:21 +03001587 ret = at91_adc_adjust_val_osr(st, val);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001588 if (chan->scan_type.sign == 's')
Gwendal Grignou652e7df2021-11-04 01:24:08 -07001589 *val = sign_extend32(*val,
1590 chan->scan_type.realbits - 1);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001591 st->conversion_done = false;
1592 }
1593
Eugen Hristeve6d5eee2021-09-01 15:30:08 +03001594 at91_adc_eoc_dis(st, st->chan->channel);
Eugen Hristev8940de22021-09-01 15:30:07 +03001595 at91_adc_writel(st, CHDR, BIT(chan->channel));
Eugen Hristev23ec2772018-05-22 10:52:35 +03001596
1597 /* Needed to ACK the DRDY interruption */
Eugen Hristev8940de22021-09-01 15:30:07 +03001598 at91_adc_readl(st, LCDR);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001599
1600 mutex_unlock(&st->lock);
1601
1602 iio_device_release_direct_mode(indio_dev);
1603 return ret;
1604}
1605
1606static 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 Desroches27e17712016-01-14 16:38:13 +01001615 case IIO_CHAN_INFO_SCALE:
1616 *val = st->vref_uv / 1000;
Ludovic Desrochesd6511322016-03-22 17:08:46 +01001617 if (chan->differential)
1618 *val *= 2;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001619 *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 Hristev6794e232018-06-21 10:56:21 +03001626 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1627 *val = st->oversampling_ratio;
1628 return IIO_VAL_INT;
1629
Ludovic Desroches27e17712016-01-14 16:38:13 +01001630 default:
1631 return -EINVAL;
1632 }
1633}
1634
1635static 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 Hristev6794e232018-06-21 10:56:21 +03001641 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 Ardeleanebf35aa2020-05-25 13:53:41 +03001658 at91_adc_setup_samp_freq(indio_dev, val);
Eugen Hristev6794e232018-06-21 10:56:21 +03001659 return 0;
1660 default:
Ludovic Desroches27e17712016-01-14 16:38:13 +01001661 return -EINVAL;
Tom Rix7f960272020-10-27 13:08:53 -07001662 }
Ludovic Desroches27e17712016-01-14 16:38:13 +01001663}
1664
Eugen Hristev073c6622017-11-15 14:56:47 +02001665static 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 Hristev8940de22021-09-01 15:30:07 +03001670 /* we have 2 bytes for each channel */
1671 unsigned int sample_size = st->soc_info.platform->nr_channels * 2;
Eugen Hristev073c6622017-11-15 14:56:47 +02001672 /*
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 Hristev8940de22021-09-01 15:30:07 +03001678 sample_size * 2, PAGE_SIZE);
Eugen Hristev073c6622017-11-15 14:56:47 +02001679
1680 if (st->dma_st.dma_chan)
1681 return;
1682
Peter Ujfalusi687d39d2020-01-07 13:37:29 +02001683 st->dma_st.dma_chan = dma_request_chan(&pdev->dev, "rx");
1684 if (IS_ERR(st->dma_st.dma_chan)) {
Eugen Hristev073c6622017-11-15 14:56:47 +02001685 dev_info(&pdev->dev, "can't get DMA channel\n");
Peter Ujfalusi687d39d2020-01-07 13:37:29 +02001686 st->dma_st.dma_chan = NULL;
Eugen Hristev073c6622017-11-15 14:56:47 +02001687 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 Hristev8940de22021-09-01 15:30:07 +03001702 + st->soc_info.platform->layout->LCDR);
Eugen Hristev073c6622017-11-15 14:56:47 +02001703 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
1717dma_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);
1720dma_chan_disable:
1721 dma_release_channel(st->dma_st.dma_chan);
Jonathan Cameron5617f222019-10-13 09:57:23 +01001722 st->dma_st.dma_chan = NULL;
Eugen Hristev073c6622017-11-15 14:56:47 +02001723dma_exit:
1724 dev_info(&pdev->dev, "continuing without DMA support\n");
1725}
1726
1727static 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 Hristev8940de22021-09-01 15:30:07 +03001731 /* we have 2 bytes for each channel */
1732 unsigned int sample_size = st->soc_info.platform->nr_channels * 2;
Eugen Hristev073c6622017-11-15 14:56:47 +02001733 unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
Eugen Hristev8940de22021-09-01 15:30:07 +03001734 sample_size * 2, PAGE_SIZE);
Eugen Hristev073c6622017-11-15 14:56:47 +02001735
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 Cameron5617f222019-10-13 09:57:23 +01001746 st->dma_st.dma_chan = NULL;
Eugen Hristev073c6622017-11-15 14:56:47 +02001747
1748 dev_info(&pdev->dev, "continuing without DMA support\n");
1749}
1750
1751static 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 Hristev1a198792020-09-23 15:17:48 +03001754 int ret;
Eugen Hristev073c6622017-11-15 14:56:47 +02001755
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 Hristev1a198792020-09-23 15:17:48 +03001778 /*
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 Hristev073c6622017-11-15 14:56:47 +02001787}
1788
Eugen Hristev23ec2772018-05-22 10:52:35 +03001789static 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 Hristev8940de22021-09-01 15:30:07 +03001795 st->soc_info.platform->max_index + 1))
Eugen Hristev23ec2772018-05-22 10:52:35 +03001796 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 Hristev8940de22021-09-01 15:30:07 +03001802 st->soc_info.platform->max_index + 1))
Eugen Hristev23ec2772018-05-22 10:52:35 +03001803 return -EINVAL;
1804 return 0;
1805}
1806
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001807static void at91_adc_hw_init(struct iio_dev *indio_dev)
Eugen Hristev500a2ee2017-06-23 15:54:57 +03001808{
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001809 struct at91_adc_state *st = iio_priv(indio_dev);
1810
Eugen Hristev8940de22021-09-01 15:30:07 +03001811 at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST);
Eugen Hristeve6d5eee2021-09-01 15:30:08 +03001812 if (st->soc_info.platform->layout->EOC_IDR)
1813 at91_adc_writel(st, EOC_IDR, 0xffffffff);
Eugen Hristev8940de22021-09-01 15:30:07 +03001814 at91_adc_writel(st, IDR, 0xffffffff);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03001815 /*
1816 * Transfer field must be set to 2 according to the datasheet and
1817 * allows different analog settings for each channel.
1818 */
Eugen Hristev8940de22021-09-01 15:30:07 +03001819 at91_adc_writel(st, MR,
Eugen Hristev500a2ee2017-06-23 15:54:57 +03001820 AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
1821
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001822 at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate);
Eugen Hristev6794e232018-06-21 10:56:21 +03001823
1824 /* configure extended mode register */
1825 at91_adc_config_emr(st);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03001826}
1827
Eugen Hristev073c6622017-11-15 14:56:47 +02001828static ssize_t at91_adc_get_fifo_state(struct device *dev,
1829 struct device_attribute *attr, char *buf)
1830{
Kefeng Wange3faedf2019-04-23 15:50:03 +08001831 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Eugen Hristev073c6622017-11-15 14:56:47 +02001832 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
1837static ssize_t at91_adc_get_watermark(struct device *dev,
1838 struct device_attribute *attr, char *buf)
1839{
Kefeng Wange3faedf2019-04-23 15:50:03 +08001840 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Eugen Hristev073c6622017-11-15 14:56:47 +02001841 struct at91_adc_state *st = iio_priv(indio_dev);
1842
1843 return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark);
1844}
1845
1846static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
1847 at91_adc_get_fifo_state, NULL, 0);
1848static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
1849 at91_adc_get_watermark, NULL, 0);
1850
1851static IIO_CONST_ATTR(hwfifo_watermark_min, "2");
1852static IIO_CONST_ATTR(hwfifo_watermark_max, AT91_HWFIFO_MAX_SIZE_STR);
1853
Eugen Hristev6794e232018-06-21 10:56:21 +03001854static IIO_CONST_ATTR(oversampling_ratio_available,
1855 __stringify(AT91_OSR_1SAMPLES) " "
1856 __stringify(AT91_OSR_4SAMPLES) " "
1857 __stringify(AT91_OSR_16SAMPLES));
1858
1859static struct attribute *at91_adc_attributes[] = {
1860 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
1861 NULL,
1862};
1863
1864static const struct attribute_group at91_adc_attribute_group = {
1865 .attrs = at91_adc_attributes,
1866};
1867
Eugen Hristev073c6622017-11-15 14:56:47 +02001868static 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 Hristev6794e232018-06-21 10:56:21 +03001876static 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 Ardelean789976a2020-09-29 15:59:42 +03001885static 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 Ardeleana2f28352020-09-29 15:59:46 +03001889 const struct attribute **fifo_attrs;
Alexandru Ardelean789976a2020-09-29 15:59:42 +03001890 int ret;
1891
Alexandru Ardeleana2f28352020-09-29 15:59:46 +03001892 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 Ardeleanc02cd5c2021-10-07 08:00:32 +00001898 &iio_pollfunc_store_time, &at91_adc_trigger_handler,
1899 IIO_BUFFER_DIRECTION_IN, &at91_buffer_setup_ops, fifo_attrs);
Alexandru Ardelean789976a2020-09-29 15:59:42 +03001900 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 Ardelean789976a2020-09-29 15:59:42 +03001908 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 Desroches27e17712016-01-14 16:38:13 +01001923static 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 Hristev5e1a1da2017-06-15 16:24:57 +03001928 int ret, i;
Eugen Hristevca4c3022017-10-11 14:21:14 +03001929 u32 edge_type = IRQ_TYPE_NONE;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001930
Ludovic Desroches61be8fd2016-01-18 09:41:56 +01001931 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
Ludovic Desroches27e17712016-01-14 16:38:13 +01001932 if (!indio_dev)
1933 return -ENOMEM;
1934
Ludovic Desroches27e17712016-01-14 16:38:13 +01001935 st = iio_priv(indio_dev);
Alexandru Ardeleanebf35aa2020-05-25 13:53:41 +03001936 st->indio_dev = indio_dev;
Ludovic Desroches27e17712016-01-14 16:38:13 +01001937
Eugen Hristev8940de22021-09-01 15:30:07 +03001938 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 Hristev23ec2772018-05-22 10:52:35 +03001946 bitmap_set(&st->touch_st.channels_bitmask,
Eugen Hristev8940de22021-09-01 15:30:07 +03001947 st->soc_info.platform->touch_chan_x, 1);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001948 bitmap_set(&st->touch_st.channels_bitmask,
Eugen Hristev8940de22021-09-01 15:30:07 +03001949 st->soc_info.platform->touch_chan_y, 1);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001950 bitmap_set(&st->touch_st.channels_bitmask,
Eugen Hristev8940de22021-09-01 15:30:07 +03001951 st->soc_info.platform->touch_chan_p, 1);
Eugen Hristev23ec2772018-05-22 10:52:35 +03001952
Eugen Hristev6794e232018-06-21 10:56:21 +03001953 st->oversampling_ratio = AT91_OSR_1SAMPLES;
1954
Ludovic Desroches27e17712016-01-14 16:38:13 +01001955 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 Hristev5e1a1da2017-06-15 16:24:57 +03001981 ret = of_property_read_u32(pdev->dev.of_node,
1982 "atmel,trigger-edge-type", &edge_type);
1983 if (ret) {
Eugen Hristevca4c3022017-10-11 14:21:14 +03001984 dev_dbg(&pdev->dev,
1985 "atmel,trigger-edge-type not specified, only software trigger available\n");
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001986 }
1987
1988 st->selected_trig = NULL;
1989
Eugen Hristevca4c3022017-10-11 14:21:14 +03001990 /* find the right trigger, or no trigger at all */
Eugen Hristev8940de22021-09-01 15:30:07 +03001991 for (i = 0; i < st->soc_info.platform->hw_trig_cnt + 1; i++)
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03001992 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 Desroches27e17712016-01-14 16:38:13 +01002002 init_waitqueue_head(&st->wq_data_available);
2003 mutex_init(&st->lock);
Eugen Hristev23ec2772018-05-22 10:52:35 +03002004 INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler);
Ludovic Desroches27e17712016-01-14 16:38:13 +01002005
Wang ShaoBocbc4ca32020-09-18 16:28:37 +08002006 st->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2007 if (IS_ERR(st->base))
2008 return PTR_ERR(st->base);
Ludovic Desroches27e17712016-01-14 16:38:13 +01002009
Eugen Hristev073c6622017-11-15 14:56:47 +02002010 /* if we plan to use DMA, we need the physical address of the regs */
2011 st->dma_st.phys_addr = res->start;
2012
Ludovic Desroches27e17712016-01-14 16:38:13 +01002013 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 Desroches27e17712016-01-14 16:38:13 +01002052 ret = clk_prepare_enable(st->per_clk);
2053 if (ret)
2054 goto vref_disable;
2055
Eugen Hristeveaefa152021-09-01 15:30:05 +03002056 at91_adc_hw_init(indio_dev);
2057
Marek Vasut8e6cb472016-04-18 18:30:05 +02002058 platform_set_drvdata(pdev, indio_dev);
2059
Alexandru Ardelean789976a2020-09-29 15:59:42 +03002060 ret = at91_adc_buffer_and_trigger_init(&pdev->dev, indio_dev);
2061 if (ret < 0)
Eugen Hristev23ec2772018-05-22 10:52:35 +03002062 goto per_clk_disable_unprepare;
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03002063
Eugen Hristev073c6622017-11-15 14:56:47 +02002064 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 Desroches27e17712016-01-14 16:38:13 +01002067 ret = iio_device_register(indio_dev);
2068 if (ret < 0)
Eugen Hristev073c6622017-11-15 14:56:47 +02002069 goto dma_disable;
Ludovic Desroches27e17712016-01-14 16:38:13 +01002070
Eugen Hristevca4c3022017-10-11 14:21:14 +03002071 if (st->selected_trig->hw_trig)
2072 dev_info(&pdev->dev, "setting up trigger as %s\n",
2073 st->selected_trig->name);
Eugen Hristev5e1a1da2017-06-15 16:24:57 +03002074
Ludovic Desroches27e17712016-01-14 16:38:13 +01002075 dev_info(&pdev->dev, "version: %x\n",
Eugen Hristev8940de22021-09-01 15:30:07 +03002076 readl_relaxed(st->base + st->soc_info.platform->layout->VERSION));
Ludovic Desroches27e17712016-01-14 16:38:13 +01002077
2078 return 0;
2079
Eugen Hristev073c6622017-11-15 14:56:47 +02002080dma_disable:
2081 at91_adc_dma_disable(pdev);
Ludovic Desroches27e17712016-01-14 16:38:13 +01002082per_clk_disable_unprepare:
2083 clk_disable_unprepare(st->per_clk);
2084vref_disable:
2085 regulator_disable(st->vref);
2086reg_disable:
2087 regulator_disable(st->reg);
2088 return ret;
2089}
2090
2091static 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 Hristev073c6622017-11-15 14:56:47 +02002098 at91_adc_dma_disable(pdev);
2099
Ludovic Desroches27e17712016-01-14 16:38:13 +01002100 clk_disable_unprepare(st->per_clk);
2101
2102 regulator_disable(st->vref);
2103 regulator_disable(st->reg);
2104
2105 return 0;
2106}
2107
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002108static __maybe_unused int at91_adc_suspend(struct device *dev)
2109{
Kefeng Wange3faedf2019-04-23 15:50:03 +08002110 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002111 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 Hristev8940de22021-09-01 15:30:07 +03002119 at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002120
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
2128static __maybe_unused int at91_adc_resume(struct device *dev)
2129{
Kefeng Wange3faedf2019-04-23 15:50:03 +08002130 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002131 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 Ardeleanebf35aa2020-05-25 13:53:41 +03002150 at91_adc_hw_init(indio_dev);
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002151
2152 /* reconfiguring trigger hardware state */
Eugen Hristev23ec2772018-05-22 10:52:35 +03002153 if (!iio_buffer_enabled(indio_dev))
2154 return 0;
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002155
Eugen Hristev23ec2772018-05-22 10:52:35 +03002156 /* check if we are enabling triggered buffer or the touchscreen */
Alexandru Ardelean065056c2020-03-04 10:42:18 +02002157 if (at91_adc_current_chan_is_touch(indio_dev))
Eugen Hristev23ec2772018-05-22 10:52:35 +03002158 return at91_adc_configure_touch(st, true);
Alexandru Ardelean065056c2020-03-04 10:42:18 +02002159 else
Eugen Hristev23ec2772018-05-22 10:52:35 +03002160 return at91_adc_configure_trigger(st->trig, true);
Eugen Hristev23ec2772018-05-22 10:52:35 +03002161
2162 /* not needed but more explicit */
Eugen Hristev500a2ee2017-06-23 15:54:57 +03002163 return 0;
2164
2165vref_disable_resume:
2166 regulator_disable(st->vref);
2167reg_disable_resume:
2168 regulator_disable(st->reg);
2169resume_failed:
2170 dev_err(&indio_dev->dev, "failed to resume\n");
2171 return ret;
2172}
2173
2174static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
2175
Ludovic Desroches27e17712016-01-14 16:38:13 +01002176static const struct of_device_id at91_adc_dt_match[] = {
2177 {
2178 .compatible = "atmel,sama5d2-adc",
Eugen Hristev8940de22021-09-01 15:30:07 +03002179 .data = (const void *)&sama5d2_platform,
Ludovic Desroches27e17712016-01-14 16:38:13 +01002180 }, {
Eugen Hristev840bf6c2021-09-01 15:30:10 +03002181 .compatible = "microchip,sama7g5-adc",
2182 .data = (const void *)&sama7g5_platform,
2183 }, {
Ludovic Desroches27e17712016-01-14 16:38:13 +01002184 /* sentinel */
2185 }
2186};
2187MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
2188
2189static 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 Hristev500a2ee2017-06-23 15:54:57 +03002195 .pm = &at91_adc_pm_ops,
Ludovic Desroches27e17712016-01-14 16:38:13 +01002196 },
2197};
2198module_platform_driver(at91_adc_driver)
2199
Eugen Hristev874b4912021-09-01 15:30:11 +03002200MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@microchip.com>");
2201MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com");
Ludovic Desroches27e17712016-01-14 16:38:13 +01002202MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
2203MODULE_LICENSE("GPL v2");