• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
109  * struct ade7758_state - device instance specific data
110  * @us:			actual spi_device
111  * @trig:		data ready trigger registered with iio
112  * @tx:			transmit buffer
113  * @rx:			receive buffer
114  * @buf_lock:		mutex to protect tx and rx
115  **/
116 struct ade7758_state {
117 	struct spi_device	*us;
118 	struct iio_trigger	*trig;
119 	u8			*tx;
120 	u8			*rx;
121 	struct mutex		buf_lock;
122 	struct spi_transfer	ring_xfer[4];
123 	struct spi_message	ring_msg;
124 	/*
125 	 * DMA (thus cache coherency maintenance) requires the
126 	 * transfer buffers to live in their own cache lines.
127 	 */
128 	unsigned char		rx_buf[8] ____cacheline_aligned;
129 	unsigned char		tx_buf[8];
130 
131 };
132 
133 #ifdef CONFIG_IIO_BUFFER
134 /* At the moment triggers are only used for ring buffer
135  * filling. This may change!
136  */
137 
138 void ade7758_remove_trigger(struct iio_dev *indio_dev);
139 int ade7758_probe_trigger(struct iio_dev *indio_dev);
140 
141 ssize_t ade7758_read_data_from_ring(struct device *dev,
142 				    struct device_attribute *attr, char *buf);
143 
144 int ade7758_configure_ring(struct iio_dev *indio_dev);
145 void ade7758_unconfigure_ring(struct iio_dev *indio_dev);
146 
147 int ade7758_set_irq(struct device *dev, bool enable);
148 
149 int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val);
150 int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 *val);
151 
152 #else /* CONFIG_IIO_BUFFER */
153 
ade7758_remove_trigger(struct iio_dev * indio_dev)154 static inline void ade7758_remove_trigger(struct iio_dev *indio_dev)
155 {
156 }
157 
ade7758_probe_trigger(struct iio_dev * indio_dev)158 static inline int ade7758_probe_trigger(struct iio_dev *indio_dev)
159 {
160 	return 0;
161 }
162 
ade7758_configure_ring(struct iio_dev * indio_dev)163 static int ade7758_configure_ring(struct iio_dev *indio_dev)
164 {
165 	return 0;
166 }
167 
ade7758_unconfigure_ring(struct iio_dev * indio_dev)168 static inline void ade7758_unconfigure_ring(struct iio_dev *indio_dev)
169 {
170 }
171 
ade7758_initialize_ring(struct iio_ring_buffer * ring)172 static inline int ade7758_initialize_ring(struct iio_ring_buffer *ring)
173 {
174 	return 0;
175 }
176 
ade7758_uninitialize_ring(struct iio_dev * indio_dev)177 static inline void ade7758_uninitialize_ring(struct iio_dev *indio_dev)
178 {
179 }
180 
181 #endif /* CONFIG_IIO_BUFFER */
182 
183 #endif
184