summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/iio/adc/xilinx-xadc.h
blob: 54adc5087210bb173afd62bfa36712e4a014eafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Xilinx XADC driver
 *
 * Copyright 2013 Analog Devices Inc.
 *  Author: Lars-Peter Clauen <lars@metafoo.de>
 *
 * Licensed under the GPL-2.
 */

#ifndef __IIO_XILINX_XADC__
#define __IIO_XILINX_XADC__

#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>

struct iio_dev;
struct clk;
struct xadc_ops;
struct platform_device;

void xadc_handle_events(struct iio_dev *indio_dev, unsigned long events);

int xadc_read_event_config(struct iio_dev *indio_dev,
	const struct iio_chan_spec *chan, enum iio_event_type type,
	enum iio_event_direction dir);
int xadc_write_event_config(struct iio_dev *indio_dev,
	const struct iio_chan_spec *chan, enum iio_event_type type,
	enum iio_event_direction dir, int state);
int xadc_read_event_value(struct iio_dev *indio_dev,
	const struct iio_chan_spec *chan, enum iio_event_type type,
	enum iio_event_direction dir, enum iio_event_info info,
	int *val, int *val2);
int xadc_write_event_value(struct iio_dev *indio_dev,
	const struct iio_chan_spec *chan, enum iio_event_type type,
	enum iio_event_direction dir, enum iio_event_info info,
	int val, int val2);

enum xadc_external_mux_mode {
	XADC_EXTERNAL_MUX_NONE,
	XADC_EXTERNAL_MUX_SINGLE,
	XADC_EXTERNAL_MUX_DUAL,
};

struct xadc {
	void __iomem *base;
	struct clk *clk;

	const struct xadc_ops *ops;

	uint16_t threshold[16];
	uint16_t temp_hysteresis;
	unsigned int alarm_mask;

	uint16_t *data;

	struct iio_trigger *trigger;
	struct iio_trigger *convst_trigger;
	struct iio_trigger *samplerate_trigger;

	enum xadc_external_mux_mode external_mux_mode;

	unsigned int zynq_alarm;
	unsigned int zynq_masked_alarm;
	unsigned int zynq_intmask;
	struct delayed_work zynq_unmask_work;

	struct mutex mutex;
	spinlock_t lock;

	struct completion completion;
};

struct xadc_ops {
	int (*read)(struct xadc *, unsigned int, uint16_t *);
	int (*write)(struct xadc *, unsigned int, uint16_t);
	int (*setup)(struct platform_device *pdev, struct iio_dev *indio_dev,
			int irq);
	void (*update_alarm)(struct xadc *, unsigned int);
	unsigned long (*get_dclk_rate)(struct xadc *);
	irqreturn_t (*interrupt_handler)(int, void *);
	irqreturn_t (*threaded_interrupt_handler)(int, void *);

	unsigned int flags;
};

static inline int _xadc_read_adc_reg(struct xadc *xadc, unsigned int reg,
	uint16_t *val)
{
	lockdep_assert_held(&xadc->mutex);
	return xadc->ops->read(xadc, reg, val);
}

static inline int _xadc_write_adc_reg(struct xadc *xadc, unsigned int reg,
	uint16_t val)
{
	lockdep_assert_held(&xadc->mutex);
	return xadc->ops->write(xadc, reg, val);
}

static inline int xadc_read_adc_reg(struct xadc *xadc, unsigned int reg,
	uint16_t *val)
{
	int ret;

	mutex_lock(&xadc->mutex);
	ret = _xadc_read_adc_reg(xadc, reg, val);
	mutex_unlock(&xadc->mutex);
	return ret;
}

static inline int xadc_write_adc_reg(struct xadc *xadc, unsigned int reg,
	uint16_t val)
{
	int ret;

	mutex_lock(&xadc->mutex);
	ret = _xadc_write_adc_reg(xadc, reg, val);
	mutex_unlock(&xadc->mutex);
	return ret;
}

/* XADC hardmacro register definitions */
#define XADC_REG_TEMP		0x00
#define XADC_REG_VCCINT		0x01
#define XADC_REG_VCCAUX		0x02
#define XADC_REG_VPVN		0x03
#define XADC_REG_VREFP		0x04
#define XADC_REG_VREFN		0x05
#define XADC_REG_VCCBRAM	0x06

