1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2021 Xilinx, Inc.
7 * Manish Narani <mnarani@xilinx.com>
8 * Rajnikant Bhojani <rajnikant.bhojani@xilinx.com>
11 #include <linux/bits.h>
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/devm-helpers.h>
16 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/overflow.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/slab.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/iio.h>
30 /* AMS registers definitions */
31 #define AMS_ISR_0 0x010
32 #define AMS_ISR_1 0x014
33 #define AMS_IER_0 0x020
34 #define AMS_IER_1 0x024
35 #define AMS_IDR_0 0x028
36 #define AMS_IDR_1 0x02C
37 #define AMS_PS_CSTS 0x040
38 #define AMS_PL_CSTS 0x044
40 #define AMS_VCC_PSPLL0 0x060
41 #define AMS_VCC_PSPLL3 0x06C
42 #define AMS_VCCINT 0x078
43 #define AMS_VCCBRAM 0x07C
44 #define AMS_VCCAUX 0x080
45 #define AMS_PSDDRPLL 0x084
46 #define AMS_PSINTFPDDR 0x09C
48 #define AMS_VCC_PSPLL0_CH 48
49 #define AMS_VCC_PSPLL3_CH 51
50 #define AMS_VCCINT_CH 54
51 #define AMS_VCCBRAM_CH 55
52 #define AMS_VCCAUX_CH 56
53 #define AMS_PSDDRPLL_CH 57
54 #define AMS_PSINTFPDDR_CH 63
56 #define AMS_REG_CONFIG0 0x100
57 #define AMS_REG_CONFIG1 0x104
58 #define AMS_REG_CONFIG3 0x10C
59 #define AMS_REG_CONFIG4 0x110
60 #define AMS_REG_SEQ_CH0 0x120
61 #define AMS_REG_SEQ_CH1 0x124
62 #define AMS_REG_SEQ_CH2 0x118
64 #define AMS_VUSER0_MASK BIT(0)
65 #define AMS_VUSER1_MASK BIT(1)
66 #define AMS_VUSER2_MASK BIT(2)
67 #define AMS_VUSER3_MASK BIT(3)
69 #define AMS_TEMP 0x000
70 #define AMS_SUPPLY1 0x004
71 #define AMS_SUPPLY2 0x008
72 #define AMS_VP_VN 0x00C
73 #define AMS_VREFP 0x010
74 #define AMS_VREFN 0x014
75 #define AMS_SUPPLY3 0x018
76 #define AMS_SUPPLY4 0x034
77 #define AMS_SUPPLY5 0x038
78 #define AMS_SUPPLY6 0x03C
79 #define AMS_SUPPLY7 0x200
80 #define AMS_SUPPLY8 0x204
81 #define AMS_SUPPLY9 0x208
82 #define AMS_SUPPLY10 0x20C
83 #define AMS_VCCAMS 0x210
84 #define AMS_TEMP_REMOTE 0x214
86 #define AMS_REG_VAUX(x) (0x40 + 4 * (x))
88 #define AMS_PS_RESET_VALUE 0xFFFF
89 #define AMS_PL_RESET_VALUE 0xFFFF
91 #define AMS_CONF0_CHANNEL_NUM_MASK GENMASK(6, 0)
93 #define AMS_CONF1_SEQ_MASK GENMASK(15, 12)
94 #define AMS_CONF1_SEQ_DEFAULT FIELD_PREP(AMS_CONF1_SEQ_MASK, 0)
95 #define AMS_CONF1_SEQ_CONTINUOUS FIELD_PREP(AMS_CONF1_SEQ_MASK, 2)
96 #define AMS_CONF1_SEQ_SINGLE_CHANNEL FIELD_PREP(AMS_CONF1_SEQ_MASK, 3)
98 #define AMS_REG_SEQ0_MASK GENMASK(15, 0)
99 #define AMS_REG_SEQ2_MASK GENMASK(21, 16)
100 #define AMS_REG_SEQ1_MASK GENMASK_ULL(37, 22)
102 #define AMS_PS_SEQ_MASK GENMASK(21, 0)
103 #define AMS_PL_SEQ_MASK GENMASK_ULL(59, 22)
105 #define AMS_ALARM_TEMP 0x140
106 #define AMS_ALARM_SUPPLY1 0x144
107 #define AMS_ALARM_SUPPLY2 0x148
108 #define AMS_ALARM_SUPPLY3 0x160
109 #define AMS_ALARM_SUPPLY4 0x164
110 #define AMS_ALARM_SUPPLY5 0x168
111 #define AMS_ALARM_SUPPLY6 0x16C
112 #define AMS_ALARM_SUPPLY7 0x180
113 #define AMS_ALARM_SUPPLY8 0x184
114 #define AMS_ALARM_SUPPLY9 0x188
115 #define AMS_ALARM_SUPPLY10 0x18C
116 #define AMS_ALARM_VCCAMS 0x190
117 #define AMS_ALARM_TEMP_REMOTE 0x194
118 #define AMS_ALARM_THRESHOLD_OFF_10 0x10
119 #define AMS_ALARM_THRESHOLD_OFF_20 0x20
121 #define AMS_ALARM_THR_DIRECT_MASK BIT(1)
122 #define AMS_ALARM_THR_MIN 0x0000
123 #define AMS_ALARM_THR_MAX (BIT(16) - 1)
125 #define AMS_ALARM_MASK GENMASK_ULL(63, 0)
126 #define AMS_NO_OF_ALARMS 32
127 #define AMS_PL_ALARM_START 16
128 #define AMS_PL_ALARM_MASK GENMASK(31, 16)
129 #define AMS_ISR0_ALARM_MASK GENMASK(31, 0)
130 #define AMS_ISR1_ALARM_MASK (GENMASK(31, 29) | GENMASK(4, 0))
131 #define AMS_ISR1_EOC_MASK BIT(3)
132 #define AMS_ISR1_INTR_MASK GENMASK_ULL(63, 32)
133 #define AMS_ISR0_ALARM_2_TO_0_MASK GENMASK(2, 0)
134 #define AMS_ISR0_ALARM_6_TO_3_MASK GENMASK(6, 3)
135 #define AMS_ISR0_ALARM_12_TO_7_MASK GENMASK(13, 8)
136 #define AMS_CONF1_ALARM_2_TO_0_MASK GENMASK(3, 1)
137 #define AMS_CONF1_ALARM_6_TO_3_MASK GENMASK(11, 8)
138 #define AMS_CONF1_ALARM_12_TO_7_MASK GENMASK(5, 0)
139 #define AMS_REGCFG1_ALARM_MASK \
140 (AMS_CONF1_ALARM_2_TO_0_MASK | AMS_CONF1_ALARM_6_TO_3_MASK | BIT(0))
141 #define AMS_REGCFG3_ALARM_MASK AMS_CONF1_ALARM_12_TO_7_MASK
143 #define AMS_PS_CSTS_PS_READY (BIT(27) | BIT(16))
144 #define AMS_PL_CSTS_ACCESS_MASK BIT(1)
146 #define AMS_PL_MAX_FIXED_CHANNEL 10
147 #define AMS_PL_MAX_EXT_CHANNEL 20
149 #define AMS_INIT_POLL_TIME_US 200
150 #define AMS_INIT_TIMEOUT_US 10000
151 #define AMS_UNMASK_TIMEOUT_MS 500
154 * Following scale and offset value is derived from
155 * UG580 (v1.7) December 20, 2016
157 #define AMS_SUPPLY_SCALE_1VOLT_mV 1000
158 #define AMS_SUPPLY_SCALE_3VOLT_mV 3000
159 #define AMS_SUPPLY_SCALE_6VOLT_mV 6000
160 #define AMS_SUPPLY_SCALE_DIV_BIT 16
162 #define AMS_TEMP_SCALE 509314
163 #define AMS_TEMP_SCALE_DIV_BIT 16
164 #define AMS_TEMP_OFFSET -((280230LL << 16) / 509314)
167 AMS_ALARM_BIT_TEMP = 0,
168 AMS_ALARM_BIT_SUPPLY1 = 1,
169 AMS_ALARM_BIT_SUPPLY2 = 2,
170 AMS_ALARM_BIT_SUPPLY3 = 3,
171 AMS_ALARM_BIT_SUPPLY4 = 4,
172 AMS_ALARM_BIT_SUPPLY5 = 5,
173 AMS_ALARM_BIT_SUPPLY6 = 6,
174 AMS_ALARM_BIT_RESERVED = 7,
175 AMS_ALARM_BIT_SUPPLY7 = 8,
176 AMS_ALARM_BIT_SUPPLY8 = 9,
177 AMS_ALARM_BIT_SUPPLY9 = 10,
178 AMS_ALARM_BIT_SUPPLY10 = 11,
179 AMS_ALARM_BIT_VCCAMS = 12,
180 AMS_ALARM_BIT_TEMP_REMOTE = 13,
184 AMS_SEQ_VCC_PSPLL = 0,
185 AMS_SEQ_VCC_PSBATT = 1,
189 AMS_SEQ_PSDDRPLL = 5,
204 AMS_SEQ_SUPPLY1 = 10,
208 AMS_SEQ_SUPPLY3 = 14,
209 AMS_SEQ_CURRENT_MON = 15,
210 AMS_SEQ_SUPPLY7 = 16,
211 AMS_SEQ_SUPPLY8 = 17,
212 AMS_SEQ_SUPPLY9 = 18,
213 AMS_SEQ_SUPPLY10 = 19,
215 AMS_SEQ_TEMP_REMOTE = 21,
219 #define AMS_PS_SEQ_MAX AMS_SEQ_MAX
220 #define AMS_SEQ(x) (AMS_SEQ_MAX + (x))
221 #define PS_SEQ(x) (x)
222 #define PL_SEQ(x) (AMS_PS_SEQ_MAX + (x))
223 #define AMS_CTRL_SEQ_BASE (AMS_PS_SEQ_MAX * 3)
225 #define AMS_CHAN_TEMP(_scan_index, _addr) { \
228 .address = (_addr), \
229 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
230 BIT(IIO_CHAN_INFO_SCALE) | \
231 BIT(IIO_CHAN_INFO_OFFSET), \
232 .event_spec = ams_temp_events, \
233 .scan_index = _scan_index, \
234 .num_event_specs = ARRAY_SIZE(ams_temp_events), \
237 #define AMS_CHAN_VOLTAGE(_scan_index, _addr, _alarm) { \
238 .type = IIO_VOLTAGE, \
240 .address = (_addr), \
241 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
242 BIT(IIO_CHAN_INFO_SCALE), \
243 .event_spec = (_alarm) ? ams_voltage_events : NULL, \
244 .scan_index = _scan_index, \
245 .num_event_specs = (_alarm) ? ARRAY_SIZE(ams_voltage_events) : 0, \
248 #define AMS_PS_CHAN_TEMP(_scan_index, _addr) \
249 AMS_CHAN_TEMP(PS_SEQ(_scan_index), _addr)
250 #define AMS_PS_CHAN_VOLTAGE(_scan_index, _addr) \
251 AMS_CHAN_VOLTAGE(PS_SEQ(_scan_index), _addr, true)
253 #define AMS_PL_CHAN_TEMP(_scan_index, _addr) \
254 AMS_CHAN_TEMP(PL_SEQ(_scan_index), _addr)
255 #define AMS_PL_CHAN_VOLTAGE(_scan_index, _addr, _alarm) \
256 AMS_CHAN_VOLTAGE(PL_SEQ(_scan_index), _addr, _alarm)
257 #define AMS_PL_AUX_CHAN_VOLTAGE(_auxno) \
258 AMS_CHAN_VOLTAGE(PL_SEQ(AMS_SEQ(_auxno)), AMS_REG_VAUX(_auxno), false)
259 #define AMS_CTRL_CHAN_VOLTAGE(_scan_index, _addr) \
260 AMS_CHAN_VOLTAGE(PL_SEQ(AMS_SEQ(AMS_SEQ(_scan_index))), _addr, false)
263 * struct ams - This structure contains necessary state for xilinx-ams to operate
264 * @base: physical base address of device
265 * @ps_base: physical base address of PS device
266 * @pl_base: physical base address of PL device
267 * @clk: clocks associated with the device
268 * @dev: pointer to device struct
269 * @lock: to handle multiple user interaction
270 * @intr_lock: to protect interrupt mask values
271 * @alarm_mask: alarm configuration
272 * @current_masked_alarm: currently masked due to alarm
273 * @intr_mask: interrupt configuration
274 * @ams_unmask_work: re-enables event once the event condition disappears
279 void __iomem *ps_base;
280 void __iomem *pl_base;
284 spinlock_t intr_lock;
285 unsigned int alarm_mask;
286 unsigned int current_masked_alarm;
288 struct delayed_work ams_unmask_work;
291 static inline void ams_ps_update_reg(struct ams *ams, unsigned int offset,
296 val = readl(ams->ps_base + offset);
297 regval = (val & ~mask) | (data & mask);
298 writel(regval, ams->ps_base + offset);
301 static inline void ams_pl_update_reg(struct ams *ams, unsigned int offset,
306 val = readl(ams->pl_base + offset);
307 regval = (val & ~mask) | (data & mask);
308 writel(regval, ams->pl_base + offset);
311 static void ams_update_intrmask(struct ams *ams, u64 mask, u64 val)
315 ams->intr_mask = (ams->intr_mask & ~mask) | (val & mask);
317 regval = ~(ams->intr_mask | ams->current_masked_alarm);
318 writel(regval, ams->base + AMS_IER_0);
320 regval = ~(FIELD_GET(AMS_ISR1_INTR_MASK, ams->intr_mask));
321 writel(regval, ams->base + AMS_IER_1);
323 regval = ams->intr_mask | ams->current_masked_alarm;
324 writel(regval, ams->base + AMS_IDR_0);
326 regval = FIELD_GET(AMS_ISR1_INTR_MASK, ams->intr_mask);
327 writel(regval, ams->base + AMS_IDR_1);
330 static void ams_disable_all_alarms(struct ams *ams)
332 /* disable PS module alarm */
334 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK,
335 AMS_REGCFG1_ALARM_MASK);
336 ams_ps_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK,
337 AMS_REGCFG3_ALARM_MASK);
340 /* disable PL module alarm */
342 ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK,
343 AMS_REGCFG1_ALARM_MASK);
344 ams_pl_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK,
345 AMS_REGCFG3_ALARM_MASK);
349 static void ams_update_ps_alarm(struct ams *ams, unsigned long alarm_mask)
354 val = FIELD_GET(AMS_ISR0_ALARM_2_TO_0_MASK, alarm_mask);
355 cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_2_TO_0_MASK, val));
357 val = FIELD_GET(AMS_ISR0_ALARM_6_TO_3_MASK, alarm_mask);
358 cfg &= ~(FIELD_PREP(AMS_CONF1_ALARM_6_TO_3_MASK, val));
360 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK, cfg);
362 val = FIELD_GET(AMS_ISR0_ALARM_12_TO_7_MASK, alarm_mask);
363 cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_12_TO_7_MASK, val));
364 ams_ps_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK, cfg);
367 static void ams_update_pl_alarm(struct ams *ams, unsigned long alarm_mask)
369 unsigned long pl_alarm_mask;
373 pl_alarm_mask = FIELD_GET(AMS_PL_ALARM_MASK, alarm_mask);
375 val = FIELD_GET(AMS_ISR0_ALARM_2_TO_0_MASK, pl_alarm_mask);
376 cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_2_TO_0_MASK, val));
378 val = FIELD_GET(AMS_ISR0_ALARM_6_TO_3_MASK, pl_alarm_mask);
379 cfg &= ~(FIELD_PREP(AMS_CONF1_ALARM_6_TO_3_MASK, val));
381 ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_REGCFG1_ALARM_MASK, cfg);
383 val = FIELD_GET(AMS_ISR0_ALARM_12_TO_7_MASK, pl_alarm_mask);
384 cfg = ~(FIELD_PREP(AMS_CONF1_ALARM_12_TO_7_MASK, val));
385 ams_pl_update_reg(ams, AMS_REG_CONFIG3, AMS_REGCFG3_ALARM_MASK, cfg);
388 static void ams_update_alarm(struct ams *ams, unsigned long alarm_mask)
393 ams_update_ps_alarm(ams, alarm_mask);
396 ams_update_pl_alarm(ams, alarm_mask);
398 spin_lock_irqsave(&ams->intr_lock, flags);
399 ams_update_intrmask(ams, AMS_ISR0_ALARM_MASK, ~alarm_mask);
400 spin_unlock_irqrestore(&ams->intr_lock, flags);
403 static void ams_enable_channel_sequence(struct iio_dev *indio_dev)
405 struct ams *ams = iio_priv(indio_dev);
406 unsigned long long scan_mask;
411 * Enable channel sequence. First 22 bits of scan_mask represent
412 * PS channels, and next remaining bits represent PL channels.
415 /* Run calibration of PS & PL as part of the sequence */
416 scan_mask = BIT(0) | BIT(AMS_PS_SEQ_MAX);
417 for (i = 0; i < indio_dev->num_channels; i++)
418 scan_mask |= BIT_ULL(indio_dev->channels[i].scan_index);
421 /* put sysmon in a soft reset to change the sequence */
422 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
423 AMS_CONF1_SEQ_DEFAULT);
425 /* configure basic channels */
426 regval = FIELD_GET(AMS_REG_SEQ0_MASK, scan_mask);
427 writel(regval, ams->ps_base + AMS_REG_SEQ_CH0);
429 regval = FIELD_GET(AMS_REG_SEQ2_MASK, scan_mask);
430 writel(regval, ams->ps_base + AMS_REG_SEQ_CH2);
432 /* set continuous sequence mode */
433 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
434 AMS_CONF1_SEQ_CONTINUOUS);
438 /* put sysmon in a soft reset to change the sequence */
439 ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
440 AMS_CONF1_SEQ_DEFAULT);
442 /* configure basic channels */
443 scan_mask = FIELD_GET(AMS_PL_SEQ_MASK, scan_mask);
445 regval = FIELD_GET(AMS_REG_SEQ0_MASK, scan_mask);
446 writel(regval, ams->pl_base + AMS_REG_SEQ_CH0);
448 regval = FIELD_GET(AMS_REG_SEQ1_MASK, scan_mask);
449 writel(regval, ams->pl_base + AMS_REG_SEQ_CH1);
451 regval = FIELD_GET(AMS_REG_SEQ2_MASK, scan_mask);
452 writel(regval, ams->pl_base + AMS_REG_SEQ_CH2);
454 /* set continuous sequence mode */
455 ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
456 AMS_CONF1_SEQ_CONTINUOUS);
460 static int ams_init_device(struct ams *ams)
462 u32 expect = AMS_PS_CSTS_PS_READY;
468 writel(AMS_PS_RESET_VALUE, ams->ps_base + AMS_VP_VN);
470 ret = readl_poll_timeout(ams->base + AMS_PS_CSTS, reg, (reg & expect),
471 AMS_INIT_POLL_TIME_US, AMS_INIT_TIMEOUT_US);
475 /* put sysmon in a default state */
476 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
477 AMS_CONF1_SEQ_DEFAULT);
481 value = readl(ams->base + AMS_PL_CSTS);
485 writel(AMS_PL_RESET_VALUE, ams->pl_base + AMS_VP_VN);
487 /* put sysmon in a default state */
488 ams_pl_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
489 AMS_CONF1_SEQ_DEFAULT);
492 ams_disable_all_alarms(ams);
494 /* Disable interrupt */
495 ams_update_intrmask(ams, AMS_ALARM_MASK, AMS_ALARM_MASK);
497 /* Clear any pending interrupt */
498 writel(AMS_ISR0_ALARM_MASK, ams->base + AMS_ISR_0);
499 writel(AMS_ISR1_ALARM_MASK, ams->base + AMS_ISR_1);
504 static int ams_enable_single_channel(struct ams *ams, unsigned int offset)
510 channel_num = AMS_VCC_PSPLL0_CH;
513 channel_num = AMS_VCC_PSPLL3_CH;
516 channel_num = AMS_VCCINT_CH;
519 channel_num = AMS_VCCBRAM_CH;
522 channel_num = AMS_VCCAUX_CH;
525 channel_num = AMS_PSDDRPLL_CH;
528 channel_num = AMS_PSINTFPDDR_CH;
534 /* put sysmon in a soft reset to change the sequence */
535 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
536 AMS_CONF1_SEQ_DEFAULT);
538 /* write the channel number */
539 ams_ps_update_reg(ams, AMS_REG_CONFIG0, AMS_CONF0_CHANNEL_NUM_MASK,
542 /* set single channel, sequencer off mode */
543 ams_ps_update_reg(ams, AMS_REG_CONFIG1, AMS_CONF1_SEQ_MASK,
544 AMS_CONF1_SEQ_SINGLE_CHANNEL);
549 static int ams_read_vcc_reg(struct ams *ams, unsigned int offset, u32 *data)
551 u32 expect = AMS_ISR1_EOC_MASK;
555 ret = ams_enable_single_channel(ams, offset);
559 /* clear end-of-conversion flag, wait for next conversion to complete */
560 writel(expect, ams->base + AMS_ISR_1);
561 ret = readl_poll_timeout(ams->base + AMS_ISR_1, reg, (reg & expect),
562 AMS_INIT_POLL_TIME_US, AMS_INIT_TIMEOUT_US);
566 *data = readl(ams->base + offset);
571 static int ams_get_ps_scale(int address)
583 val = AMS_SUPPLY_SCALE_3VOLT_mV;
589 val = AMS_SUPPLY_SCALE_6VOLT_mV;
592 val = AMS_SUPPLY_SCALE_1VOLT_mV;
599 static int ams_get_pl_scale(struct ams *ams, int address)
613 val = AMS_SUPPLY_SCALE_3VOLT_mV;
616 regval = readl(ams->pl_base + AMS_REG_CONFIG4);
617 if (FIELD_GET(AMS_VUSER0_MASK, regval))
618 val = AMS_SUPPLY_SCALE_6VOLT_mV;
620 val = AMS_SUPPLY_SCALE_3VOLT_mV;
623 regval = readl(ams->pl_base + AMS_REG_CONFIG4);
624 if (FIELD_GET(AMS_VUSER1_MASK, regval))
625 val = AMS_SUPPLY_SCALE_6VOLT_mV;
627 val = AMS_SUPPLY_SCALE_3VOLT_mV;
630 regval = readl(ams->pl_base + AMS_REG_CONFIG4);
631 if (FIELD_GET(AMS_VUSER2_MASK, regval))
632 val = AMS_SUPPLY_SCALE_6VOLT_mV;
634 val = AMS_SUPPLY_SCALE_3VOLT_mV;
637 regval = readl(ams->pl_base + AMS_REG_CONFIG4);
638 if (FIELD_GET(AMS_VUSER3_MASK, regval))
639 val = AMS_SUPPLY_SCALE_6VOLT_mV;
641 val = AMS_SUPPLY_SCALE_3VOLT_mV;
644 case AMS_REG_VAUX(0) ... AMS_REG_VAUX(15):
645 val = AMS_SUPPLY_SCALE_1VOLT_mV;
648 val = AMS_SUPPLY_SCALE_1VOLT_mV;
655 static int ams_get_ctrl_scale(int address)
667 val = AMS_SUPPLY_SCALE_3VOLT_mV;
670 val = AMS_SUPPLY_SCALE_1VOLT_mV;
677 static int ams_read_raw(struct iio_dev *indio_dev,
678 struct iio_chan_spec const *chan,
679 int *val, int *val2, long mask)
681 struct ams *ams = iio_priv(indio_dev);
685 case IIO_CHAN_INFO_RAW:
686 mutex_lock(&ams->lock);
687 if (chan->scan_index >= AMS_CTRL_SEQ_BASE) {
688 ret = ams_read_vcc_reg(ams, chan->address, val);
691 ams_enable_channel_sequence(indio_dev);
692 } else if (chan->scan_index >= AMS_PS_SEQ_MAX)
693 *val = readl(ams->pl_base + chan->address);
695 *val = readl(ams->ps_base + chan->address);
699 mutex_unlock(&ams->lock);
701 case IIO_CHAN_INFO_SCALE:
702 switch (chan->type) {
704 if (chan->scan_index < AMS_PS_SEQ_MAX)
705 *val = ams_get_ps_scale(chan->address);
706 else if (chan->scan_index >= AMS_PS_SEQ_MAX &&
707 chan->scan_index < AMS_CTRL_SEQ_BASE)
708 *val = ams_get_pl_scale(ams, chan->address);
710 *val = ams_get_ctrl_scale(chan->address);
712 *val2 = AMS_SUPPLY_SCALE_DIV_BIT;
713 return IIO_VAL_FRACTIONAL_LOG2;
715 *val = AMS_TEMP_SCALE;
716 *val2 = AMS_TEMP_SCALE_DIV_BIT;
717 return IIO_VAL_FRACTIONAL_LOG2;
721 case IIO_CHAN_INFO_OFFSET:
722 /* Only the temperature channel has an offset */
723 *val = AMS_TEMP_OFFSET;
730 static int ams_get_alarm_offset(int scan_index, enum iio_event_direction dir)
734 if (scan_index >= AMS_PS_SEQ_MAX)
735 scan_index -= AMS_PS_SEQ_MAX;
737 if (dir == IIO_EV_DIR_FALLING) {
738 if (scan_index < AMS_SEQ_SUPPLY7)
739 offset = AMS_ALARM_THRESHOLD_OFF_10;
741 offset = AMS_ALARM_THRESHOLD_OFF_20;
746 switch (scan_index) {
748 return AMS_ALARM_TEMP + offset;
749 case AMS_SEQ_SUPPLY1:
750 return AMS_ALARM_SUPPLY1 + offset;
751 case AMS_SEQ_SUPPLY2:
752 return AMS_ALARM_SUPPLY2 + offset;
753 case AMS_SEQ_SUPPLY3:
754 return AMS_ALARM_SUPPLY3 + offset;
755 case AMS_SEQ_SUPPLY4:
756 return AMS_ALARM_SUPPLY4 + offset;
757 case AMS_SEQ_SUPPLY5:
758 return AMS_ALARM_SUPPLY5 + offset;
759 case AMS_SEQ_SUPPLY6:
760 return AMS_ALARM_SUPPLY6 + offset;
761 case AMS_SEQ_SUPPLY7:
762 return AMS_ALARM_SUPPLY7 + offset;
763 case AMS_SEQ_SUPPLY8:
764 return AMS_ALARM_SUPPLY8 + offset;
765 case AMS_SEQ_SUPPLY9:
766 return AMS_ALARM_SUPPLY9 + offset;
767 case AMS_SEQ_SUPPLY10:
768 return AMS_ALARM_SUPPLY10 + offset;
770 return AMS_ALARM_VCCAMS + offset;
771 case AMS_SEQ_TEMP_REMOTE:
772 return AMS_ALARM_TEMP_REMOTE + offset;
778 static const struct iio_chan_spec *ams_event_to_channel(struct iio_dev *dev,
781 int scan_index = 0, i;
783 if (event >= AMS_PL_ALARM_START) {
784 event -= AMS_PL_ALARM_START;
785 scan_index = AMS_PS_SEQ_MAX;
789 case AMS_ALARM_BIT_TEMP:
790 scan_index += AMS_SEQ_TEMP;
792 case AMS_ALARM_BIT_SUPPLY1:
793 scan_index += AMS_SEQ_SUPPLY1;
795 case AMS_ALARM_BIT_SUPPLY2:
796 scan_index += AMS_SEQ_SUPPLY2;
798 case AMS_ALARM_BIT_SUPPLY3:
799 scan_index += AMS_SEQ_SUPPLY3;
801 case AMS_ALARM_BIT_SUPPLY4:
802 scan_index += AMS_SEQ_SUPPLY4;
804 case AMS_ALARM_BIT_SUPPLY5:
805 scan_index += AMS_SEQ_SUPPLY5;
807 case AMS_ALARM_BIT_SUPPLY6:
808 scan_index += AMS_SEQ_SUPPLY6;
810 case AMS_ALARM_BIT_SUPPLY7:
811 scan_index += AMS_SEQ_SUPPLY7;
813 case AMS_ALARM_BIT_SUPPLY8:
814 scan_index += AMS_SEQ_SUPPLY8;
816 case AMS_ALARM_BIT_SUPPLY9:
817 scan_index += AMS_SEQ_SUPPLY9;
819 case AMS_ALARM_BIT_SUPPLY10:
820 scan_index += AMS_SEQ_SUPPLY10;
822 case AMS_ALARM_BIT_VCCAMS:
823 scan_index += AMS_SEQ_VCCAMS;
825 case AMS_ALARM_BIT_TEMP_REMOTE:
826 scan_index += AMS_SEQ_TEMP_REMOTE;
832 for (i = 0; i < dev->num_channels; i++)
833 if (dev->channels[i].scan_index == scan_index)
836 return &dev->channels[i];
839 static int ams_get_alarm_mask(int scan_index)
843 if (scan_index >= AMS_PS_SEQ_MAX) {
844 bit = AMS_PL_ALARM_START;
845 scan_index -= AMS_PS_SEQ_MAX;
848 switch (scan_index) {
850 return BIT(AMS_ALARM_BIT_TEMP + bit);
851 case AMS_SEQ_SUPPLY1:
852 return BIT(AMS_ALARM_BIT_SUPPLY1 + bit);
853 case AMS_SEQ_SUPPLY2:
854 return BIT(AMS_ALARM_BIT_SUPPLY2 + bit);
855 case AMS_SEQ_SUPPLY3:
856 return BIT(AMS_ALARM_BIT_SUPPLY3 + bit);
857 case AMS_SEQ_SUPPLY4:
858 return BIT(AMS_ALARM_BIT_SUPPLY4 + bit);
859 case AMS_SEQ_SUPPLY5:
860 return BIT(AMS_ALARM_BIT_SUPPLY5 + bit);
861 case AMS_SEQ_SUPPLY6:
862 return BIT(AMS_ALARM_BIT_SUPPLY6 + bit);
863 case AMS_SEQ_SUPPLY7:
864 return BIT(AMS_ALARM_BIT_SUPPLY7 + bit);
865 case AMS_SEQ_SUPPLY8:
866 return BIT(AMS_ALARM_BIT_SUPPLY8 + bit);
867 case AMS_SEQ_SUPPLY9:
868 return BIT(AMS_ALARM_BIT_SUPPLY9 + bit);
869 case AMS_SEQ_SUPPLY10:
870 return BIT(AMS_ALARM_BIT_SUPPLY10 + bit);
872 return BIT(AMS_ALARM_BIT_VCCAMS + bit);
873 case AMS_SEQ_TEMP_REMOTE:
874 return BIT(AMS_ALARM_BIT_TEMP_REMOTE + bit);
880 static int ams_read_event_config(struct iio_dev *indio_dev,
881 const struct iio_chan_spec *chan,
882 enum iio_event_type type,
883 enum iio_event_direction dir)
885 struct ams *ams = iio_priv(indio_dev);
887 return !!(ams->alarm_mask & ams_get_alarm_mask(chan->scan_index));
890 static int ams_write_event_config(struct iio_dev *indio_dev,
891 const struct iio_chan_spec *chan,
892 enum iio_event_type type,
893 enum iio_event_direction dir,
896 struct ams *ams = iio_priv(indio_dev);
899 alarm = ams_get_alarm_mask(chan->scan_index);
901 mutex_lock(&ams->lock);
904 ams->alarm_mask |= alarm;
906 ams->alarm_mask &= ~alarm;
908 ams_update_alarm(ams, ams->alarm_mask);
910 mutex_unlock(&ams->lock);
915 static int ams_read_event_value(struct iio_dev *indio_dev,
916 const struct iio_chan_spec *chan,
917 enum iio_event_type type,
918 enum iio_event_direction dir,
919 enum iio_event_info info, int *val, int *val2)
921 struct ams *ams = iio_priv(indio_dev);
922 unsigned int offset = ams_get_alarm_offset(chan->scan_index, dir);
924 mutex_lock(&ams->lock);
926 if (chan->scan_index >= AMS_PS_SEQ_MAX)
927 *val = readl(ams->pl_base + offset);
929 *val = readl(ams->ps_base + offset);
931 mutex_unlock(&ams->lock);
936 static int ams_write_event_value(struct iio_dev *indio_dev,
937 const struct iio_chan_spec *chan,
938 enum iio_event_type type,
939 enum iio_event_direction dir,
940 enum iio_event_info info, int val, int val2)
942 struct ams *ams = iio_priv(indio_dev);
945 mutex_lock(&ams->lock);
947 /* Set temperature channel threshold to direct threshold */
948 if (chan->type == IIO_TEMP) {
949 offset = ams_get_alarm_offset(chan->scan_index, IIO_EV_DIR_FALLING);
951 if (chan->scan_index >= AMS_PS_SEQ_MAX)
952 ams_pl_update_reg(ams, offset,
953 AMS_ALARM_THR_DIRECT_MASK,
954 AMS_ALARM_THR_DIRECT_MASK);
956 ams_ps_update_reg(ams, offset,
957 AMS_ALARM_THR_DIRECT_MASK,
958 AMS_ALARM_THR_DIRECT_MASK);
961 offset = ams_get_alarm_offset(chan->scan_index, dir);
962 if (chan->scan_index >= AMS_PS_SEQ_MAX)
963 writel(val, ams->pl_base + offset);
965 writel(val, ams->ps_base + offset);
967 mutex_unlock(&ams->lock);
972 static void ams_handle_event(struct iio_dev *indio_dev, u32 event)
974 const struct iio_chan_spec *chan;
976 chan = ams_event_to_channel(indio_dev, event);
978 if (chan->type == IIO_TEMP) {
980 * The temperature channel only supports over-temperature
983 iio_push_event(indio_dev,
984 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
987 iio_get_time_ns(indio_dev));
990 * For other channels we don't know whether it is a upper or
991 * lower threshold event. Userspace will have to check the
992 * channel value if it wants to know.
994 iio_push_event(indio_dev,
995 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
998 iio_get_time_ns(indio_dev));
1002 static void ams_handle_events(struct iio_dev *indio_dev, unsigned long events)
1006 for_each_set_bit(bit, &events, AMS_NO_OF_ALARMS)
1007 ams_handle_event(indio_dev, bit);
1011 * ams_unmask_worker - ams alarm interrupt unmask worker
1012 * @work: work to be done
1014 * The ZynqMP threshold interrupts are level sensitive. Since we can't make the
1015 * threshold condition go way from within the interrupt handler, this means as
1016 * soon as a threshold condition is present we would enter the interrupt handler
1017 * again and again. To work around this we mask all active threshold interrupts
1018 * in the interrupt handler and start a timer. In this timer we poll the
1019 * interrupt status and only if the interrupt is inactive we unmask it again.
1021 static void ams_unmask_worker(struct work_struct *work)
1023 struct ams *ams = container_of(work, struct ams, ams_unmask_work.work);
1024 unsigned int status, unmask;
1026 spin_lock_irq(&ams->intr_lock);
1028 status = readl(ams->base + AMS_ISR_0);
1030 /* Clear those bits which are not active anymore */
1031 unmask = (ams->current_masked_alarm ^ status) & ams->current_masked_alarm;
1033 /* Clear status of disabled alarm */
1034 unmask |= ams->intr_mask;
1036 ams->current_masked_alarm &= status;
1038 /* Also clear those which are masked out anyway */
1039 ams->current_masked_alarm &= ~ams->intr_mask;
1041 /* Clear the interrupts before we unmask them */
1042 writel(unmask, ams->base + AMS_ISR_0);
1044 ams_update_intrmask(ams, ~AMS_ALARM_MASK, ~AMS_ALARM_MASK);
1046 spin_unlock_irq(&ams->intr_lock);
1048 /* If still pending some alarm re-trigger the timer */
1049 if (ams->current_masked_alarm)
1050 schedule_delayed_work(&ams->ams_unmask_work,
1051 msecs_to_jiffies(AMS_UNMASK_TIMEOUT_MS));
1054 static irqreturn_t ams_irq(int irq, void *data)
1056 struct iio_dev *indio_dev = data;
1057 struct ams *ams = iio_priv(indio_dev);
1060 spin_lock(&ams->intr_lock);
1062 isr0 = readl(ams->base + AMS_ISR_0);
1064 /* Only process alarms that are not masked */
1065 isr0 &= ~((ams->intr_mask & AMS_ISR0_ALARM_MASK) | ams->current_masked_alarm);
1067 spin_unlock(&ams->intr_lock);
1071 /* Clear interrupt */
1072 writel(isr0, ams->base + AMS_ISR_0);
1074 /* Mask the alarm interrupts until cleared */
1075 ams->current_masked_alarm |= isr0;
1076 ams_update_intrmask(ams, ~AMS_ALARM_MASK, ~AMS_ALARM_MASK);
1078 ams_handle_events(indio_dev, isr0);
1080 schedule_delayed_work(&ams->ams_unmask_work,
1081 msecs_to_jiffies(AMS_UNMASK_TIMEOUT_MS));
1083 spin_unlock(&ams->intr_lock);
1088 static const struct iio_event_spec ams_temp_events[] = {
1090 .type = IIO_EV_TYPE_THRESH,
1091 .dir = IIO_EV_DIR_RISING,
1092 .mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
1096 static const struct iio_event_spec ams_voltage_events[] = {
1098 .type = IIO_EV_TYPE_THRESH,
1099 .dir = IIO_EV_DIR_RISING,
1100 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1103 .type = IIO_EV_TYPE_THRESH,
1104 .dir = IIO_EV_DIR_FALLING,
1105 .mask_separate = BIT(IIO_EV_INFO_VALUE),
1108 .type = IIO_EV_TYPE_THRESH,
1109 .dir = IIO_EV_DIR_EITHER,
1110 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
1114 static const struct iio_chan_spec ams_ps_channels[] = {
1115 AMS_PS_CHAN_TEMP(AMS_SEQ_TEMP, AMS_TEMP),
1116 AMS_PS_CHAN_TEMP(AMS_SEQ_TEMP_REMOTE, AMS_TEMP_REMOTE),
1117 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY1, AMS_SUPPLY1),
1118 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY2, AMS_SUPPLY2),
1119 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY3, AMS_SUPPLY3),
1120 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY4, AMS_SUPPLY4),
1121 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY5, AMS_SUPPLY5),
1122 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY6, AMS_SUPPLY6),
1123 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY7, AMS_SUPPLY7),
1124 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY8, AMS_SUPPLY8),
1125 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY9, AMS_SUPPLY9),
1126 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_SUPPLY10, AMS_SUPPLY10),
1127 AMS_PS_CHAN_VOLTAGE(AMS_SEQ_VCCAMS, AMS_VCCAMS),
1130 static const struct iio_chan_spec ams_pl_channels[] = {
1131 AMS_PL_CHAN_TEMP(AMS_SEQ_TEMP, AMS_TEMP),
1132 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY1, AMS_SUPPLY1, true),
1133 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY2, AMS_SUPPLY2, true),
1134 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VREFP, AMS_VREFP, false),
1135 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VREFN, AMS_VREFN, false),
1136 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY3, AMS_SUPPLY3, true),
1137 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY4, AMS_SUPPLY4, true),
1138 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY5, AMS_SUPPLY5, true),
1139 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY6, AMS_SUPPLY6, true),
1140 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VCCAMS, AMS_VCCAMS, true),
1141 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_VP_VN, AMS_VP_VN, false),
1142 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY7, AMS_SUPPLY7, true),
1143 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY8, AMS_SUPPLY8, true),
1144 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY9, AMS_SUPPLY9, true),
1145 AMS_PL_CHAN_VOLTAGE(AMS_SEQ_SUPPLY10, AMS_SUPPLY10, true),
1146 AMS_PL_AUX_CHAN_VOLTAGE(0),
1147 AMS_PL_AUX_CHAN_VOLTAGE(1),
1148 AMS_PL_AUX_CHAN_VOLTAGE(2),
1149 AMS_PL_AUX_CHAN_VOLTAGE(3),
1150 AMS_PL_AUX_CHAN_VOLTAGE(4),
1151 AMS_PL_AUX_CHAN_VOLTAGE(5),
1152 AMS_PL_AUX_CHAN_VOLTAGE(6),
1153 AMS_PL_AUX_CHAN_VOLTAGE(7),
1154 AMS_PL_AUX_CHAN_VOLTAGE(8),
1155 AMS_PL_AUX_CHAN_VOLTAGE(9),
1156 AMS_PL_AUX_CHAN_VOLTAGE(10),
1157 AMS_PL_AUX_CHAN_VOLTAGE(11),
1158 AMS_PL_AUX_CHAN_VOLTAGE(12),
1159 AMS_PL_AUX_CHAN_VOLTAGE(13),
1160 AMS_PL_AUX_CHAN_VOLTAGE(14),
1161 AMS_PL_AUX_CHAN_VOLTAGE(15),
1164 static const struct iio_chan_spec ams_ctrl_channels[] = {
1165 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSPLL, AMS_VCC_PSPLL0),
1166 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSBATT, AMS_VCC_PSPLL3),
1167 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCINT, AMS_VCCINT),
1168 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCBRAM, AMS_VCCBRAM),
1169 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCCAUX, AMS_VCCAUX),
1170 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_PSDDRPLL, AMS_PSDDRPLL),
1171 AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_INTDDR, AMS_PSINTFPDDR),
1174 static int ams_get_ext_chan(struct fwnode_handle *chan_node,
1175 struct iio_chan_spec *channels, int num_channels)
1177 struct iio_chan_spec *chan;
1178 struct fwnode_handle *child;
1179 unsigned int reg, ext_chan;
1182 fwnode_for_each_child_node(chan_node, child) {
1183 ret = fwnode_property_read_u32(child, "reg", ®);
1184 if (ret || reg > AMS_PL_MAX_EXT_CHANNEL + 30)
1187 chan = &channels[num_channels];
1188 ext_chan = reg + AMS_PL_MAX_FIXED_CHANNEL - 30;
1189 memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));
1191 if (fwnode_property_read_bool(child, "xlnx,bipolar"))
1192 chan->scan_type.sign = 's';
1197 return num_channels;
1200 static void ams_iounmap_ps(void *data)
1202 struct ams *ams = data;
1204 iounmap(ams->ps_base);
1207 static void ams_iounmap_pl(void *data)
1209 struct ams *ams = data;
1211 iounmap(ams->pl_base);
1214 static int ams_init_module(struct iio_dev *indio_dev,
1215 struct fwnode_handle *fwnode,
1216 struct iio_chan_spec *channels)
1218 struct device *dev = indio_dev->dev.parent;
1219 struct ams *ams = iio_priv(indio_dev);
1220 int num_channels = 0;
1223 if (fwnode_property_match_string(fwnode, "compatible",
1224 "xlnx,zynqmp-ams-ps") == 0) {
1225 ams->ps_base = fwnode_iomap(fwnode, 0);
1228 ret = devm_add_action_or_reset(dev, ams_iounmap_ps, ams);
1232 /* add PS channels to iio device channels */
1233 memcpy(channels, ams_ps_channels, sizeof(ams_ps_channels));
1234 num_channels = ARRAY_SIZE(ams_ps_channels);
1235 } else if (fwnode_property_match_string(fwnode, "compatible",
1236 "xlnx,zynqmp-ams-pl") == 0) {
1237 ams->pl_base = fwnode_iomap(fwnode, 0);
1241 ret = devm_add_action_or_reset(dev, ams_iounmap_pl, ams);
1245 /* Copy only first 10 fix channels */
1246 memcpy(channels, ams_pl_channels, AMS_PL_MAX_FIXED_CHANNEL * sizeof(*channels));
1247 num_channels += AMS_PL_MAX_FIXED_CHANNEL;
1248 num_channels = ams_get_ext_chan(fwnode, channels,
1250 } else if (fwnode_property_match_string(fwnode, "compatible",
1251 "xlnx,zynqmp-ams") == 0) {
1252 /* add AMS channels to iio device channels */
1253 memcpy(channels, ams_ctrl_channels, sizeof(ams_ctrl_channels));
1254 num_channels += ARRAY_SIZE(ams_ctrl_channels);
1259 return num_channels;
1262 static int ams_parse_firmware(struct iio_dev *indio_dev)
1264 struct ams *ams = iio_priv(indio_dev);
1265 struct iio_chan_spec *ams_channels, *dev_channels;
1266 struct device *dev = indio_dev->dev.parent;
1267 struct fwnode_handle *child = NULL;
1268 struct fwnode_handle *fwnode = dev_fwnode(dev);
1269 size_t ams_size, dev_size;
1270 int ret, ch_cnt = 0, i, rising_off, falling_off;
1271 unsigned int num_channels = 0;
1273 ams_size = ARRAY_SIZE(ams_ps_channels) + ARRAY_SIZE(ams_pl_channels) +
1274 ARRAY_SIZE(ams_ctrl_channels);
1276 /* Initialize buffer for channel specification */
1277 ams_channels = devm_kcalloc(dev, ams_size, sizeof(*ams_channels), GFP_KERNEL);
1281 if (fwnode_device_is_available(fwnode)) {
1282 ret = ams_init_module(indio_dev, fwnode, ams_channels);
1286 num_channels += ret;
1289 fwnode_for_each_child_node(fwnode, child) {
1290 if (fwnode_device_is_available(child)) {
1291 ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
1293 fwnode_handle_put(child);
1297 num_channels += ret;
1301 for (i = 0; i < num_channels; i++) {
1302 ams_channels[i].channel = ch_cnt++;
1304 if (ams_channels[i].scan_index < AMS_CTRL_SEQ_BASE) {
1305 /* set threshold to max and min for each channel */
1307 ams_get_alarm_offset(ams_channels[i].scan_index,
1308 IIO_EV_DIR_FALLING);
1310 ams_get_alarm_offset(ams_channels[i].scan_index,
1312 if (ams_channels[i].scan_index >= AMS_PS_SEQ_MAX) {
1313 writel(AMS_ALARM_THR_MIN,
1314 ams->pl_base + falling_off);
1315 writel(AMS_ALARM_THR_MAX,
1316 ams->pl_base + rising_off);
1318 writel(AMS_ALARM_THR_MIN,
1319 ams->ps_base + falling_off);
1320 writel(AMS_ALARM_THR_MAX,
1321 ams->ps_base + rising_off);
1326 dev_size = array_size(sizeof(*dev_channels), num_channels);
1327 if (dev_size == SIZE_MAX)
1330 dev_channels = devm_krealloc(dev, ams_channels, dev_size, GFP_KERNEL);
1334 indio_dev->channels = dev_channels;
1335 indio_dev->num_channels = num_channels;
1340 static const struct iio_info iio_ams_info = {
1341 .read_raw = &ams_read_raw,
1342 .read_event_config = &ams_read_event_config,
1343 .write_event_config = &ams_write_event_config,
1344 .read_event_value = &ams_read_event_value,
1345 .write_event_value = &ams_write_event_value,
1348 static const struct of_device_id ams_of_match_table[] = {
1349 { .compatible = "xlnx,zynqmp-ams" },
1352 MODULE_DEVICE_TABLE(of, ams_of_match_table);
1354 static void ams_clk_disable_unprepare(void *data)
1356 clk_disable_unprepare(data);
1359 static int ams_probe(struct platform_device *pdev)
1361 struct iio_dev *indio_dev;
1366 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ams));
1370 ams = iio_priv(indio_dev);
1371 mutex_init(&ams->lock);
1372 spin_lock_init(&ams->intr_lock);
1374 indio_dev->name = "xilinx-ams";
1376 indio_dev->info = &iio_ams_info;
1377 indio_dev->modes = INDIO_DIRECT_MODE;
1379 ams->base = devm_platform_ioremap_resource(pdev, 0);
1380 if (IS_ERR(ams->base))
1381 return PTR_ERR(ams->base);
1383 ams->clk = devm_clk_get(&pdev->dev, NULL);
1384 if (IS_ERR(ams->clk))
1385 return PTR_ERR(ams->clk);
1387 ret = clk_prepare_enable(ams->clk);
1391 ret = devm_add_action_or_reset(&pdev->dev, ams_clk_disable_unprepare, ams->clk);
1395 ret = devm_delayed_work_autocancel(&pdev->dev, &ams->ams_unmask_work,
1400 ret = ams_parse_firmware(indio_dev);
1402 return dev_err_probe(&pdev->dev, ret, "failure in parsing DT\n");
1404 ret = ams_init_device(ams);
1406 return dev_err_probe(&pdev->dev, ret, "failed to initialize AMS\n");
1408 ams_enable_channel_sequence(indio_dev);
1410 irq = platform_get_irq(pdev, 0);
1414 ret = devm_request_irq(&pdev->dev, irq, &ams_irq, 0, "ams-irq",
1417 return dev_err_probe(&pdev->dev, ret, "failed to register interrupt\n");
1419 platform_set_drvdata(pdev, indio_dev);
1421 return devm_iio_device_register(&pdev->dev, indio_dev);
1424 static int __maybe_unused ams_suspend(struct device *dev)
1426 struct ams *ams = iio_priv(dev_get_drvdata(dev));
1428 clk_disable_unprepare(ams->clk);
1433 static int __maybe_unused ams_resume(struct device *dev)
1435 struct ams *ams = iio_priv(dev_get_drvdata(dev));
1437 return clk_prepare_enable(ams->clk);
1440 static SIMPLE_DEV_PM_OPS(ams_pm_ops, ams_suspend, ams_resume);
1442 static struct platform_driver ams_driver = {
1445 .name = "xilinx-ams",
1447 .of_match_table = ams_of_match_table,
1450 module_platform_driver(ams_driver);
1452 MODULE_LICENSE("GPL v2");
1453 MODULE_AUTHOR("Xilinx, Inc.");