1 // SPDX-License-Identifier: GPL-2.0-only
3 * Atmel ADC driver for SAMA5D2 devices and compatible.
5 * Copyright (C) 2015 Atmel,
6 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
7 * 2021 Microchip Technology, Inc. and its subsidiaries
8 * 2021 Eugen Hristev <eugen.hristev@microchip.com>
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/sched.h>
23 #include <linux/units.h>
24 #include <linux/wait.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include <linux/nvmem-consumer.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/regulator/consumer.h>
36 #include <dt-bindings/iio/adc/at91-sama5d2_adc.h>
38 struct at91_adc_reg_layout
{
39 /* Control Register */
42 #define AT91_SAMA5D2_CR_SWRST BIT(0)
43 /* Start Conversion */
44 #define AT91_SAMA5D2_CR_START BIT(1)
45 /* Touchscreen Calibration */
46 #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
47 /* Comparison Restart */
48 #define AT91_SAMA5D2_CR_CMPRST BIT(4)
52 /* Trigger Selection */
53 #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
55 #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
57 #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
59 #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
61 #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
62 /* PWM event line 0 */
63 #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
64 /* PWM event line 1 */
65 #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
67 #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
69 #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
71 #define AT91_SAMA5D2_MR_SLEEP BIT(5)
73 #define AT91_SAMA5D2_MR_FWUP BIT(6)
74 /* Prescaler Rate Selection */
75 #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
76 #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
77 #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
78 #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
80 #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
81 #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
82 /* Minimum startup time for temperature sensor */
83 #define AT91_SAMA5D2_MR_STARTUP_TS_MIN (50)
85 #define AT91_SAMA5D2_MR_ANACH BIT(23)
87 #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
88 #define AT91_SAMA5D2_MR_TRACKTIM_TS 6
89 #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xf
91 #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
92 #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
93 /* Use Sequence Enable */
94 #define AT91_SAMA5D2_MR_USEQ BIT(31)
96 /* Channel Sequence Register 1 */
98 /* Channel Sequence Register 2 */
100 /* Channel Enable Register */
102 /* Channel Disable Register */
104 /* Channel Status Register */
106 /* Last Converted Data Register */
108 /* Interrupt Enable Register */
110 /* Interrupt Enable Register - TS X measurement ready */
111 #define AT91_SAMA5D2_IER_XRDY BIT(20)
112 /* Interrupt Enable Register - TS Y measurement ready */
113 #define AT91_SAMA5D2_IER_YRDY BIT(21)
114 /* Interrupt Enable Register - TS pressure measurement ready */
115 #define AT91_SAMA5D2_IER_PRDY BIT(22)
116 /* Interrupt Enable Register - Data ready */
117 #define AT91_SAMA5D2_IER_DRDY BIT(24)
118 /* Interrupt Enable Register - general overrun error */
119 #define AT91_SAMA5D2_IER_GOVRE BIT(25)
120 /* Interrupt Enable Register - Pen detect */
121 #define AT91_SAMA5D2_IER_PEN BIT(29)
122 /* Interrupt Enable Register - No pen detect */
123 #define AT91_SAMA5D2_IER_NOPEN BIT(30)
125 /* Interrupt Disable Register */
127 /* Interrupt Mask Register */
129 /* Interrupt Status Register */
131 /* End of Conversion Interrupt Enable Register */
133 /* End of Conversion Interrupt Disable Register */
135 /* End of Conversion Interrupt Mask Register */
137 /* End of Conversion Interrupt Status Register */
139 /* Interrupt Status Register - Pen touching sense status */
140 #define AT91_SAMA5D2_ISR_PENS BIT(31)
141 /* Last Channel Trigger Mode Register */
143 /* Last Channel Compare Window Register */
145 /* Overrun Status Register */
147 /* Extended Mode Register */
149 /* Extended Mode Register - Oversampling rate */
150 #define AT91_SAMA5D2_EMR_OSR(V, M) (((V) << 16) & (M))
151 #define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0
152 #define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1
153 #define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2
154 #define AT91_SAMA5D2_EMR_OSR_64SAMPLES 3
155 #define AT91_SAMA5D2_EMR_OSR_256SAMPLES 4
157 /* Extended Mode Register - TRACKX */
158 #define AT91_SAMA5D2_TRACKX_MASK GENMASK(23, 22)
159 #define AT91_SAMA5D2_TRACKX(x) (((x) << 22) & \
160 AT91_SAMA5D2_TRACKX_MASK)
161 /* TRACKX for temperature sensor. */
162 #define AT91_SAMA5D2_TRACKX_TS (1)
164 /* Extended Mode Register - Averaging on single trigger event */
165 #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20)
167 /* Compare Window Register */
169 /* Channel Gain Register */
171 /* Channel Offset Register */
173 /* Channel Offset Register differential offset - constant, not a register */
175 /* Analog Control Register */
177 /* Analog Control Register - Pen detect sensitivity mask */
178 #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0)
179 /* Analog Control Register - Source last channel */
180 #define AT91_SAMA5D2_ACR_SRCLCH BIT(16)
182 /* Touchscreen Mode Register */
184 /* Touchscreen Mode Register - No touch mode */
185 #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0
186 /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
187 #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1
188 /* Touchscreen Mode Register - 4 wire screen, pressure measurement */
189 #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS 2
190 /* Touchscreen Mode Register - 5 wire screen */
191 #define AT91_SAMA5D2_TSMR_TSMODE_5WIRE 3
192 /* Touchscreen Mode Register - Average samples mask */
193 #define AT91_SAMA5D2_TSMR_TSAV_MASK GENMASK(5, 4)
194 /* Touchscreen Mode Register - Average samples */
195 #define AT91_SAMA5D2_TSMR_TSAV(x) ((x) << 4)
196 /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
197 #define AT91_SAMA5D2_TSMR_TSFREQ_MASK GENMASK(11, 8)
198 /* Touchscreen Mode Register - Touch/trigger frequency ratio */
199 #define AT91_SAMA5D2_TSMR_TSFREQ(x) ((x) << 8)
200 /* Touchscreen Mode Register - Pen Debounce Time mask */
201 #define AT91_SAMA5D2_TSMR_PENDBC_MASK GENMASK(31, 28)
202 /* Touchscreen Mode Register - Pen Debounce Time */
203 #define AT91_SAMA5D2_TSMR_PENDBC(x) ((x) << 28)
204 /* Touchscreen Mode Register - No DMA for touch measurements */
205 #define AT91_SAMA5D2_TSMR_NOTSDMA BIT(22)
206 /* Touchscreen Mode Register - Disable pen detection */
207 #define AT91_SAMA5D2_TSMR_PENDET_DIS (0 << 24)
208 /* Touchscreen Mode Register - Enable pen detection */
209 #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24)
211 /* Touchscreen X Position Register */
213 /* Touchscreen Y Position Register */
215 /* Touchscreen Pressure Register */
217 /* Trigger Register */
219 /* Mask for TRGMOD field of TRGR register */
220 #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0)
221 /* No trigger, only software trigger can start conversions */
222 #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0
223 /* Trigger Mode external trigger rising edge */
224 #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1
225 /* Trigger Mode external trigger falling edge */
226 #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
227 /* Trigger Mode external trigger any edge */
228 #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
229 /* Trigger Mode internal periodic */
230 #define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5
231 /* Trigger Mode - trigger period mask */
232 #define AT91_SAMA5D2_TRGR_TRGPER_MASK GENMASK(31, 16)
233 /* Trigger Mode - trigger period */
234 #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16)
236 /* Correction Select Register */
238 /* Correction Value Register */
240 /* Channel Error Correction Register */
242 /* Write Protection Mode Register */
244 /* Write Protection Status Register */
246 /* Version Register */
248 /* Temperature Sensor Mode Register */
250 /* Temperature Sensor Mode - Temperature sensor on */
251 #define AT91_SAMA5D2_TEMPMR_TEMPON BIT(0)
254 static const struct at91_adc_reg_layout sama5d2_layout
= {
274 .COR_diff_offset
= 16,
289 static const struct at91_adc_reg_layout sama7g5_layout
= {
311 .COR_diff_offset
= 0,
322 #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
323 #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200
325 #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0)
327 #define AT91_SAMA5D2_MAX_POS_BITS 12
329 #define AT91_HWFIFO_MAX_SIZE_STR "128"
330 #define AT91_HWFIFO_MAX_SIZE 128
332 #define AT91_SAMA5D2_CHAN_SINGLE(index, num, addr) \
334 .type = IIO_VOLTAGE, \
337 .scan_index = index, \
343 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
344 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
345 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
346 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
347 .info_mask_shared_by_all_available = \
348 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
349 .datasheet_name = "CH"#num, \
353 #define AT91_SAMA5D2_CHAN_DIFF(index, num, num2, addr) \
355 .type = IIO_VOLTAGE, \
360 .scan_index = index, \
366 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
367 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
368 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
369 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
370 .info_mask_shared_by_all_available = \
371 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
372 .datasheet_name = "CH"#num"-CH"#num2, \
376 #define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod) \
378 .type = IIO_POSITIONRELATIVE, \
388 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
389 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
390 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
391 .info_mask_shared_by_all_available = \
392 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
393 .datasheet_name = name, \
395 #define AT91_SAMA5D2_CHAN_PRESSURE(num, name) \
397 .type = IIO_PRESSURE, \
405 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
406 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
407 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
408 .info_mask_shared_by_all_available = \
409 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
410 .datasheet_name = name, \
413 #define AT91_SAMA5D2_CHAN_TEMP(num, name, addr) \
419 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
420 .info_mask_shared_by_all = \
421 BIT(IIO_CHAN_INFO_PROCESSED) | \
422 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
423 .info_mask_shared_by_all_available = \
424 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
425 .datasheet_name = name, \
428 #define at91_adc_readl(st, reg) \
429 readl_relaxed((st)->base + (st)->soc_info.platform->layout->reg)
430 #define at91_adc_read_chan(st, reg) \
431 readl_relaxed((st)->base + reg)
432 #define at91_adc_writel(st, reg, val) \
433 writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg)
436 * struct at91_adc_platform - at91-sama5d2 platform information struct
437 * @layout: pointer to the reg layout struct
438 * @adc_channels: pointer to an array of channels for registering in
440 * @nr_channels: number of physical channels available
441 * @touch_chan_x: index of the touchscreen X channel
442 * @touch_chan_y: index of the touchscreen Y channel
443 * @touch_chan_p: index of the touchscreen P channel
444 * @max_channels: number of total channels
445 * @max_index: highest channel index (highest index may be higher
446 * than the total channel number)
447 * @hw_trig_cnt: number of possible hardware triggers
448 * @osr_mask: oversampling ratio bitmask on EMR register
449 * @oversampling_avail: available oversampling values
450 * @oversampling_avail_no: number of available oversampling values
451 * @chan_realbits: realbits for registered channels
452 * @temp_chan: temperature channel index
453 * @temp_sensor: temperature sensor supported
455 struct at91_adc_platform
{
456 const struct at91_adc_reg_layout
*layout
;
457 const struct iio_chan_spec (*adc_channels
)[];
458 unsigned int nr_channels
;
459 unsigned int touch_chan_x
;
460 unsigned int touch_chan_y
;
461 unsigned int touch_chan_p
;
462 unsigned int max_channels
;
463 unsigned int max_index
;
464 unsigned int hw_trig_cnt
;
465 unsigned int osr_mask
;
466 unsigned int oversampling_avail
[5];
467 unsigned int oversampling_avail_no
;
468 unsigned int chan_realbits
;
469 unsigned int temp_chan
;
474 * struct at91_adc_temp_sensor_clb - at91-sama5d2 temperature sensor
475 * calibration data structure
476 * @p1: P1 calibration temperature
477 * @p4: P4 calibration voltage
478 * @p6: P6 calibration voltage
480 struct at91_adc_temp_sensor_clb
{
487 * enum at91_adc_ts_clb_idx - calibration indexes in NVMEM buffer
488 * @AT91_ADC_TS_CLB_IDX_P1: index for P1
489 * @AT91_ADC_TS_CLB_IDX_P4: index for P4
490 * @AT91_ADC_TS_CLB_IDX_P6: index for P6
491 * @AT91_ADC_TS_CLB_IDX_MAX: max index for temperature calibration packet in OTP
493 enum at91_adc_ts_clb_idx
{
494 AT91_ADC_TS_CLB_IDX_P1
= 2,
495 AT91_ADC_TS_CLB_IDX_P4
= 5,
496 AT91_ADC_TS_CLB_IDX_P6
= 7,
497 AT91_ADC_TS_CLB_IDX_MAX
= 19,
500 /* Temperature sensor calibration - Vtemp voltage sensitivity to temperature. */
501 #define AT91_ADC_TS_VTEMP_DT (2080U)
504 * struct at91_adc_soc_info - at91-sama5d2 soc information struct
505 * @startup_time: device startup time
506 * @min_sample_rate: minimum sample rate in Hz
507 * @max_sample_rate: maximum sample rate in Hz
508 * @platform: pointer to the platform structure
509 * @temp_sensor_clb: temperature sensor calibration data structure
511 struct at91_adc_soc_info
{
512 unsigned startup_time
;
513 unsigned min_sample_rate
;
514 unsigned max_sample_rate
;
515 const struct at91_adc_platform
*platform
;
516 struct at91_adc_temp_sensor_clb temp_sensor_clb
;
519 struct at91_adc_trigger
{
521 unsigned int trgmod_value
;
522 unsigned int edge_type
;
527 * struct at91_adc_dma - at91-sama5d2 dma information struct
528 * @dma_chan: the dma channel acquired
529 * @rx_buf: dma coherent allocated area
530 * @rx_dma_buf: dma handler for the buffer
531 * @phys_addr: physical address of the ADC base register
532 * @buf_idx: index inside the dma buffer where reading was last done
533 * @rx_buf_sz: size of buffer used by DMA operation
534 * @watermark: number of conversions to copy before DMA triggers irq
535 * @dma_ts: hold the start timestamp of dma operation
537 struct at91_adc_dma
{
538 struct dma_chan
*dma_chan
;
540 dma_addr_t rx_dma_buf
;
541 phys_addr_t phys_addr
;
549 * struct at91_adc_touch - at91-sama5d2 touchscreen information struct
550 * @sample_period_val: the value for periodic trigger interval
551 * @touching: is the pen touching the screen or not
552 * @x_pos: temporary placeholder for pressure computation
553 * @channels_bitmask: bitmask with the touchscreen channels enabled
554 * @workq: workqueue for buffer data pushing
556 struct at91_adc_touch
{
557 u16 sample_period_val
;
560 unsigned long channels_bitmask
;
561 struct work_struct workq
;
565 * struct at91_adc_temp - at91-sama5d2 temperature information structure
566 * @sample_period_val: sample period value
567 * @saved_sample_rate: saved sample rate
568 * @saved_oversampling: saved oversampling
570 struct at91_adc_temp
{
571 u16 sample_period_val
;
572 u16 saved_sample_rate
;
573 u16 saved_oversampling
;
577 * Buffer size requirements:
578 * No channels * bytes_per_channel(2) + timestamp bytes (8)
579 * Divided by 2 because we need half words.
580 * We assume 32 channels for now, has to be increased if needed.
581 * Nobody minds a buffer being too big.
583 #define AT91_BUFFER_MAX_HWORDS ((32 * 2 + 8) / 2)
585 struct at91_adc_state
{
589 struct regulator
*reg
;
590 struct regulator
*vref
;
592 unsigned int current_sample_rate
;
593 struct iio_trigger
*trig
;
594 const struct at91_adc_trigger
*selected_trig
;
595 const struct iio_chan_spec
*chan
;
596 bool conversion_done
;
597 u32 conversion_value
;
598 unsigned int oversampling_ratio
;
599 struct at91_adc_soc_info soc_info
;
600 wait_queue_head_t wq_data_available
;
601 struct at91_adc_dma dma_st
;
602 struct at91_adc_touch touch_st
;
603 struct at91_adc_temp temp_st
;
604 struct iio_dev
*indio_dev
;
606 /* Ensure naturally aligned timestamp */
607 u16 buffer
[AT91_BUFFER_MAX_HWORDS
] __aligned(8);
609 * lock to prevent concurrent 'single conversion' requests through
615 static const struct at91_adc_trigger at91_adc_trigger_list
[] = {
617 .name
= "external_rising",
618 .trgmod_value
= AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE
,
619 .edge_type
= IRQ_TYPE_EDGE_RISING
,
623 .name
= "external_falling",
624 .trgmod_value
= AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL
,
625 .edge_type
= IRQ_TYPE_EDGE_FALLING
,
629 .name
= "external_any",
630 .trgmod_value
= AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY
,
631 .edge_type
= IRQ_TYPE_EDGE_BOTH
,
636 .trgmod_value
= AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER
,
637 .edge_type
= IRQ_TYPE_NONE
,
642 static const struct iio_chan_spec at91_sama5d2_adc_channels
[] = {
643 AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x50),
644 AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x54),
645 AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x58),
646 AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x5c),
647 AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x60),
648 AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x64),
649 AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x68),
650 AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x6c),
651 AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x70),
652 AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x74),
653 AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x78),
654 AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x7c),
655 /* original ABI has the differential channels with a gap in between */
656 AT91_SAMA5D2_CHAN_DIFF(12, 0, 1, 0x50),
657 AT91_SAMA5D2_CHAN_DIFF(14, 2, 3, 0x58),
658 AT91_SAMA5D2_CHAN_DIFF(16, 4, 5, 0x60),
659 AT91_SAMA5D2_CHAN_DIFF(18, 6, 7, 0x68),
660 AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x70),
661 AT91_SAMA5D2_CHAN_DIFF(22, 10, 11, 0x78),
662 IIO_CHAN_SOFT_TIMESTAMP(23),
663 AT91_SAMA5D2_CHAN_TOUCH(24, "x", IIO_MOD_X
),
664 AT91_SAMA5D2_CHAN_TOUCH(25, "y", IIO_MOD_Y
),
665 AT91_SAMA5D2_CHAN_PRESSURE(26, "pressure"),
668 static const struct iio_chan_spec at91_sama7g5_adc_channels
[] = {
669 AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x60),
670 AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x64),
671 AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x68),
672 AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x6c),
673 AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x70),
674 AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x74),
675 AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x78),
676 AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x7c),
677 AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x80),
678 AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x84),
679 AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x88),
680 AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x8c),
681 AT91_SAMA5D2_CHAN_SINGLE(12, 12, 0x90),
682 AT91_SAMA5D2_CHAN_SINGLE(13, 13, 0x94),
683 AT91_SAMA5D2_CHAN_SINGLE(14, 14, 0x98),
684 AT91_SAMA5D2_CHAN_SINGLE(15, 15, 0x9c),
685 AT91_SAMA5D2_CHAN_DIFF(16, 0, 1, 0x60),
686 AT91_SAMA5D2_CHAN_DIFF(17, 2, 3, 0x68),
687 AT91_SAMA5D2_CHAN_DIFF(18, 4, 5, 0x70),
688 AT91_SAMA5D2_CHAN_DIFF(19, 6, 7, 0x78),
689 AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x80),
690 AT91_SAMA5D2_CHAN_DIFF(21, 10, 11, 0x88),
691 AT91_SAMA5D2_CHAN_DIFF(22, 12, 13, 0x90),
692 AT91_SAMA5D2_CHAN_DIFF(23, 14, 15, 0x98),
693 IIO_CHAN_SOFT_TIMESTAMP(24),
694 AT91_SAMA5D2_CHAN_TEMP(AT91_SAMA7G5_ADC_TEMP_CHANNEL
, "temp", 0xdc),
697 static const struct at91_adc_platform sama5d2_platform
= {
698 .layout
= &sama5d2_layout
,
699 .adc_channels
= &at91_sama5d2_adc_channels
,
700 #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
701 #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
702 .nr_channels
= AT91_SAMA5D2_SINGLE_CHAN_CNT
+
703 AT91_SAMA5D2_DIFF_CHAN_CNT
,
704 #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
705 AT91_SAMA5D2_DIFF_CHAN_CNT * 2)
706 .touch_chan_x
= AT91_SAMA5D2_TOUCH_X_CHAN_IDX
,
707 #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1)
708 .touch_chan_y
= AT91_SAMA5D2_TOUCH_Y_CHAN_IDX
,
709 #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1)
710 .touch_chan_p
= AT91_SAMA5D2_TOUCH_P_CHAN_IDX
,
711 #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX
712 .max_channels
= ARRAY_SIZE(at91_sama5d2_adc_channels
),
713 .max_index
= AT91_SAMA5D2_MAX_CHAN_IDX
,
714 #define AT91_SAMA5D2_HW_TRIG_CNT 3
715 .hw_trig_cnt
= AT91_SAMA5D2_HW_TRIG_CNT
,
716 .osr_mask
= GENMASK(17, 16),
717 .oversampling_avail
= { 1, 4, 16, },
718 .oversampling_avail_no
= 3,
722 static const struct at91_adc_platform sama7g5_platform
= {
723 .layout
= &sama7g5_layout
,
724 .adc_channels
= &at91_sama7g5_adc_channels
,
725 #define AT91_SAMA7G5_SINGLE_CHAN_CNT 16
726 #define AT91_SAMA7G5_DIFF_CHAN_CNT 8
727 #define AT91_SAMA7G5_TEMP_CHAN_CNT 1
728 .nr_channels
= AT91_SAMA7G5_SINGLE_CHAN_CNT
+
729 AT91_SAMA7G5_DIFF_CHAN_CNT
+
730 AT91_SAMA7G5_TEMP_CHAN_CNT
,
731 #define AT91_SAMA7G5_MAX_CHAN_IDX (AT91_SAMA7G5_SINGLE_CHAN_CNT + \
732 AT91_SAMA7G5_DIFF_CHAN_CNT + \
733 AT91_SAMA7G5_TEMP_CHAN_CNT)
734 .max_channels
= ARRAY_SIZE(at91_sama7g5_adc_channels
),
735 .max_index
= AT91_SAMA7G5_MAX_CHAN_IDX
,
736 #define AT91_SAMA7G5_HW_TRIG_CNT 3
737 .hw_trig_cnt
= AT91_SAMA7G5_HW_TRIG_CNT
,
738 .osr_mask
= GENMASK(18, 16),
739 .oversampling_avail
= { 1, 4, 16, 64, 256, },
740 .oversampling_avail_no
= 5,
743 .temp_chan
= AT91_SAMA7G5_ADC_TEMP_CHANNEL
,
746 static int at91_adc_chan_xlate(struct iio_dev
*indio_dev
, int chan
)
750 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
751 if (indio_dev
->channels
[i
].scan_index
== chan
)
757 static inline struct iio_chan_spec
const *
758 at91_adc_chan_get(struct iio_dev
*indio_dev
, int chan
)
760 int index
= at91_adc_chan_xlate(indio_dev
, chan
);
764 return indio_dev
->channels
+ index
;
767 static inline int at91_adc_fwnode_xlate(struct iio_dev
*indio_dev
,
768 const struct fwnode_reference_args
*iiospec
)
770 return at91_adc_chan_xlate(indio_dev
, iiospec
->args
[0]);
773 static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev
*indio_dev
)
777 struct at91_adc_state
*st
= iio_priv(indio_dev
);
779 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
780 indio_dev
->num_channels
) {
781 struct iio_chan_spec
const *chan
=
782 at91_adc_chan_get(indio_dev
, bit
);
783 mask
|= BIT(chan
->channel
);
786 return mask
& GENMASK(st
->soc_info
.platform
->nr_channels
, 0);
789 static void at91_adc_cor(struct at91_adc_state
*st
,
790 struct iio_chan_spec
const *chan
)
794 cor
= BIT(chan
->channel
) | BIT(chan
->channel2
);
796 cur_cor
= at91_adc_readl(st
, COR
);
797 cor
<<= st
->soc_info
.platform
->layout
->COR_diff_offset
;
798 if (chan
->differential
)
799 at91_adc_writel(st
, COR
, cur_cor
| cor
);
801 at91_adc_writel(st
, COR
, cur_cor
& ~cor
);
804 static void at91_adc_irq_status(struct at91_adc_state
*st
, u32
*status
,
807 *status
= at91_adc_readl(st
, ISR
);
808 if (st
->soc_info
.platform
->layout
->EOC_ISR
)
809 *eoc
= at91_adc_readl(st
, EOC_ISR
);
814 static void at91_adc_irq_mask(struct at91_adc_state
*st
, u32
*status
, u32
*eoc
)
816 *status
= at91_adc_readl(st
, IMR
);
817 if (st
->soc_info
.platform
->layout
->EOC_IMR
)
818 *eoc
= at91_adc_readl(st
, EOC_IMR
);
823 static void at91_adc_eoc_dis(struct at91_adc_state
*st
, unsigned int channel
)
826 * On some products having the EOC bits in a separate register,
827 * errata recommends not writing this register (EOC_IDR).
828 * On products having the EOC bits in the IDR register, it's fine to write it.
830 if (!st
->soc_info
.platform
->layout
->EOC_IDR
)
831 at91_adc_writel(st
, IDR
, BIT(channel
));
834 static void at91_adc_eoc_ena(struct at91_adc_state
*st
, unsigned int channel
)
836 if (!st
->soc_info
.platform
->layout
->EOC_IDR
)
837 at91_adc_writel(st
, IER
, BIT(channel
));
839 at91_adc_writel(st
, EOC_IER
, BIT(channel
));
842 static int at91_adc_config_emr(struct at91_adc_state
*st
,
843 u32 oversampling_ratio
, u32 trackx
)
845 /* configure the extended mode register */
846 unsigned int emr
, osr
;
847 unsigned int osr_mask
= st
->soc_info
.platform
->osr_mask
;
850 /* Check against supported oversampling values. */
851 for (i
= 0; i
< st
->soc_info
.platform
->oversampling_avail_no
; i
++) {
852 if (oversampling_ratio
== st
->soc_info
.platform
->oversampling_avail
[i
])
855 if (i
== st
->soc_info
.platform
->oversampling_avail_no
)
858 /* select oversampling ratio from configuration */
859 switch (oversampling_ratio
) {
861 osr
= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_1SAMPLES
,
865 osr
= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_4SAMPLES
,
869 osr
= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_16SAMPLES
,
873 osr
= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_64SAMPLES
,
877 osr
= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_256SAMPLES
,
882 ret
= pm_runtime_resume_and_get(st
->dev
);
886 emr
= at91_adc_readl(st
, EMR
);
887 /* select oversampling per single trigger event */
888 emr
|= AT91_SAMA5D2_EMR_ASTE(1);
889 /* delete leftover content if it's the case */
890 emr
&= ~(osr_mask
| AT91_SAMA5D2_TRACKX_MASK
);
891 /* Update osr and trackx. */
892 emr
|= osr
| AT91_SAMA5D2_TRACKX(trackx
);
893 at91_adc_writel(st
, EMR
, emr
);
895 pm_runtime_mark_last_busy(st
->dev
);
896 pm_runtime_put_autosuspend(st
->dev
);
898 st
->oversampling_ratio
= oversampling_ratio
;
903 static int at91_adc_adjust_val_osr(struct at91_adc_state
*st
, int *val
)
907 if (st
->oversampling_ratio
== 1)
909 else if (st
->oversampling_ratio
== 4)
911 else if (st
->oversampling_ratio
== 16)
913 else if (st
->oversampling_ratio
== 64)
915 else if (st
->oversampling_ratio
== 256)
918 /* Should not happen. */
922 * We have nbits of real data and channel is registered as
923 * st->soc_info.platform->chan_realbits, so shift left diff bits.
925 diff
= st
->soc_info
.platform
->chan_realbits
- nbits
;
931 static void at91_adc_adjust_val_osr_array(struct at91_adc_state
*st
, void *buf
,
935 u16
*buf_u16
= (u16
*) buf
;
938 * We are converting each two bytes (each sample).
939 * First convert the byte based array to u16, and convert each sample
941 * Each value is two bytes in an array of chars, so to not shift
942 * more than we need, save the value separately.
943 * len is in bytes, so divide by two to get number of samples.
945 while (i
< len
/ 2) {
947 at91_adc_adjust_val_osr(st
, &val
);
953 static int at91_adc_configure_touch(struct at91_adc_state
*st
, bool state
)
955 u32 clk_khz
= st
->current_sample_rate
/ 1000;
961 ret
= pm_runtime_resume_and_get(st
->dev
);
965 /* disabling touch IRQs and setting mode to no touch enabled */
966 at91_adc_writel(st
, IDR
,
967 AT91_SAMA5D2_IER_PEN
| AT91_SAMA5D2_IER_NOPEN
);
968 at91_adc_writel(st
, TSMR
, 0);
970 pm_runtime_mark_last_busy(st
->dev
);
971 pm_runtime_put_autosuspend(st
->dev
);
975 * debounce time is in microseconds, we need it in milliseconds to
976 * multiply with kilohertz, so, divide by 1000, but after the multiply.
977 * round up to make sure pendbc is at least 1
979 pendbc
= round_up(AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US
*
982 /* get the required exponent */
983 while (pendbc
>> i
++)
988 tsmr
= AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS
;
990 tsmr
|= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK
;
991 tsmr
|= AT91_SAMA5D2_TSMR_PENDBC(pendbc
) &
992 AT91_SAMA5D2_TSMR_PENDBC_MASK
;
993 tsmr
|= AT91_SAMA5D2_TSMR_NOTSDMA
;
994 tsmr
|= AT91_SAMA5D2_TSMR_PENDET_ENA
;
995 tsmr
|= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK
;
997 at91_adc_writel(st
, TSMR
, tsmr
);
999 acr
= at91_adc_readl(st
, ACR
);
1000 acr
&= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK
;
1001 acr
|= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK
;
1002 at91_adc_writel(st
, ACR
, acr
);
1004 /* Sample Period Time = (TRGPER + 1) / ADCClock */
1005 st
->touch_st
.sample_period_val
=
1006 round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US
*
1007 clk_khz
/ 1000) - 1, 1);
1008 /* enable pen detect IRQ */
1009 at91_adc_writel(st
, IER
, AT91_SAMA5D2_IER_PEN
);
1014 static u16
at91_adc_touch_pos(struct at91_adc_state
*st
, int reg
)
1017 u32 scale
, result
, pos
;
1020 * to obtain the actual position we must divide by scale
1021 * and multiply with max, where
1022 * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1
1024 /* first half of register is the x or y, second half is the scale */
1025 if (reg
== st
->soc_info
.platform
->layout
->XPOSR
)
1026 val
= at91_adc_readl(st
, XPOSR
);
1027 else if (reg
== st
->soc_info
.platform
->layout
->YPOSR
)
1028 val
= at91_adc_readl(st
, YPOSR
);
1031 dev_dbg(&st
->indio_dev
->dev
, "pos is 0\n");
1033 pos
= val
& AT91_SAMA5D2_XYZ_MASK
;
1034 result
= (pos
<< AT91_SAMA5D2_MAX_POS_BITS
) - pos
;
1035 scale
= (val
>> 16) & AT91_SAMA5D2_XYZ_MASK
;
1037 dev_err(&st
->indio_dev
->dev
, "scale is 0\n");
1045 static u16
at91_adc_touch_x_pos(struct at91_adc_state
*st
)
1047 st
->touch_st
.x_pos
= at91_adc_touch_pos(st
, st
->soc_info
.platform
->layout
->XPOSR
);
1048 return st
->touch_st
.x_pos
;
1051 static u16
at91_adc_touch_y_pos(struct at91_adc_state
*st
)
1053 return at91_adc_touch_pos(st
, st
->soc_info
.platform
->layout
->YPOSR
);
1056 static u16
at91_adc_touch_pressure(struct at91_adc_state
*st
)
1064 /* calculate the pressure */
1065 val
= at91_adc_readl(st
, PRESSR
);
1066 z1
= val
& AT91_SAMA5D2_XYZ_MASK
;
1067 z2
= (val
>> 16) & AT91_SAMA5D2_XYZ_MASK
;
1070 pres
= rxp
* (st
->touch_st
.x_pos
* factor
/ 1024) *
1071 (z2
* factor
/ z1
- factor
) /
1074 pres
= 0xFFFF; /* no pen contact */
1077 * The pressure from device grows down, minimum is 0xFFFF, maximum 0x0.
1078 * We compute it this way, but let's return it in the expected way,
1079 * growing from 0 to 0xFFFF.
1081 return 0xFFFF - pres
;
1084 static int at91_adc_read_position(struct at91_adc_state
*st
, int chan
, u16
*val
)
1087 if (!st
->touch_st
.touching
)
1089 if (chan
== st
->soc_info
.platform
->touch_chan_x
)
1090 *val
= at91_adc_touch_x_pos(st
);
1091 else if (chan
== st
->soc_info
.platform
->touch_chan_y
)
1092 *val
= at91_adc_touch_y_pos(st
);
1099 static int at91_adc_read_pressure(struct at91_adc_state
*st
, int chan
, u16
*val
)
1102 if (!st
->touch_st
.touching
)
1104 if (chan
== st
->soc_info
.platform
->touch_chan_p
)
1105 *val
= at91_adc_touch_pressure(st
);
1112 static void at91_adc_configure_trigger_registers(struct at91_adc_state
*st
,
1115 u32 status
= at91_adc_readl(st
, TRGR
);
1118 status
&= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK
;
1121 status
|= st
->selected_trig
->trgmod_value
;
1123 /* set/unset hw trigger */
1124 at91_adc_writel(st
, TRGR
, status
);
1127 static int at91_adc_configure_trigger(struct iio_trigger
*trig
, bool state
)
1129 struct iio_dev
*indio
= iio_trigger_get_drvdata(trig
);
1130 struct at91_adc_state
*st
= iio_priv(indio
);
1134 ret
= pm_runtime_resume_and_get(st
->dev
);
1139 at91_adc_configure_trigger_registers(st
, state
);
1142 pm_runtime_mark_last_busy(st
->dev
);
1143 pm_runtime_put_autosuspend(st
->dev
);
1149 static void at91_adc_reenable_trigger(struct iio_trigger
*trig
)
1151 struct iio_dev
*indio
= iio_trigger_get_drvdata(trig
);
1152 struct at91_adc_state
*st
= iio_priv(indio
);
1154 /* if we are using DMA, we must not reenable irq after each trigger */
1155 if (st
->dma_st
.dma_chan
)
1158 enable_irq(st
->irq
);
1160 /* Needed to ACK the DRDY interruption */
1161 at91_adc_readl(st
, LCDR
);
1164 static const struct iio_trigger_ops at91_adc_trigger_ops
= {
1165 .set_trigger_state
= &at91_adc_configure_trigger
,
1166 .reenable
= &at91_adc_reenable_trigger
,
1167 .validate_device
= iio_trigger_validate_own_device
,
1170 static int at91_adc_dma_size_done(struct at91_adc_state
*st
)
1172 struct dma_tx_state state
;
1173 enum dma_status status
;
1176 status
= dmaengine_tx_status(st
->dma_st
.dma_chan
,
1177 st
->dma_st
.dma_chan
->cookie
,
1179 if (status
!= DMA_IN_PROGRESS
)
1182 /* Transferred length is size in bytes from end of buffer */
1183 i
= st
->dma_st
.rx_buf_sz
- state
.residue
;
1185 /* Return available bytes */
1186 if (i
>= st
->dma_st
.buf_idx
)
1187 size
= i
- st
->dma_st
.buf_idx
;
1189 size
= st
->dma_st
.rx_buf_sz
+ i
- st
->dma_st
.buf_idx
;
1193 static void at91_dma_buffer_done(void *data
)
1195 struct iio_dev
*indio_dev
= data
;
1197 iio_trigger_poll_nested(indio_dev
->trig
);
1200 static int at91_adc_dma_start(struct iio_dev
*indio_dev
)
1202 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1203 struct dma_async_tx_descriptor
*desc
;
1204 dma_cookie_t cookie
;
1208 if (!st
->dma_st
.dma_chan
)
1211 /* we start a new DMA, so set buffer index to start */
1212 st
->dma_st
.buf_idx
= 0;
1215 * compute buffer size w.r.t. watermark and enabled channels.
1216 * scan_bytes is aligned so we need an exact size for DMA
1218 st
->dma_st
.rx_buf_sz
= 0;
1220 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1221 indio_dev
->num_channels
) {
1222 struct iio_chan_spec
const *chan
=
1223 at91_adc_chan_get(indio_dev
, bit
);
1228 st
->dma_st
.rx_buf_sz
+= chan
->scan_type
.storagebits
/ 8;
1230 st
->dma_st
.rx_buf_sz
*= st
->dma_st
.watermark
;
1232 /* Prepare a DMA cyclic transaction */
1233 desc
= dmaengine_prep_dma_cyclic(st
->dma_st
.dma_chan
,
1234 st
->dma_st
.rx_dma_buf
,
1235 st
->dma_st
.rx_buf_sz
,
1236 st
->dma_st
.rx_buf_sz
/ 2,
1237 DMA_DEV_TO_MEM
, DMA_PREP_INTERRUPT
);
1240 dev_err(&indio_dev
->dev
, "cannot prepare DMA cyclic\n");
1244 desc
->callback
= at91_dma_buffer_done
;
1245 desc
->callback_param
= indio_dev
;
1247 cookie
= dmaengine_submit(desc
);
1248 ret
= dma_submit_error(cookie
);
1250 dev_err(&indio_dev
->dev
, "cannot submit DMA cyclic\n");
1251 dmaengine_terminate_async(st
->dma_st
.dma_chan
);
1255 /* enable general overrun error signaling */
1256 at91_adc_writel(st
, IER
, AT91_SAMA5D2_IER_GOVRE
);
1257 /* Issue pending DMA requests */
1258 dma_async_issue_pending(st
->dma_st
.dma_chan
);
1260 /* consider current time as DMA start time for timestamps */
1261 st
->dma_st
.dma_ts
= iio_get_time_ns(indio_dev
);
1263 dev_dbg(&indio_dev
->dev
, "DMA cyclic started\n");
1268 static bool at91_adc_buffer_check_use_irq(struct iio_dev
*indio
,
1269 struct at91_adc_state
*st
)
1271 /* if using DMA, we do not use our own IRQ (we use DMA-controller) */
1272 if (st
->dma_st
.dma_chan
)
1274 /* if the trigger is not ours, then it has its own IRQ */
1275 if (iio_trigger_validate_own_device(indio
->trig
, indio
))
1280 static bool at91_adc_current_chan_is_touch(struct iio_dev
*indio_dev
)
1282 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1284 return !!bitmap_subset(indio_dev
->active_scan_mask
,
1285 &st
->touch_st
.channels_bitmask
,
1286 st
->soc_info
.platform
->max_index
+ 1);
1289 static int at91_adc_buffer_prepare(struct iio_dev
*indio_dev
)
1293 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1295 /* check if we are enabling triggered buffer or the touchscreen */
1296 if (at91_adc_current_chan_is_touch(indio_dev
))
1297 return at91_adc_configure_touch(st
, true);
1299 /* if we are not in triggered mode, we cannot enable the buffer. */
1300 if (!(iio_device_get_current_mode(indio_dev
) & INDIO_ALL_TRIGGERED_MODES
))
1303 ret
= pm_runtime_resume_and_get(st
->dev
);
1307 /* we continue with the triggered buffer */
1308 ret
= at91_adc_dma_start(indio_dev
);
1310 dev_err(&indio_dev
->dev
, "buffer prepare failed\n");
1311 goto pm_runtime_put
;
1314 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1315 indio_dev
->num_channels
) {
1316 struct iio_chan_spec
const *chan
=
1317 at91_adc_chan_get(indio_dev
, bit
);
1320 /* these channel types cannot be handled by this trigger */
1321 if (chan
->type
== IIO_POSITIONRELATIVE
||
1322 chan
->type
== IIO_PRESSURE
||
1323 chan
->type
== IIO_TEMP
)
1326 at91_adc_cor(st
, chan
);
1328 at91_adc_writel(st
, CHER
, BIT(chan
->channel
));
1331 if (at91_adc_buffer_check_use_irq(indio_dev
, st
))
1332 at91_adc_writel(st
, IER
, AT91_SAMA5D2_IER_DRDY
);
1335 pm_runtime_mark_last_busy(st
->dev
);
1336 pm_runtime_put_autosuspend(st
->dev
);
1340 static int at91_adc_buffer_postdisable(struct iio_dev
*indio_dev
)
1342 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1346 /* check if we are disabling triggered buffer or the touchscreen */
1347 if (at91_adc_current_chan_is_touch(indio_dev
))
1348 return at91_adc_configure_touch(st
, false);
1350 /* if we are not in triggered mode, nothing to do here */
1351 if (!(iio_device_get_current_mode(indio_dev
) & INDIO_ALL_TRIGGERED_MODES
))
1354 ret
= pm_runtime_resume_and_get(st
->dev
);
1359 * For each enable channel we must disable it in hardware.
1360 * In the case of DMA, we must read the last converted value
1361 * to clear EOC status and not get a possible interrupt later.
1362 * This value is being read by DMA from LCDR anyway, so it's not lost.
1364 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1365 indio_dev
->num_channels
) {
1366 struct iio_chan_spec
const *chan
=
1367 at91_adc_chan_get(indio_dev
, bit
);
1371 /* these channel types are virtual, no need to do anything */
1372 if (chan
->type
== IIO_POSITIONRELATIVE
||
1373 chan
->type
== IIO_PRESSURE
||
1374 chan
->type
== IIO_TEMP
)
1377 at91_adc_writel(st
, CHDR
, BIT(chan
->channel
));
1379 if (st
->dma_st
.dma_chan
)
1380 at91_adc_read_chan(st
, chan
->address
);
1383 if (at91_adc_buffer_check_use_irq(indio_dev
, st
))
1384 at91_adc_writel(st
, IDR
, AT91_SAMA5D2_IER_DRDY
);
1386 /* read overflow register to clear possible overflow status */
1387 at91_adc_readl(st
, OVER
);
1389 /* if we are using DMA we must clear registers and end DMA */
1390 if (st
->dma_st
.dma_chan
)
1391 dmaengine_terminate_sync(st
->dma_st
.dma_chan
);
1393 pm_runtime_mark_last_busy(st
->dev
);
1394 pm_runtime_put_autosuspend(st
->dev
);
1399 static const struct iio_buffer_setup_ops at91_buffer_setup_ops
= {
1400 .postdisable
= &at91_adc_buffer_postdisable
,
1403 static struct iio_trigger
*at91_adc_allocate_trigger(struct iio_dev
*indio
,
1406 struct iio_trigger
*trig
;
1409 trig
= devm_iio_trigger_alloc(&indio
->dev
, "%s-dev%d-%s", indio
->name
,
1410 iio_device_id(indio
), trigger_name
);
1412 return ERR_PTR(-ENOMEM
);
1414 trig
->dev
.parent
= indio
->dev
.parent
;
1415 iio_trigger_set_drvdata(trig
, indio
);
1416 trig
->ops
= &at91_adc_trigger_ops
;
1418 ret
= devm_iio_trigger_register(&indio
->dev
, trig
);
1420 return ERR_PTR(ret
);
1425 static void at91_adc_trigger_handler_nodma(struct iio_dev
*indio_dev
,
1426 struct iio_poll_func
*pf
)
1428 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1432 u32 mask
= at91_adc_active_scan_mask_to_reg(indio_dev
);
1433 unsigned int timeout
= 50;
1434 u32 status
, imr
, eoc
= 0, eoc_imr
;
1437 * Check if the conversion is ready. If not, wait a little bit, and
1438 * in case of timeout exit with an error.
1440 while (((eoc
& mask
) != mask
) && timeout
) {
1441 at91_adc_irq_status(st
, &status
, &eoc
);
1442 at91_adc_irq_mask(st
, &imr
, &eoc_imr
);
1443 usleep_range(50, 100);
1447 /* Cannot read data, not ready. Continue without reporting data */
1451 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1452 indio_dev
->num_channels
) {
1453 struct iio_chan_spec
const *chan
=
1454 at91_adc_chan_get(indio_dev
, bit
);
1459 * Our external trigger only supports the voltage channels.
1460 * In case someone requested a different type of channel
1461 * just put zeroes to buffer.
1462 * This should not happen because we check the scan mode
1463 * and scan mask when we enable the buffer, and we don't allow
1464 * the buffer to start with a mixed mask (voltage and something
1466 * Thus, emit a warning.
1468 if (chan
->type
== IIO_VOLTAGE
) {
1469 val
= at91_adc_read_chan(st
, chan
->address
);
1470 at91_adc_adjust_val_osr(st
, &val
);
1471 st
->buffer
[i
] = val
;
1474 WARN(true, "This trigger cannot handle this type of channel");
1478 iio_push_to_buffers_with_timestamp(indio_dev
, st
->buffer
,
1482 static void at91_adc_trigger_handler_dma(struct iio_dev
*indio_dev
)
1484 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1485 int transferred_len
= at91_adc_dma_size_done(st
);
1486 s64 ns
= iio_get_time_ns(indio_dev
);
1488 int sample_index
= 0, sample_count
, sample_size
;
1490 u32 status
= at91_adc_readl(st
, ISR
);
1491 /* if we reached this point, we cannot sample faster */
1492 if (status
& AT91_SAMA5D2_IER_GOVRE
)
1493 pr_info_ratelimited("%s: conversion overrun detected\n",
1496 sample_size
= div_s64(st
->dma_st
.rx_buf_sz
, st
->dma_st
.watermark
);
1498 sample_count
= div_s64(transferred_len
, sample_size
);
1501 * interval between samples is total time since last transfer handling
1502 * divided by the number of samples (total size divided by sample size)
1504 interval
= div_s64((ns
- st
->dma_st
.dma_ts
), sample_count
);
1506 while (transferred_len
>= sample_size
) {
1508 * for all the values in the current sample,
1509 * adjust the values inside the buffer for oversampling
1511 at91_adc_adjust_val_osr_array(st
,
1512 &st
->dma_st
.rx_buf
[st
->dma_st
.buf_idx
],
1515 iio_push_to_buffers_with_timestamp(indio_dev
,
1516 (st
->dma_st
.rx_buf
+ st
->dma_st
.buf_idx
),
1517 (st
->dma_st
.dma_ts
+ interval
* sample_index
));
1518 /* adjust remaining length */
1519 transferred_len
-= sample_size
;
1520 /* adjust buffer index */
1521 st
->dma_st
.buf_idx
+= sample_size
;
1522 /* in case of reaching end of buffer, reset index */
1523 if (st
->dma_st
.buf_idx
>= st
->dma_st
.rx_buf_sz
)
1524 st
->dma_st
.buf_idx
= 0;
1527 /* adjust saved time for next transfer handling */
1528 st
->dma_st
.dma_ts
= iio_get_time_ns(indio_dev
);
1531 static irqreturn_t
at91_adc_trigger_handler(int irq
, void *p
)
1533 struct iio_poll_func
*pf
= p
;
1534 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1535 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1538 * If it's not our trigger, start a conversion now, as we are
1539 * actually polling the trigger now.
1541 if (iio_trigger_validate_own_device(indio_dev
->trig
, indio_dev
))
1542 at91_adc_writel(st
, CR
, AT91_SAMA5D2_CR_START
);
1544 if (st
->dma_st
.dma_chan
)
1545 at91_adc_trigger_handler_dma(indio_dev
);
1547 at91_adc_trigger_handler_nodma(indio_dev
, pf
);
1549 iio_trigger_notify_done(indio_dev
->trig
);
1554 static unsigned at91_adc_startup_time(unsigned startup_time_min
,
1555 unsigned adc_clk_khz
)
1557 static const unsigned int startup_lookup
[] = {
1563 unsigned ticks_min
, i
;
1566 * Since the adc frequency is checked before, there is no reason
1567 * to not meet the startup time constraint.
1570 ticks_min
= startup_time_min
* adc_clk_khz
/ 1000;
1571 for (i
= 0; i
< ARRAY_SIZE(startup_lookup
); i
++)
1572 if (startup_lookup
[i
] > ticks_min
)
1578 static void at91_adc_setup_samp_freq(struct iio_dev
*indio_dev
, unsigned freq
,
1579 unsigned int startup_time
,
1580 unsigned int tracktim
)
1582 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1583 unsigned f_per
, prescal
, startup
, mr
;
1586 f_per
= clk_get_rate(st
->per_clk
);
1587 prescal
= (f_per
/ (2 * freq
)) - 1;
1589 startup
= at91_adc_startup_time(startup_time
, freq
/ 1000);
1591 ret
= pm_runtime_resume_and_get(st
->dev
);
1595 mr
= at91_adc_readl(st
, MR
);
1596 mr
&= ~(AT91_SAMA5D2_MR_STARTUP_MASK
| AT91_SAMA5D2_MR_PRESCAL_MASK
);
1597 mr
|= AT91_SAMA5D2_MR_STARTUP(startup
);
1598 mr
|= AT91_SAMA5D2_MR_PRESCAL(prescal
);
1599 mr
|= AT91_SAMA5D2_MR_TRACKTIM(tracktim
);
1600 at91_adc_writel(st
, MR
, mr
);
1602 pm_runtime_mark_last_busy(st
->dev
);
1603 pm_runtime_put_autosuspend(st
->dev
);
1605 dev_dbg(&indio_dev
->dev
, "freq: %u, startup: %u, prescal: %u, tracktim=%u\n",
1606 freq
, startup
, prescal
, tracktim
);
1607 st
->current_sample_rate
= freq
;
1610 static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state
*st
)
1612 return st
->current_sample_rate
;
1615 static void at91_adc_touch_data_handler(struct iio_dev
*indio_dev
)
1617 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1622 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1623 st
->soc_info
.platform
->max_index
+ 1) {
1624 struct iio_chan_spec
const *chan
=
1625 at91_adc_chan_get(indio_dev
, bit
);
1627 if (chan
->type
== IIO_POSITIONRELATIVE
)
1628 at91_adc_read_position(st
, chan
->channel
, &val
);
1629 else if (chan
->type
== IIO_PRESSURE
)
1630 at91_adc_read_pressure(st
, chan
->channel
, &val
);
1633 st
->buffer
[i
] = val
;
1637 * Schedule work to push to buffers.
1638 * This is intended to push to the callback buffer that another driver
1639 * registered. We are still in a handler from our IRQ. If we push
1640 * directly, it means the other driver has it's callback called
1641 * from our IRQ context. Which is something we better avoid.
1642 * Let's schedule it after our IRQ is completed.
1644 schedule_work(&st
->touch_st
.workq
);
1647 static void at91_adc_pen_detect_interrupt(struct at91_adc_state
*st
)
1649 at91_adc_writel(st
, IDR
, AT91_SAMA5D2_IER_PEN
);
1650 at91_adc_writel(st
, IER
, AT91_SAMA5D2_IER_NOPEN
|
1651 AT91_SAMA5D2_IER_XRDY
| AT91_SAMA5D2_IER_YRDY
|
1652 AT91_SAMA5D2_IER_PRDY
);
1653 at91_adc_writel(st
, TRGR
, AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC
|
1654 AT91_SAMA5D2_TRGR_TRGPER(st
->touch_st
.sample_period_val
));
1655 st
->touch_st
.touching
= true;
1658 static void at91_adc_no_pen_detect_interrupt(struct iio_dev
*indio_dev
)
1660 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1662 at91_adc_writel(st
, TRGR
, AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER
);
1663 at91_adc_writel(st
, IDR
, AT91_SAMA5D2_IER_NOPEN
|
1664 AT91_SAMA5D2_IER_XRDY
| AT91_SAMA5D2_IER_YRDY
|
1665 AT91_SAMA5D2_IER_PRDY
);
1666 st
->touch_st
.touching
= false;
1668 at91_adc_touch_data_handler(indio_dev
);
1670 at91_adc_writel(st
, IER
, AT91_SAMA5D2_IER_PEN
);
1673 static void at91_adc_workq_handler(struct work_struct
*workq
)
1675 struct at91_adc_touch
*touch_st
= container_of(workq
,
1676 struct at91_adc_touch
, workq
);
1677 struct at91_adc_state
*st
= container_of(touch_st
,
1678 struct at91_adc_state
, touch_st
);
1679 struct iio_dev
*indio_dev
= st
->indio_dev
;
1681 iio_push_to_buffers(indio_dev
, st
->buffer
);
1684 static irqreturn_t
at91_adc_interrupt(int irq
, void *private)
1686 struct iio_dev
*indio
= private;
1687 struct at91_adc_state
*st
= iio_priv(indio
);
1688 u32 status
, eoc
, imr
, eoc_imr
;
1689 u32 rdy_mask
= AT91_SAMA5D2_IER_XRDY
| AT91_SAMA5D2_IER_YRDY
|
1690 AT91_SAMA5D2_IER_PRDY
;
1692 at91_adc_irq_status(st
, &status
, &eoc
);
1693 at91_adc_irq_mask(st
, &imr
, &eoc_imr
);
1695 if (!(status
& imr
) && !(eoc
& eoc_imr
))
1697 if (status
& AT91_SAMA5D2_IER_PEN
) {
1698 /* pen detected IRQ */
1699 at91_adc_pen_detect_interrupt(st
);
1700 } else if ((status
& AT91_SAMA5D2_IER_NOPEN
)) {
1701 /* nopen detected IRQ */
1702 at91_adc_no_pen_detect_interrupt(indio
);
1703 } else if ((status
& AT91_SAMA5D2_ISR_PENS
) &&
1704 ((status
& rdy_mask
) == rdy_mask
)) {
1705 /* periodic trigger IRQ - during pen sense */
1706 at91_adc_touch_data_handler(indio
);
1707 } else if (status
& AT91_SAMA5D2_ISR_PENS
) {
1709 * touching, but the measurements are not ready yet.
1712 status
= at91_adc_readl(st
, XPOSR
);
1713 status
= at91_adc_readl(st
, YPOSR
);
1714 status
= at91_adc_readl(st
, PRESSR
);
1715 } else if (iio_buffer_enabled(indio
) &&
1716 (status
& AT91_SAMA5D2_IER_DRDY
)) {
1717 /* triggered buffer without DMA */
1718 disable_irq_nosync(irq
);
1719 iio_trigger_poll(indio
->trig
);
1720 } else if (iio_buffer_enabled(indio
) && st
->dma_st
.dma_chan
) {
1721 /* triggered buffer with DMA - should not happen */
1722 disable_irq_nosync(irq
);
1723 WARN(true, "Unexpected irq occurred\n");
1724 } else if (!iio_buffer_enabled(indio
)) {
1725 /* software requested conversion */
1726 st
->conversion_value
= at91_adc_read_chan(st
, st
->chan
->address
);
1727 st
->conversion_done
= true;
1728 wake_up_interruptible(&st
->wq_data_available
);
1733 /* This needs to be called with direct mode claimed and st->lock locked. */
1734 static int at91_adc_read_info_raw(struct iio_dev
*indio_dev
,
1735 struct iio_chan_spec
const *chan
, int *val
)
1737 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1741 ret
= pm_runtime_resume_and_get(st
->dev
);
1746 * Keep in mind that we cannot use software trigger or touchscreen
1747 * if external trigger is enabled
1749 if (chan
->type
== IIO_POSITIONRELATIVE
) {
1750 ret
= at91_adc_read_position(st
, chan
->channel
,
1754 ret
= at91_adc_adjust_val_osr(st
, val
);
1756 goto pm_runtime_put
;
1758 if (chan
->type
== IIO_PRESSURE
) {
1759 ret
= at91_adc_read_pressure(st
, chan
->channel
,
1763 ret
= at91_adc_adjust_val_osr(st
, val
);
1765 goto pm_runtime_put
;
1768 /* in this case we have a voltage or temperature channel */
1772 at91_adc_cor(st
, chan
);
1773 at91_adc_writel(st
, CHER
, BIT(chan
->channel
));
1775 * TEMPMR.TEMPON needs to update after CHER otherwise if none
1776 * of the channels are enabled and TEMPMR.TEMPON = 1 will
1777 * trigger DRDY interruption while preparing for temperature read.
1779 if (chan
->type
== IIO_TEMP
)
1780 at91_adc_writel(st
, TEMPMR
, AT91_SAMA5D2_TEMPMR_TEMPON
);
1781 at91_adc_eoc_ena(st
, chan
->channel
);
1782 at91_adc_writel(st
, CR
, AT91_SAMA5D2_CR_START
);
1784 ret
= wait_event_interruptible_timeout(st
->wq_data_available
,
1785 st
->conversion_done
,
1786 msecs_to_jiffies(1000));
1791 *val
= st
->conversion_value
;
1792 ret
= at91_adc_adjust_val_osr(st
, val
);
1793 if (chan
->scan_type
.sign
== 's')
1794 *val
= sign_extend32(*val
,
1795 chan
->scan_type
.realbits
- 1);
1796 st
->conversion_done
= false;
1799 at91_adc_eoc_dis(st
, st
->chan
->channel
);
1800 if (chan
->type
== IIO_TEMP
)
1801 at91_adc_writel(st
, TEMPMR
, 0U);
1802 at91_adc_writel(st
, CHDR
, BIT(chan
->channel
));
1804 /* Needed to ACK the DRDY interruption */
1805 at91_adc_readl(st
, LCDR
);
1808 pm_runtime_mark_last_busy(st
->dev
);
1809 pm_runtime_put_autosuspend(st
->dev
);
1813 static int at91_adc_read_info_locked(struct iio_dev
*indio_dev
,
1814 struct iio_chan_spec
const *chan
, int *val
)
1816 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1819 ret
= iio_device_claim_direct_mode(indio_dev
);
1823 mutex_lock(&st
->lock
);
1824 ret
= at91_adc_read_info_raw(indio_dev
, chan
, val
);
1825 mutex_unlock(&st
->lock
);
1827 iio_device_release_direct_mode(indio_dev
);
1832 static void at91_adc_temp_sensor_configure(struct at91_adc_state
*st
,
1835 u32 sample_rate
, oversampling_ratio
;
1836 u32 startup_time
, tracktim
, trackx
;
1840 * Configure the sensor for best accuracy: 10MHz frequency,
1841 * oversampling rate of 256, tracktim=0xf and trackx=1.
1843 sample_rate
= 10 * MEGA
;
1844 oversampling_ratio
= 256;
1845 startup_time
= AT91_SAMA5D2_MR_STARTUP_TS_MIN
;
1846 tracktim
= AT91_SAMA5D2_MR_TRACKTIM_TS
;
1847 trackx
= AT91_SAMA5D2_TRACKX_TS
;
1849 st
->temp_st
.saved_sample_rate
= st
->current_sample_rate
;
1850 st
->temp_st
.saved_oversampling
= st
->oversampling_ratio
;
1852 /* Go back to previous settings. */
1853 sample_rate
= st
->temp_st
.saved_sample_rate
;
1854 oversampling_ratio
= st
->temp_st
.saved_oversampling
;
1855 startup_time
= st
->soc_info
.startup_time
;
1860 at91_adc_setup_samp_freq(st
->indio_dev
, sample_rate
, startup_time
,
1862 at91_adc_config_emr(st
, oversampling_ratio
, trackx
);
1865 static int at91_adc_read_temp(struct iio_dev
*indio_dev
,
1866 struct iio_chan_spec
const *chan
, int *val
)
1868 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1869 struct at91_adc_temp_sensor_clb
*clb
= &st
->soc_info
.temp_sensor_clb
;
1872 int ret
, vbg
, vtemp
;
1874 ret
= iio_device_claim_direct_mode(indio_dev
);
1877 mutex_lock(&st
->lock
);
1879 ret
= pm_runtime_resume_and_get(st
->dev
);
1883 at91_adc_temp_sensor_configure(st
, true);
1886 tmp
= at91_adc_readl(st
, ACR
);
1887 tmp
|= AT91_SAMA5D2_ACR_SRCLCH
;
1888 at91_adc_writel(st
, ACR
, tmp
);
1889 ret
= at91_adc_read_info_raw(indio_dev
, chan
, &vbg
);
1891 goto restore_config
;
1894 tmp
&= ~AT91_SAMA5D2_ACR_SRCLCH
;
1895 at91_adc_writel(st
, ACR
, tmp
);
1896 ret
= at91_adc_read_info_raw(indio_dev
, chan
, &vtemp
);
1899 /* Revert previous settings. */
1900 at91_adc_temp_sensor_configure(st
, false);
1901 pm_runtime_mark_last_busy(st
->dev
);
1902 pm_runtime_put_autosuspend(st
->dev
);
1904 mutex_unlock(&st
->lock
);
1905 iio_device_release_direct_mode(indio_dev
);
1910 * Temp[milli] = p1[milli] + (vtemp * clb->p6 - clb->p4 * vbg)/
1911 * (vbg * AT91_ADC_TS_VTEMP_DT)
1913 div1
= DIV_ROUND_CLOSEST_ULL(((u64
)vtemp
* clb
->p6
), vbg
);
1914 div1
= DIV_ROUND_CLOSEST_ULL((div1
* 1000), AT91_ADC_TS_VTEMP_DT
);
1915 div2
= DIV_ROUND_CLOSEST_ULL((u64
)clb
->p4
, AT91_ADC_TS_VTEMP_DT
);
1917 *val
= clb
->p1
+ (int)div1
- (int)div2
;
1922 static int at91_adc_read_raw(struct iio_dev
*indio_dev
,
1923 struct iio_chan_spec
const *chan
,
1924 int *val
, int *val2
, long mask
)
1926 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1929 case IIO_CHAN_INFO_RAW
:
1930 return at91_adc_read_info_locked(indio_dev
, chan
, val
);
1932 case IIO_CHAN_INFO_SCALE
:
1933 *val
= st
->vref_uv
/ 1000;
1934 if (chan
->differential
)
1936 *val2
= chan
->scan_type
.realbits
;
1937 return IIO_VAL_FRACTIONAL_LOG2
;
1939 case IIO_CHAN_INFO_PROCESSED
:
1940 if (chan
->type
!= IIO_TEMP
)
1942 return at91_adc_read_temp(indio_dev
, chan
, val
);
1944 case IIO_CHAN_INFO_SAMP_FREQ
:
1945 *val
= at91_adc_get_sample_freq(st
);
1948 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1949 *val
= st
->oversampling_ratio
;
1957 static int at91_adc_write_raw(struct iio_dev
*indio_dev
,
1958 struct iio_chan_spec
const *chan
,
1959 int val
, int val2
, long mask
)
1961 struct at91_adc_state
*st
= iio_priv(indio_dev
);
1965 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1966 /* if no change, optimize out */
1967 if (val
== st
->oversampling_ratio
)
1970 ret
= iio_device_claim_direct_mode(indio_dev
);
1973 mutex_lock(&st
->lock
);
1975 ret
= at91_adc_config_emr(st
, val
, 0);
1976 mutex_unlock(&st
->lock
);
1977 iio_device_release_direct_mode(indio_dev
);
1979 case IIO_CHAN_INFO_SAMP_FREQ
:
1980 if (val
< st
->soc_info
.min_sample_rate
||
1981 val
> st
->soc_info
.max_sample_rate
)
1984 ret
= iio_device_claim_direct_mode(indio_dev
);
1987 mutex_lock(&st
->lock
);
1988 at91_adc_setup_samp_freq(indio_dev
, val
,
1989 st
->soc_info
.startup_time
, 0);
1990 mutex_unlock(&st
->lock
);
1991 iio_device_release_direct_mode(indio_dev
);
1998 static int at91_adc_read_avail(struct iio_dev
*indio_dev
,
1999 struct iio_chan_spec
const *chan
,
2000 const int **vals
, int *type
, int *length
,
2003 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2006 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
2007 *vals
= (int *)st
->soc_info
.platform
->oversampling_avail
;
2008 *type
= IIO_VAL_INT
;
2009 *length
= st
->soc_info
.platform
->oversampling_avail_no
;
2010 return IIO_AVAIL_LIST
;
2016 static void at91_adc_dma_init(struct at91_adc_state
*st
)
2018 struct device
*dev
= &st
->indio_dev
->dev
;
2019 struct dma_slave_config config
= {0};
2020 /* we have 2 bytes for each channel */
2021 unsigned int sample_size
= st
->soc_info
.platform
->nr_channels
* 2;
2023 * We make the buffer double the size of the fifo,
2024 * such that DMA uses one half of the buffer (full fifo size)
2025 * and the software uses the other half to read/write.
2027 unsigned int pages
= DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE
*
2028 sample_size
* 2, PAGE_SIZE
);
2030 if (st
->dma_st
.dma_chan
)
2033 st
->dma_st
.dma_chan
= dma_request_chan(dev
, "rx");
2034 if (IS_ERR(st
->dma_st
.dma_chan
)) {
2035 dev_info(dev
, "can't get DMA channel\n");
2036 st
->dma_st
.dma_chan
= NULL
;
2040 st
->dma_st
.rx_buf
= dma_alloc_coherent(st
->dma_st
.dma_chan
->device
->dev
,
2042 &st
->dma_st
.rx_dma_buf
,
2044 if (!st
->dma_st
.rx_buf
) {
2045 dev_info(dev
, "can't allocate coherent DMA area\n");
2046 goto dma_chan_disable
;
2049 /* Configure DMA channel to read data register */
2050 config
.direction
= DMA_DEV_TO_MEM
;
2051 config
.src_addr
= (phys_addr_t
)(st
->dma_st
.phys_addr
2052 + st
->soc_info
.platform
->layout
->LCDR
);
2053 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
2054 config
.src_maxburst
= 1;
2055 config
.dst_maxburst
= 1;
2057 if (dmaengine_slave_config(st
->dma_st
.dma_chan
, &config
)) {
2058 dev_info(dev
, "can't configure DMA slave\n");
2062 dev_info(dev
, "using %s for rx DMA transfers\n",
2063 dma_chan_name(st
->dma_st
.dma_chan
));
2068 dma_free_coherent(st
->dma_st
.dma_chan
->device
->dev
, pages
* PAGE_SIZE
,
2069 st
->dma_st
.rx_buf
, st
->dma_st
.rx_dma_buf
);
2071 dma_release_channel(st
->dma_st
.dma_chan
);
2072 st
->dma_st
.dma_chan
= NULL
;
2074 dev_info(dev
, "continuing without DMA support\n");
2077 static void at91_adc_dma_disable(struct at91_adc_state
*st
)
2079 struct device
*dev
= &st
->indio_dev
->dev
;
2080 /* we have 2 bytes for each channel */
2081 unsigned int sample_size
= st
->soc_info
.platform
->nr_channels
* 2;
2082 unsigned int pages
= DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE
*
2083 sample_size
* 2, PAGE_SIZE
);
2085 /* if we are not using DMA, just return */
2086 if (!st
->dma_st
.dma_chan
)
2089 /* wait for all transactions to be terminated first*/
2090 dmaengine_terminate_sync(st
->dma_st
.dma_chan
);
2092 dma_free_coherent(st
->dma_st
.dma_chan
->device
->dev
, pages
* PAGE_SIZE
,
2093 st
->dma_st
.rx_buf
, st
->dma_st
.rx_dma_buf
);
2094 dma_release_channel(st
->dma_st
.dma_chan
);
2095 st
->dma_st
.dma_chan
= NULL
;
2097 dev_info(dev
, "continuing without DMA support\n");
2100 static int at91_adc_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
2102 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2105 if (val
> AT91_HWFIFO_MAX_SIZE
)
2106 val
= AT91_HWFIFO_MAX_SIZE
;
2108 if (!st
->selected_trig
->hw_trig
) {
2109 dev_dbg(&indio_dev
->dev
, "we need hw trigger for DMA\n");
2113 dev_dbg(&indio_dev
->dev
, "new watermark is %u\n", val
);
2114 st
->dma_st
.watermark
= val
;
2117 * The logic here is: if we have watermark 1, it means we do
2118 * each conversion with it's own IRQ, thus we don't need DMA.
2119 * If the watermark is higher, we do DMA to do all the transfers in bulk
2123 at91_adc_dma_disable(st
);
2125 at91_adc_dma_init(st
);
2128 * We can start the DMA only after setting the watermark and
2129 * having the DMA initialization completed
2131 ret
= at91_adc_buffer_prepare(indio_dev
);
2133 at91_adc_dma_disable(st
);
2138 static int at91_adc_update_scan_mode(struct iio_dev
*indio_dev
,
2139 const unsigned long *scan_mask
)
2141 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2143 if (bitmap_subset(scan_mask
, &st
->touch_st
.channels_bitmask
,
2144 st
->soc_info
.platform
->max_index
+ 1))
2147 * if the new bitmap is a combination of touchscreen and regular
2148 * channels, then we are not fine
2150 if (bitmap_intersects(&st
->touch_st
.channels_bitmask
, scan_mask
,
2151 st
->soc_info
.platform
->max_index
+ 1))
2156 static void at91_adc_hw_init(struct iio_dev
*indio_dev
)
2158 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2160 at91_adc_writel(st
, CR
, AT91_SAMA5D2_CR_SWRST
);
2161 if (st
->soc_info
.platform
->layout
->EOC_IDR
)
2162 at91_adc_writel(st
, EOC_IDR
, 0xffffffff);
2163 at91_adc_writel(st
, IDR
, 0xffffffff);
2165 * Transfer field must be set to 2 according to the datasheet and
2166 * allows different analog settings for each channel.
2168 at91_adc_writel(st
, MR
,
2169 AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH
);
2171 at91_adc_setup_samp_freq(indio_dev
, st
->soc_info
.min_sample_rate
,
2172 st
->soc_info
.startup_time
, 0);
2174 /* configure extended mode register */
2175 at91_adc_config_emr(st
, st
->oversampling_ratio
, 0);
2178 static ssize_t
at91_adc_get_fifo_state(struct device
*dev
,
2179 struct device_attribute
*attr
, char *buf
)
2181 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
2182 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2184 return sysfs_emit(buf
, "%d\n", !!st
->dma_st
.dma_chan
);
2187 static ssize_t
at91_adc_get_watermark(struct device
*dev
,
2188 struct device_attribute
*attr
, char *buf
)
2190 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
2191 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2193 return sysfs_emit(buf
, "%d\n", st
->dma_st
.watermark
);
2196 static IIO_DEVICE_ATTR(hwfifo_enabled
, 0444,
2197 at91_adc_get_fifo_state
, NULL
, 0);
2198 static IIO_DEVICE_ATTR(hwfifo_watermark
, 0444,
2199 at91_adc_get_watermark
, NULL
, 0);
2201 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min
, "2");
2202 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max
, AT91_HWFIFO_MAX_SIZE_STR
);
2204 static const struct iio_dev_attr
*at91_adc_fifo_attributes
[] = {
2205 &iio_dev_attr_hwfifo_watermark_min
,
2206 &iio_dev_attr_hwfifo_watermark_max
,
2207 &iio_dev_attr_hwfifo_watermark
,
2208 &iio_dev_attr_hwfifo_enabled
,
2212 static const struct iio_info at91_adc_info
= {
2213 .read_avail
= &at91_adc_read_avail
,
2214 .read_raw
= &at91_adc_read_raw
,
2215 .write_raw
= &at91_adc_write_raw
,
2216 .update_scan_mode
= &at91_adc_update_scan_mode
,
2217 .fwnode_xlate
= &at91_adc_fwnode_xlate
,
2218 .hwfifo_set_watermark
= &at91_adc_set_watermark
,
2221 static int at91_adc_buffer_and_trigger_init(struct device
*dev
,
2222 struct iio_dev
*indio
)
2224 struct at91_adc_state
*st
= iio_priv(indio
);
2225 const struct iio_dev_attr
**fifo_attrs
;
2228 if (st
->selected_trig
->hw_trig
)
2229 fifo_attrs
= at91_adc_fifo_attributes
;
2233 ret
= devm_iio_triggered_buffer_setup_ext(&indio
->dev
, indio
,
2234 &iio_pollfunc_store_time
, &at91_adc_trigger_handler
,
2235 IIO_BUFFER_DIRECTION_IN
, &at91_buffer_setup_ops
, fifo_attrs
);
2237 dev_err(dev
, "couldn't initialize the buffer.\n");
2241 if (!st
->selected_trig
->hw_trig
)
2244 st
->trig
= at91_adc_allocate_trigger(indio
, st
->selected_trig
->name
);
2245 if (IS_ERR(st
->trig
)) {
2246 dev_err(dev
, "could not allocate trigger\n");
2247 return PTR_ERR(st
->trig
);
2251 * Initially the iio buffer has a length of 2 and
2254 st
->dma_st
.watermark
= 1;
2259 static int at91_adc_temp_sensor_init(struct at91_adc_state
*st
,
2262 struct at91_adc_temp_sensor_clb
*clb
= &st
->soc_info
.temp_sensor_clb
;
2263 struct nvmem_cell
*temp_calib
;
2268 if (!st
->soc_info
.platform
->temp_sensor
)
2271 /* Get the calibration data from NVMEM. */
2272 temp_calib
= devm_nvmem_cell_get(dev
, "temperature_calib");
2273 if (IS_ERR(temp_calib
)) {
2274 ret
= PTR_ERR(temp_calib
);
2276 dev_err(dev
, "Failed to get temperature_calib cell!\n");
2280 buf
= nvmem_cell_read(temp_calib
, &len
);
2282 dev_err(dev
, "Failed to read calibration data!\n");
2283 return PTR_ERR(buf
);
2285 if (len
< AT91_ADC_TS_CLB_IDX_MAX
* 4) {
2286 dev_err(dev
, "Invalid calibration data!\n");
2291 /* Store calibration data for later use. */
2292 clb
->p1
= buf
[AT91_ADC_TS_CLB_IDX_P1
];
2293 clb
->p4
= buf
[AT91_ADC_TS_CLB_IDX_P4
];
2294 clb
->p6
= buf
[AT91_ADC_TS_CLB_IDX_P6
];
2297 * We prepare here the conversion to milli to avoid doing it on hotpath.
2299 clb
->p1
= clb
->p1
* 1000;
2306 static int at91_adc_probe(struct platform_device
*pdev
)
2308 struct device
*dev
= &pdev
->dev
;
2309 struct iio_dev
*indio_dev
;
2310 struct at91_adc_state
*st
;
2311 struct resource
*res
;
2312 int ret
, i
, num_channels
;
2313 u32 edge_type
= IRQ_TYPE_NONE
;
2315 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*st
));
2319 st
= iio_priv(indio_dev
);
2320 st
->indio_dev
= indio_dev
;
2322 st
->soc_info
.platform
= device_get_match_data(dev
);
2324 ret
= at91_adc_temp_sensor_init(st
, &pdev
->dev
);
2325 /* Don't register temperature channel if initialization failed. */
2327 num_channels
= st
->soc_info
.platform
->max_channels
- 1;
2329 num_channels
= st
->soc_info
.platform
->max_channels
;
2331 indio_dev
->name
= dev_name(&pdev
->dev
);
2332 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
2333 indio_dev
->info
= &at91_adc_info
;
2334 indio_dev
->channels
= *st
->soc_info
.platform
->adc_channels
;
2335 indio_dev
->num_channels
= num_channels
;
2337 bitmap_set(&st
->touch_st
.channels_bitmask
,
2338 st
->soc_info
.platform
->touch_chan_x
, 1);
2339 bitmap_set(&st
->touch_st
.channels_bitmask
,
2340 st
->soc_info
.platform
->touch_chan_y
, 1);
2341 bitmap_set(&st
->touch_st
.channels_bitmask
,
2342 st
->soc_info
.platform
->touch_chan_p
, 1);
2344 st
->oversampling_ratio
= 1;
2346 ret
= device_property_read_u32(dev
, "atmel,min-sample-rate-hz",
2347 &st
->soc_info
.min_sample_rate
);
2350 "invalid or missing value for atmel,min-sample-rate-hz\n");
2354 ret
= device_property_read_u32(dev
, "atmel,max-sample-rate-hz",
2355 &st
->soc_info
.max_sample_rate
);
2358 "invalid or missing value for atmel,max-sample-rate-hz\n");
2362 ret
= device_property_read_u32(dev
, "atmel,startup-time-ms",
2363 &st
->soc_info
.startup_time
);
2366 "invalid or missing value for atmel,startup-time-ms\n");
2370 ret
= device_property_read_u32(dev
, "atmel,trigger-edge-type",
2374 "atmel,trigger-edge-type not specified, only software trigger available\n");
2377 st
->selected_trig
= NULL
;
2379 /* find the right trigger, or no trigger at all */
2380 for (i
= 0; i
< st
->soc_info
.platform
->hw_trig_cnt
+ 1; i
++)
2381 if (at91_adc_trigger_list
[i
].edge_type
== edge_type
) {
2382 st
->selected_trig
= &at91_adc_trigger_list
[i
];
2386 if (!st
->selected_trig
) {
2387 dev_err(&pdev
->dev
, "invalid external trigger edge value\n");
2391 init_waitqueue_head(&st
->wq_data_available
);
2392 mutex_init(&st
->lock
);
2393 INIT_WORK(&st
->touch_st
.workq
, at91_adc_workq_handler
);
2395 st
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
2396 if (IS_ERR(st
->base
))
2397 return PTR_ERR(st
->base
);
2399 /* if we plan to use DMA, we need the physical address of the regs */
2400 st
->dma_st
.phys_addr
= res
->start
;
2402 st
->irq
= platform_get_irq(pdev
, 0);
2406 st
->per_clk
= devm_clk_get(&pdev
->dev
, "adc_clk");
2407 if (IS_ERR(st
->per_clk
))
2408 return PTR_ERR(st
->per_clk
);
2410 st
->reg
= devm_regulator_get(&pdev
->dev
, "vddana");
2411 if (IS_ERR(st
->reg
))
2412 return PTR_ERR(st
->reg
);
2414 st
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
2415 if (IS_ERR(st
->vref
))
2416 return PTR_ERR(st
->vref
);
2418 ret
= devm_request_irq(&pdev
->dev
, st
->irq
, at91_adc_interrupt
, 0,
2419 pdev
->dev
.driver
->name
, indio_dev
);
2423 ret
= regulator_enable(st
->reg
);
2427 ret
= regulator_enable(st
->vref
);
2431 st
->vref_uv
= regulator_get_voltage(st
->vref
);
2432 if (st
->vref_uv
<= 0) {
2437 ret
= clk_prepare_enable(st
->per_clk
);
2441 platform_set_drvdata(pdev
, indio_dev
);
2442 st
->dev
= &pdev
->dev
;
2443 pm_runtime_set_autosuspend_delay(st
->dev
, 500);
2444 pm_runtime_use_autosuspend(st
->dev
);
2445 pm_runtime_set_active(st
->dev
);
2446 pm_runtime_enable(st
->dev
);
2447 pm_runtime_get_noresume(st
->dev
);
2449 at91_adc_hw_init(indio_dev
);
2451 ret
= at91_adc_buffer_and_trigger_init(&pdev
->dev
, indio_dev
);
2453 goto err_pm_disable
;
2455 if (dma_coerce_mask_and_coherent(&indio_dev
->dev
, DMA_BIT_MASK(32)))
2456 dev_info(&pdev
->dev
, "cannot set DMA mask to 32-bit\n");
2458 ret
= iio_device_register(indio_dev
);
2462 if (st
->selected_trig
->hw_trig
)
2463 dev_info(&pdev
->dev
, "setting up trigger as %s\n",
2464 st
->selected_trig
->name
);
2466 dev_info(&pdev
->dev
, "version: %x\n",
2467 readl_relaxed(st
->base
+ st
->soc_info
.platform
->layout
->VERSION
));
2469 pm_runtime_mark_last_busy(st
->dev
);
2470 pm_runtime_put_autosuspend(st
->dev
);
2475 at91_adc_dma_disable(st
);
2477 pm_runtime_put_noidle(st
->dev
);
2478 pm_runtime_disable(st
->dev
);
2479 pm_runtime_set_suspended(st
->dev
);
2480 pm_runtime_dont_use_autosuspend(st
->dev
);
2481 clk_disable_unprepare(st
->per_clk
);
2483 regulator_disable(st
->vref
);
2485 regulator_disable(st
->reg
);
2489 static void at91_adc_remove(struct platform_device
*pdev
)
2491 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
2492 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2494 iio_device_unregister(indio_dev
);
2496 at91_adc_dma_disable(st
);
2498 pm_runtime_disable(st
->dev
);
2499 pm_runtime_set_suspended(st
->dev
);
2500 clk_disable_unprepare(st
->per_clk
);
2502 regulator_disable(st
->vref
);
2503 regulator_disable(st
->reg
);
2506 static int at91_adc_suspend(struct device
*dev
)
2508 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2509 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2512 ret
= pm_runtime_resume_and_get(st
->dev
);
2516 if (iio_buffer_enabled(indio_dev
))
2517 at91_adc_buffer_postdisable(indio_dev
);
2520 * Do a sofware reset of the ADC before we go to suspend.
2521 * this will ensure that all pins are free from being muxed by the ADC
2522 * and can be used by for other devices.
2523 * Otherwise, ADC will hog them and we can't go to suspend mode.
2525 at91_adc_writel(st
, CR
, AT91_SAMA5D2_CR_SWRST
);
2527 pm_runtime_mark_last_busy(st
->dev
);
2528 pm_runtime_put_noidle(st
->dev
);
2529 clk_disable_unprepare(st
->per_clk
);
2530 regulator_disable(st
->vref
);
2531 regulator_disable(st
->reg
);
2533 return pinctrl_pm_select_sleep_state(dev
);
2536 static int at91_adc_resume(struct device
*dev
)
2538 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2539 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2542 ret
= pinctrl_pm_select_default_state(dev
);
2546 ret
= regulator_enable(st
->reg
);
2550 ret
= regulator_enable(st
->vref
);
2552 goto reg_disable_resume
;
2554 ret
= clk_prepare_enable(st
->per_clk
);
2556 goto vref_disable_resume
;
2558 pm_runtime_get_noresume(st
->dev
);
2560 at91_adc_hw_init(indio_dev
);
2562 /* reconfiguring trigger hardware state */
2563 if (iio_buffer_enabled(indio_dev
)) {
2564 ret
= at91_adc_buffer_prepare(indio_dev
);
2566 goto pm_runtime_put
;
2568 at91_adc_configure_trigger_registers(st
, true);
2571 pm_runtime_mark_last_busy(st
->dev
);
2572 pm_runtime_put_autosuspend(st
->dev
);
2577 pm_runtime_mark_last_busy(st
->dev
);
2578 pm_runtime_put_noidle(st
->dev
);
2579 clk_disable_unprepare(st
->per_clk
);
2580 vref_disable_resume
:
2581 regulator_disable(st
->vref
);
2583 regulator_disable(st
->reg
);
2585 dev_err(&indio_dev
->dev
, "failed to resume\n");
2589 static int at91_adc_runtime_suspend(struct device
*dev
)
2591 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2592 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2594 clk_disable(st
->per_clk
);
2599 static int at91_adc_runtime_resume(struct device
*dev
)
2601 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2602 struct at91_adc_state
*st
= iio_priv(indio_dev
);
2604 return clk_enable(st
->per_clk
);
2607 static const struct dev_pm_ops at91_adc_pm_ops
= {
2608 SYSTEM_SLEEP_PM_OPS(at91_adc_suspend
, at91_adc_resume
)
2609 RUNTIME_PM_OPS(at91_adc_runtime_suspend
, at91_adc_runtime_resume
,
2613 static const struct of_device_id at91_adc_dt_match
[] = {
2615 .compatible
= "atmel,sama5d2-adc",
2616 .data
= (const void *)&sama5d2_platform
,
2618 .compatible
= "microchip,sama7g5-adc",
2619 .data
= (const void *)&sama7g5_platform
,
2624 MODULE_DEVICE_TABLE(of
, at91_adc_dt_match
);
2626 static struct platform_driver at91_adc_driver
= {
2627 .probe
= at91_adc_probe
,
2628 .remove
= at91_adc_remove
,
2630 .name
= "at91-sama5d2_adc",
2631 .of_match_table
= at91_adc_dt_match
,
2632 .pm
= pm_ptr(&at91_adc_pm_ops
),
2635 module_platform_driver(at91_adc_driver
)
2637 MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@microchip.com>");
2638 MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com");
2639 MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
2640 MODULE_LICENSE("GPL v2");