372a9f62b3b29ae3b6ae027a61cee14ad830d5a5
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / meter / ade7758.h
1 /*
2  * ADE7758 Poly Phase Multifunction Energy Metering IC driver
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #ifndef _ADE7758_H
10 #define _ADE7758_H
11
12 #define ADE7758_AWATTHR   0x01
13 #define ADE7758_BWATTHR   0x02
14 #define ADE7758_CWATTHR   0x03
15 #define ADE7758_AVARHR    0x04
16 #define ADE7758_BVARHR    0x05
17 #define ADE7758_CVARHR    0x06
18 #define ADE7758_AVAHR     0x07
19 #define ADE7758_BVAHR     0x08
20 #define ADE7758_CVAHR     0x09
21 #define ADE7758_AIRMS     0x0A
22 #define ADE7758_BIRMS     0x0B
23 #define ADE7758_CIRMS     0x0C
24 #define ADE7758_AVRMS     0x0D
25 #define ADE7758_BVRMS     0x0E
26 #define ADE7758_CVRMS     0x0F
27 #define ADE7758_FREQ      0x10
28 #define ADE7758_TEMP      0x11
29 #define ADE7758_WFORM     0x12
30 #define ADE7758_OPMODE    0x13
31 #define ADE7758_MMODE     0x14
32 #define ADE7758_WAVMODE   0x15
33 #define ADE7758_COMPMODE  0x16
34 #define ADE7758_LCYCMODE  0x17
35 #define ADE7758_MASK      0x18
36 #define ADE7758_STATUS    0x19
37 #define ADE7758_RSTATUS   0x1A
38 #define ADE7758_ZXTOUT    0x1B
39 #define ADE7758_LINECYC   0x1C
40 #define ADE7758_SAGCYC    0x1D
41 #define ADE7758_SAGLVL    0x1E
42 #define ADE7758_VPINTLVL  0x1F
43 #define ADE7758_IPINTLVL  0x20
44 #define ADE7758_VPEAK     0x21
45 #define ADE7758_IPEAK     0x22
46 #define ADE7758_GAIN      0x23
47 #define ADE7758_AVRMSGAIN 0x24
48 #define ADE7758_BVRMSGAIN 0x25
49 #define ADE7758_CVRMSGAIN 0x26
50 #define ADE7758_AIGAIN    0x27
51 #define ADE7758_BIGAIN    0x28
52 #define ADE7758_CIGAIN    0x29
53 #define ADE7758_AWG       0x2A
54 #define ADE7758_BWG       0x2B
55 #define ADE7758_CWG       0x2C
56 #define ADE7758_AVARG     0x2D
57 #define ADE7758_BVARG     0x2E
58 #define ADE7758_CVARG     0x2F
59 #define ADE7758_AVAG      0x30
60 #define ADE7758_BVAG      0x31
61 #define ADE7758_CVAG      0x32
62 #define ADE7758_AVRMSOS   0x33
63 #define ADE7758_BVRMSOS   0x34
64 #define ADE7758_CVRMSOS   0x35
65 #define ADE7758_AIRMSOS   0x36
66 #define ADE7758_BIRMSOS   0x37
67 #define ADE7758_CIRMSOS   0x38
68 #define ADE7758_AWAITOS   0x39
69 #define ADE7758_BWAITOS   0x3A
70 #define ADE7758_CWAITOS   0x3B
71 #define ADE7758_AVAROS    0x3C
72 #define ADE7758_BVAROS    0x3D
73 #define ADE7758_CVAROS    0x3E
74 #define ADE7758_APHCAL    0x3F
75 #define ADE7758_BPHCAL    0x40
76 #define ADE7758_CPHCAL    0x41
77 #define ADE7758_WDIV      0x42
78 #define ADE7758_VADIV     0x44
79 #define ADE7758_VARDIV    0x43
80 #define ADE7758_APCFNUM   0x45
81 #define ADE7758_APCFDEN   0x46
82 #define ADE7758_VARCFNUM  0x47
83 #define ADE7758_VARCFDEN  0x48
84 #define ADE7758_CHKSUM    0x7E
85 #define ADE7758_VERSION   0x7F
86
87 #define ADE7758_READ_REG(a)    a
88 #define ADE7758_WRITE_REG(a) ((a) | 0x80)
89
90 #define ADE7758_MAX_TX    8
91 #define ADE7758_MAX_RX    4
92 #define ADE7758_STARTUP_DELAY 1
93
94 #define AD7758_NUM_WAVSEL       5
95 #define AD7758_NUM_PHSEL        3
96 #define AD7758_NUM_WAVESRC      (AD7758_NUM_WAVSEL * AD7758_NUM_PHSEL)
97
98 #define AD7758_PHASE_A          0
99 #define AD7758_PHASE_B          1
100 #define AD7758_PHASE_C          2
101 #define AD7758_CURRENT          0
102 #define AD7758_VOLTAGE          1
103 #define AD7758_ACT_PWR          2
104 #define AD7758_REACT_PWR        3
105 #define AD7758_APP_PWR          4
106 #define AD7758_WT(p, w)         (((w) << 2) | (p))
107
108 #define DRIVER_NAME             "ade7758"
109
110
111 /**
112  * struct ade7758_state - device instance specific data
113  * @us:                 actual spi_device
114  * @indio_dev:          industrial I/O device structure
115  * @trig:               data ready trigger registered with iio
116  * @tx:                 transmit buffer
117  * @rx:                 receive buffer
118  * @buf_lock:           mutex to protect tx and rx
119  **/
120 struct ade7758_state {
121         struct spi_device       *us;
122         struct iio_dev          *indio_dev;
123         struct iio_trigger      *trig;
124         u8                      *tx;
125         u8                      *rx;
126         struct mutex            buf_lock;
127         u32                     available_scan_masks[AD7758_NUM_WAVESRC];
128         struct iio_chan_spec    *ade7758_ring_channels;
129         struct spi_transfer             ring_xfer[4];
130         struct spi_message              ring_msg;
131         /*
132          * DMA (thus cache coherency maintenance) requires the
133          * transfer buffers to live in their own cache lines.
134          */
135         unsigned char                   rx_buf[8] ____cacheline_aligned;
136         unsigned char                   tx_buf[8];
137
138 };
139 #ifdef CONFIG_IIO_RING_BUFFER
140 /* At the moment triggers are only used for ring buffer
141  * filling. This may change!
142  */
143
144 void ade7758_remove_trigger(struct iio_dev *indio_dev);
145 int ade7758_probe_trigger(struct iio_dev *indio_dev);
146
147 ssize_t ade7758_read_data_from_ring(struct device *dev,
148                 struct device_attribute *attr,
149                 char *buf);
150
151
152 int ade7758_configure_ring(struct iio_dev *indio_dev);
153 void ade7758_unconfigure_ring(struct iio_dev *indio_dev);
154
155 int ade7758_initialize_ring(struct iio_ring_buffer *ring);
156 void ade7758_uninitialize_ring(struct iio_ring_buffer *ring);
157 int ade7758_set_irq(struct device *dev, bool enable);
158
159 int ade7758_spi_write_reg_8(struct device *dev,
160                 u8 reg_address, u8 val);
161 int ade7758_spi_read_reg_8(struct device *dev,
162                 u8 reg_address, u8 *val);
163
164 #else /* CONFIG_IIO_RING_BUFFER */
165
166 static inline void ade7758_remove_trigger(struct iio_dev *indio_dev)
167 {
168 }
169 static inline int ade7758_probe_trigger(struct iio_dev *indio_dev)
170 {
171         return 0;
172 }
173
174 static int ade7758_configure_ring(struct iio_dev *indio_dev)
175 {
176         return 0;
177 }
178 static inline void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
179 {
180 }
181 static inline int ade7758_initialize_ring(struct iio_ring_buffer *ring)
182 {
183         return 0;
184 }
185 static inline void ade7758_uninitialize_ring(struct iio_ring_buffer *ring)
186 {
187 }
188 #endif /* CONFIG_IIO_RING_BUFFER */
189
190 #endif