1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * AD7091RX Analog to Digital converter driver
4 *
5 * Copyright 2014-2019 Analog Devices Inc.
6 */
7
8#ifndef __DRIVERS_IIO_ADC_AD7091R_BASE_H__
9#define __DRIVERS_IIO_ADC_AD7091R_BASE_H__
10
11#include <linux/regmap.h>
12
13#define AD7091R_REG_RESULT  0
14#define AD7091R_REG_CHANNEL 1
15#define AD7091R_REG_CONF    2
16#define AD7091R_REG_ALERT   3
17#define AD7091R_REG_CH_LOW_LIMIT(ch) ((ch) * 3 + 4)
18#define AD7091R_REG_CH_HIGH_LIMIT(ch) ((ch) * 3 + 5)
19#define AD7091R_REG_CH_HYSTERESIS(ch) ((ch) * 3 + 6)
20
21/* AD7091R_REG_RESULT */
22#define AD7091R5_REG_RESULT_CH_ID(x)	    (((x) >> 13) & 0x3)
23#define AD7091R8_REG_RESULT_CH_ID(x)	    (((x) >> 13) & 0x7)
24#define AD7091R_REG_RESULT_CONV_RESULT(x)   ((x) & 0xfff)
25
26/* AD7091R_REG_CONF */
27#define AD7091R_REG_CONF_INT_VREF	BIT(0)
28#define AD7091R_REG_CONF_ALERT_EN	BIT(4)
29#define AD7091R_REG_CONF_AUTO		BIT(8)
30#define AD7091R_REG_CONF_CMD		BIT(10)
31
32#define AD7091R_REG_CONF_MODE_MASK  \
33	(AD7091R_REG_CONF_AUTO | AD7091R_REG_CONF_CMD)
34
35/* AD7091R_REG_CH_LIMIT */
36#define AD7091R_HIGH_LIMIT		0xFFF
37#define AD7091R_LOW_LIMIT		0x0
38
39#define AD7091R_CHANNEL(idx, bits, ev, num_ev) {			\
40	.type = IIO_VOLTAGE,						\
41	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
42	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
43	.indexed = 1,							\
44	.channel = idx,							\
45	.event_spec = ev,						\
46	.num_event_specs = num_ev,					\
47	.scan_type.storagebits = 16,					\
48	.scan_type.realbits = bits,					\
49}
50
51struct device;
52struct gpio_desc;
53
54enum ad7091r_mode {
55	AD7091R_MODE_SAMPLE,
56	AD7091R_MODE_COMMAND,
57	AD7091R_MODE_AUTOCYCLE,
58};
59
60struct ad7091r_state {
61	struct device *dev;
62	struct regmap *map;
63	struct gpio_desc *convst_gpio;
64	struct gpio_desc *reset_gpio;
65	struct regulator *vref;
66	const struct ad7091r_chip_info *chip_info;
67	enum ad7091r_mode mode;
68	struct mutex lock; /*lock to prevent concurent reads */
69	__be16 tx_buf __aligned(IIO_DMA_MINALIGN);
70	__be16 rx_buf;
71};
72
73struct ad7091r_chip_info {
74	const char *name;
75	unsigned int num_channels;
76	const struct iio_chan_spec *channels;
77	unsigned int vref_mV;
78	unsigned int (*reg_result_chan_id)(unsigned int val);
79	int (*set_mode)(struct ad7091r_state *st, enum ad7091r_mode mode);
80};
81
82struct ad7091r_init_info {
83	const struct ad7091r_chip_info *info_irq;
84	const struct ad7091r_chip_info *info_no_irq;
85	const struct regmap_config *regmap_config;
86	void (*init_adc_regmap)(struct ad7091r_state *st,
87				const struct regmap_config *regmap_conf);
88	int (*setup)(struct ad7091r_state *st);
89};
90
91extern const struct iio_event_spec ad7091r_events[3];
92
93int ad7091r_probe(struct device *dev, const struct ad7091r_init_info *init_info,
94		  int irq);
95
96bool ad7091r_volatile_reg(struct device *dev, unsigned int reg);
97bool ad7091r_writeable_reg(struct device *dev, unsigned int reg);
98
99#endif /* __DRIVERS_IIO_ADC_AD7091R_BASE_H__ */
100