xref: /linux/drivers/iio/dac/ad5592r-base.h (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * AD5592R / AD5593R Digital <-> Analog converters driver
4  *
5  * Copyright 2015-2016 Analog Devices Inc.
6  * Author: Paul Cercueil <paul.cercueil@analog.com>
7  */
8 
9 #ifndef __DRIVERS_IIO_DAC_AD5592R_BASE_H__
10 #define __DRIVERS_IIO_DAC_AD5592R_BASE_H__
11 
12 #include <linux/types.h>
13 #include <linux/cache.h>
14 #include <linux/mutex.h>
15 #include <linux/gpio/driver.h>
16 
17 struct device;
18 struct ad5592r_state;
19 
20 enum ad5592r_registers {
21 	AD5592R_REG_NOOP		= 0x0,
22 	AD5592R_REG_DAC_READBACK	= 0x1,
23 	AD5592R_REG_ADC_SEQ		= 0x2,
24 	AD5592R_REG_CTRL		= 0x3,
25 	AD5592R_REG_ADC_EN		= 0x4,
26 	AD5592R_REG_DAC_EN		= 0x5,
27 	AD5592R_REG_PULLDOWN		= 0x6,
28 	AD5592R_REG_LDAC		= 0x7,
29 	AD5592R_REG_GPIO_OUT_EN		= 0x8,
30 	AD5592R_REG_GPIO_SET		= 0x9,
31 	AD5592R_REG_GPIO_IN_EN		= 0xA,
32 	AD5592R_REG_PD			= 0xB,
33 	AD5592R_REG_OPEN_DRAIN		= 0xC,
34 	AD5592R_REG_TRISTATE		= 0xD,
35 	AD5592R_REG_RESET		= 0xF,
36 };
37 
38 #define AD5592R_REG_PD_EN_REF		BIT(9)
39 #define AD5592R_REG_CTRL_ADC_RANGE	BIT(5)
40 #define AD5592R_REG_CTRL_DAC_RANGE	BIT(4)
41 
42 struct ad5592r_rw_ops {
43 	int (*write_dac)(struct ad5592r_state *st, unsigned chan, u16 value);
44 	int (*read_adc)(struct ad5592r_state *st, unsigned chan, u16 *value);
45 	int (*reg_write)(struct ad5592r_state *st, u8 reg, u16 value);
46 	int (*reg_read)(struct ad5592r_state *st, u8 reg, u16 *value);
47 	int (*gpio_read)(struct ad5592r_state *st, u8 *value);
48 };
49 
50 struct ad5592r_state {
51 	struct device *dev;
52 	struct regulator *reg;
53 	struct gpio_chip gpiochip;
54 	struct mutex gpio_lock;	/* Protect cached gpio_out, gpio_val, etc. */
55 	struct mutex lock;
56 	unsigned int num_channels;
57 	const struct ad5592r_rw_ops *ops;
58 	int scale_avail[2][2];
59 	u16 cached_dac[8];
60 	u16 cached_gp_ctrl;
61 	u8 channel_modes[8];
62 	u8 channel_offstate[8];
63 	u8 gpio_map;
64 	u8 gpio_out;
65 	u8 gpio_in;
66 	u8 gpio_val;
67 
68 	__be16 spi_msg ____cacheline_aligned;
69 	__be16 spi_msg_nop;
70 };
71 
72 int ad5592r_probe(struct device *dev, const char *name,
73 		const struct ad5592r_rw_ops *ops);
74 void ad5592r_remove(struct device *dev);
75 
76 #endif /* __DRIVERS_IIO_DAC_AD5592R_BASE_H__ */
77