#define XADC_REG_VCCPINT	0x0d
#define XADC_REG_VCCPAUX	0x0e
#define XADC_REG_VCCO_DDR	0x0f
#define XADC_REG_VAUX(x)	(0x10 + (x))

#define XADC_REG_MAX_TEMP	0x20
#define XADC_REG_MAX_VCCINT	0x21
#define XADC_REG_MAX_VCCAUX	0x22
#define XADC_REG_MAX_VCCBRAM	0x23
#define XADC_REG_MIN_TEMP	0x24
#define XADC_REG_MIN_VCCINT	0x25
#define XADC_REG_MIN_VCCAUX	0x26
#define XADC_REG_MIN_VCCBRAM	0x27
#define XADC_REG_MAX_VCCPINT	0x28
#define XADC_REG_MAX_VCCPAUX	0x29
#define XADC_REG_MAX_VCCO_DDR	0x2a
#define XADC_REG_MIN_VCCPINT	0x2c
#define XADC_REG_MIN_VCCPAUX	0x2d
#define XADC_REG_MIN_VCCO_DDR	0x2e

#define XADC_REG_CONF0		0x40
#define XADC_REG_CONF1		0x41
#define XADC_REG_CONF2		0x42
#define XADC_REG_SEQ(x)		(0x48 + (x))
#define XADC_REG_INPUT_MODE(x)	(0x4c + (x))
#define XADC_REG_THRESHOLD(x)	(0x50 + (x))

#define XADC_REG_FLAG		0x3f

#define XADC_CONF0_EC			BIT(9)
#define XADC_CONF0_ACQ			BIT(8)
#define XADC_CONF0_MUX			BIT(11)
#define XADC_CONF0_CHAN(x)		(x)

#define XADC_CONF1_SEQ_MASK		(0xf << 12)
#define XADC_CONF1_SEQ_DEFAULT		(0 << 12)
#define XADC_CONF1_SEQ_SINGLE_PASS	(1 << 12)
#define XADC_CONF1_SEQ_CONTINUOUS	(2 << 12)
#define XADC_CONF1_SEQ_SINGLE_CHANNEL	(3 << 12)
#define XADC_CONF1_SEQ_SIMULTANEOUS	(4 << 12)
#define XADC_CONF1_SEQ_INDEPENDENT	(8 << 12)
#define XADC_CONF1_ALARM_MASK		0x0f0f

#define XADC_CONF2_DIV_MASK	0xff00
#define XADC_CONF2_DIV_OFFSET	8

#define XADC_CONF2_PD_MASK	(0x3 << 4)
#define XADC_CONF2_PD_NONE	(0x0 << 4)
#define XADC_CONF2_PD_ADC_B	(0x2 << 4)
#define XADC_CONF2_PD_BOTH	(0x3 << 4)

#define XADC_ALARM_TEMP_MASK		BIT(0)
#define XADC_ALARM_VCCINT_MASK		BIT(1)
#define XADC_ALARM_VCCAUX_MASK		BIT(2)
#define XADC_ALARM_OT_MASK		BIT(3)
#define XADC_ALARM_VCCBRAM_MASK		BIT(4)
#define XADC_ALARM_VCCPINT_MASK		BIT(5)
#define XADC_ALARM_VCCPAUX_MASK		BIT(6)
#define XADC_ALARM_VCCODDR_MASK		BIT(7)

#define XADC_THRESHOLD_TEMP_MAX		0x0
#define XADC_THRESHOLD_VCCINT_MAX	0x1
#define XADC_THRESHOLD_VCCAUX_MAX	0x2
#define XADC_THRESHOLD_OT_MAX		0x3
#define XADC_THRESHOLD_TEMP_MIN		0x4
#define XADC_THRESHOLD_VCCINT_MIN	0x5
#define XADC_THRESHOLD_VCCAUX_MIN	0x6
#define XADC_THRESHOLD_OT_MIN		0x7
#define XADC_THRESHOLD_VCCBRAM_MAX	0x8
#define XADC_THRESHOLD_VCCPINT_MAX	0x9
#define XADC_THRESHOLD_VCCPAUX_MAX	0xa
#define XADC_THRESHOLD_VCCODDR_MAX	0xb
#define XADC_THRESHOLD_VCCBRAM_MIN	0xc
#define XADC_THRESHOLD_VCCPINT_MIN	0xd
#define XADC_THRESHOLD_VCCPAUX_MIN	0xe
#define XADC_THRESHOLD_VCCODDR_MIN	0xf

#endif