Merge commit 'jwb/next' into merge
[pandora-kernel.git] / drivers / staging / iio / adc / max1363.h
1 #ifndef _MAX1363_H_
2 #define  _MAX1363_H_
3
4 #define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
5
6 /* There is a fair bit more defined here than currently
7  * used, but the intention is to support everything these
8  * chips do in the long run */
9
10 /* see data sheets */
11 /* max1363 and max1236, max1237, max1238, max1239 */
12 #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD   0x00
13 #define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF    0x20
14 #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT   0x40
15 #define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT    0x60
16 #define MAX1363_SETUP_POWER_UP_INT_REF          0x10
17 #define MAX1363_SETUP_POWER_DOWN_INT_REF        0x00
18
19 /* think about includeing max11600 etc - more settings */
20 #define MAX1363_SETUP_EXT_CLOCK                 0x08
21 #define MAX1363_SETUP_INT_CLOCK                 0x00
22 #define MAX1363_SETUP_UNIPOLAR                  0x00
23 #define MAX1363_SETUP_BIPOLAR                   0x04
24 #define MAX1363_SETUP_RESET                     0x00
25 #define MAX1363_SETUP_NORESET                   0x02
26 /* max1363 only - though don't care on others.
27  * For now monitor modes are not implemented as the relevant
28  * line is not connected on my test board.
29  * The definitions are here as I intend to add this soon.
30  */
31 #define MAX1363_SETUP_MONITOR_SETUP             0x01
32
33 /* Specific to the max1363 */
34 #define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
35 #define MAX1363_MON_CONV_RATE_133ksps           0
36 #define MAX1363_MON_CONV_RATE_66_5ksps          0x02
37 #define MAX1363_MON_CONV_RATE_33_3ksps          0x04
38 #define MAX1363_MON_CONV_RATE_16_6ksps          0x06
39 #define MAX1363_MON_CONV_RATE_8_3ksps           0x08
40 #define MAX1363_MON_CONV_RATE_4_2ksps           0x0A
41 #define MAX1363_MON_CONV_RATE_2_0ksps           0x0C
42 #define MAX1363_MON_CONV_RATE_1_0ksps           0x0E
43 #define MAX1363_MON_INT_ENABLE                  0x01
44
45 /* defined for readability reasons */
46 /* All chips */
47 #define MAX1363_CONFIG_BYTE(a) ((a))
48
49 #define MAX1363_CONFIG_SE                       0x01
50 #define MAX1363_CONFIG_DE                       0x00
51 #define MAX1363_CONFIG_SCAN_TO_CS               0x00
52 #define MAX1363_CONFIG_SCAN_SINGLE_8            0x20
53 #define MAX1363_CONFIG_SCAN_MONITOR_MODE        0x40
54 #define MAX1363_CONFIG_SCAN_SINGLE_1            0x60
55 /* max123{6-9} only */
56 #define MAX1236_SCAN_MID_TO_CHANNEL             0x40
57
58 /* max1363 only - merely part of channel selects or don't care for others*/
59 #define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
60
61 #define MAX1363_CHANNEL_SEL(a) ((a) << 1)
62
63 /* max1363 strictly 0x06 - but doesn't matter */
64 #define MAX1363_CHANNEL_SEL_MASK                0x1E
65 #define MAX1363_SCAN_MASK                       0x60
66 #define MAX1363_SE_DE_MASK                      0x01
67
68 /**
69  * struct max1363_mode - scan mode information
70  * @name:       Name used to identify the scan mode.
71  * @conf:       The corresponding value of the configuration register
72  * @numvals:    The number of values returned by a single scan
73  */
74 struct max1363_mode {
75         const char      *name;
76         int8_t          conf;
77         int             numvals;
78 };
79
80 #define MAX1363_MODE_SINGLE(_num) {                                     \
81                 .name = #_num,                                          \
82                         .conf = MAX1363_CHANNEL_SEL(_num)               \
83                         | MAX1363_CONFIG_SCAN_SINGLE_1                  \
84                         | MAX1363_CONFIG_SE,                            \
85                         .numvals = 1,                                   \
86                         }
87
88 #define MAX1363_MODE_SINGLE_TIMES_8(_num) {                             \
89                 .name = #_num"x8",                                      \
90                         .conf = MAX1363_CHANNEL_SEL(_num)               \
91                         | MAX1363_CONFIG_SCAN_SINGLE_8                  \
92                         | MAX1363_CONFIG_SE,                            \
93                         .numvals = 8,                                   \
94                         }
95
96 #define MAX1363_MODE_SCAN_TO_CHANNEL(_num) {                            \
97                 .name = "0..."#_num,                                    \
98                         .conf = MAX1363_CHANNEL_SEL(_num)               \
99                         | MAX1363_CONFIG_SCAN_TO_CS                     \
100                         | MAX1363_CONFIG_SE,                            \
101                         .numvals = _num + 1,                            \
102                         }
103
104
105 /* note not available for max1363 hence naming */
106 #define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num) {                  \
107                 .name = #_mid"..."#_num,                                \
108                         .conf = MAX1363_CHANNEL_SEL(_num)               \
109                         | MAX1236_SCAN_MID_TO_CHANNEL                   \
110                         | MAX1363_CONFIG_SE,                            \
111                         .numvals = _num - _mid + 1                      \
112 }
113
114 #define MAX1363_MODE_DIFF_SINGLE(_nump, _numm) {                        \
115                 .name = #_nump"-"#_numm,                                \
116                         .conf = MAX1363_CHANNEL_SEL(_nump)              \
117                         | MAX1363_CONFIG_SCAN_SINGLE_1                  \
118                         | MAX1363_CONFIG_DE,                            \
119                         .numvals = 1,                                   \
120                         }
121
122 #define MAX1363_MODE_DIFF_SINGLE_TIMES_8(_nump, _numm) {                \
123                 .name = #_nump"-"#_numm,                                \
124                         .conf = MAX1363_CHANNEL_SEL(_nump)              \
125                         | MAX1363_CONFIG_SCAN_SINGLE_8                  \
126                         | MAX1363_CONFIG_DE,                            \
127                         .numvals = 1,                                   \
128                         }
129
130 /* Can't think how to automate naming so specify for now */
131 #define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL_NAMED(_name, _num, _numvals) { \
132                 .name = #_name,                                         \
133                         .conf = MAX1363_CHANNEL_SEL(_num)               \
134                         | MAX1363_CONFIG_SCAN_TO_CS                     \
135                         | MAX1363_CONFIG_DE,                            \
136                         .numvals = _numvals,                            \
137                         }
138
139 /* note only available for max1363 hence naming */
140 #define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL_NAMED(_name, _num, _numvals) { \
141     .name = #_name,                                                     \
142                         .conf = MAX1363_CHANNEL_SEL(_num)               \
143                         | MAX1236_SCAN_MID_TO_CHANNEL                   \
144                         | MAX1363_CONFIG_SE,                            \
145                         .numvals = _numvals,                            \
146 }
147
148 /* Not currently handled */
149 #define MAX1363_MODE_MONITOR {                                  \
150                 .name = "monitor",                              \
151                         .conf = MAX1363_CHANNEL_SEL(3)          \
152                         | MAX1363_CONFIG_SCAN_MONITOR_MODE      \
153                         | MAX1363_CONFIG_SE,                    \
154                         .numvals = 10,                          \
155                 }
156
157 /* This may seem an overly long winded way to do this, but at least it makes
158  * clear what all the various options actually do. Alternative suggestions
159  * that don't require user to have intimate knowledge of the chip welcomed.
160  */
161
162 /* This must be maintained along side the max1363_mode_table in max1363_core */
163 enum max1363_modes {
164         /* Single read of a single channel */
165         _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
166         /* Eight reads of a single channel */
167         se0, se1, se2, se3, se4, se5, se6, se7, se8, se9, se10, se11,
168         /* Scan to channel */
169         s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
170         s0to7, s0to8, s0to9, s0to10, s0to11,
171         /* Differential single read */
172         d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
173         d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
174         /* Differential single read 8 times */
175         de0m1, de2m3, de4m5, de6m7, de8m9, de10m11,
176         de1m0, de3m2, de5m4, de7m6, de9m8, de11m10,
177         /* Differential scan to channel */
178         d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
179         d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
180         /* Scan mid to channel max123{6-9} only */
181         s2to3, s6to7, s6to8, s6to9, s6to10, s6to11,
182         /* Differential scan mid to channel */
183         s6m7to8m9, s6m7to10m11, s7m6to9m8, s7m6to11m10,
184 };
185
186 /**
187  * struct max1363_chip_info - chip specifc information
188  * @name:               indentification string for chip
189  * @num_inputs:         number of physical inputs on chip
190  * @int_vref_mv:        the internal reference voltage
191  * @monitor_mode:       whether the chip supports monitor interrupts
192  * @mode_list:          array of available scan modes
193  * @num_modes:          the number of scan modes available
194  * @default_mode:       the scan mode in which the chip starts up
195  */
196 struct max1363_chip_info {
197         const char                      *name;
198         u8                              num_inputs;
199         u16                             int_vref_mv;
200         bool                            monitor_mode;
201         const enum max1363_modes        *mode_list;
202         int                             num_modes;
203         enum max1363_modes              default_mode;
204 };
205
206
207 /**
208  * struct max1363_state - driver instance specific data
209  * @indio_dev:          the industrial I/O device
210  * @client:             i2c_client
211  * @setupbyte:          cache of current device setup byte
212  * @configbyte:         cache of current device config byte
213  * @chip_info:          chip model specific constants, available modes etc
214  * @current_mode:       the scan mode of this chip
215  * @poll_work:          bottom half of polling interrupt handler
216  * @protect_ring:       used to ensure only one polling bh running at a time
217  * @reg:                supply regulator
218  */
219 struct max1363_state {
220         struct iio_dev                  *indio_dev;
221         struct i2c_client               *client;
222         char                            setupbyte;
223         char                            configbyte;
224         const struct max1363_chip_info  *chip_info;
225         const struct max1363_mode       *current_mode;
226         struct work_struct              poll_work;
227         atomic_t                        protect_ring;
228         struct iio_trigger              *trig;
229         struct regulator                *reg;
230 };
231 #ifdef CONFIG_MAX1363_RING_BUFFER
232
233 ssize_t max1363_scan_from_ring(struct device *dev,
234                                struct device_attribute *attr,
235                                char *buf);
236 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev);
237 void max1363_ring_cleanup(struct iio_dev *indio_dev);
238
239 int max1363_initialize_ring(struct iio_ring_buffer *ring);
240 void max1363_uninitialize_ring(struct iio_ring_buffer *ring);
241
242 #else /* CONFIG_MAX1363_RING_BUFFER */
243
244 static inline void max1363_uninitialize_ring(struct iio_ring_buffer *ring)
245 {
246 };
247
248 static inline int max1363_initialize_ring(struct iio_ring_buffer *ring)
249 {
250         return 0;
251 };
252
253
254 static inline ssize_t max1363_scan_from_ring(struct device *dev,
255                                              struct device_attribute *attr,
256                                              char *buf)
257 {
258         return 0;
259 };
260
261 static inline int
262 max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
263 {
264         return 0;
265 };
266
267 static inline void max1363_ring_cleanup(struct iio_dev *indio_dev) {};
268 #endif /* CONFIG_MAX1363_RING_BUFFER */
269 #endif /* _MAX1363_H_ */