blob: 46732380566fd7b3728ad391acb8aefdfd2bcf93 [file] [log] [blame]
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001 /*
Jonathan Cameron82020b02010-05-04 14:43:03 +01002 * iio/adc/max1363.c
3 * Copyright (C) 2008-2010 Jonathan Cameron
4 *
5 * based on linux/drivers/i2c/chips/max123x
6 * Copyright (C) 2002-2004 Stefan Eletzhofer
7 *
8 * based on linux/drivers/acron/char/pcf8583.c
9 * Copyright (C) 2000 Russell King
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * max1363.c
16 *
17 * Partial support for max1363 and similar chips.
18 *
19 * Not currently implemented.
20 *
Jonathan Cameron82020b02010-05-04 14:43:03 +010021 * - Control of internal reference.
22 */
Jonathan Camerond1325cf2009-08-18 18:06:20 +010023
24#include <linux/interrupt.h>
Jonathan Camerond1325cf2009-08-18 18:06:20 +010025#include <linux/device.h>
26#include <linux/kernel.h>
27#include <linux/sysfs.h>
28#include <linux/list.h>
29#include <linux/i2c.h>
Jonathan Camerond1325cf2009-08-18 18:06:20 +010030#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jonathan Cameronc40ab872010-06-26 12:54:14 +010032#include <linux/err.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040033#include <linux/module.h>
Jonathan Camerond1325cf2009-08-18 18:06:20 +010034
Jonathan Cameron06458e22012-04-25 15:54:58 +010035#include <linux/iio/iio.h>
36#include <linux/iio/sysfs.h>
37#include <linux/iio/events.h>
38#include <linux/iio/buffer.h>
39#include <linux/iio/driver.h>
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +010040#include <linux/iio/kfifo_buf.h>
41#include <linux/iio/trigger_consumer.h>
Peter Meerwald4389fbe2012-12-30 23:26:00 +000042#include <linux/iio/triggered_buffer.h>
Jonathan Cameroncdf38702011-08-12 17:08:43 +010043
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +010044#define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
45
46/* There is a fair bit more defined here than currently
47 * used, but the intention is to support everything these
48 * chips do in the long run */
49
50/* see data sheets */
51/* max1363 and max1236, max1237, max1238, max1239 */
52#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 0x00
53#define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF 0x20
54#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT 0x40
55#define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT 0x60
56#define MAX1363_SETUP_POWER_UP_INT_REF 0x10
57#define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
58
Peter Meerwald71d2c122012-12-30 23:26:00 +000059/* think about including max11600 etc - more settings */
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +010060#define MAX1363_SETUP_EXT_CLOCK 0x08
61#define MAX1363_SETUP_INT_CLOCK 0x00
62#define MAX1363_SETUP_UNIPOLAR 0x00
63#define MAX1363_SETUP_BIPOLAR 0x04
64#define MAX1363_SETUP_RESET 0x00
65#define MAX1363_SETUP_NORESET 0x02
66/* max1363 only - though don't care on others.
67 * For now monitor modes are not implemented as the relevant
68 * line is not connected on my test board.
69 * The definitions are here as I intend to add this soon.
70 */
71#define MAX1363_SETUP_MONITOR_SETUP 0x01
72
73/* Specific to the max1363 */
74#define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
75#define MAX1363_MON_INT_ENABLE 0x01
76
77/* defined for readability reasons */
78/* All chips */
79#define MAX1363_CONFIG_BYTE(a) ((a))
80
81#define MAX1363_CONFIG_SE 0x01
82#define MAX1363_CONFIG_DE 0x00
83#define MAX1363_CONFIG_SCAN_TO_CS 0x00
84#define MAX1363_CONFIG_SCAN_SINGLE_8 0x20
85#define MAX1363_CONFIG_SCAN_MONITOR_MODE 0x40
86#define MAX1363_CONFIG_SCAN_SINGLE_1 0x60
87/* max123{6-9} only */
88#define MAX1236_SCAN_MID_TO_CHANNEL 0x40
89
Peter Meerwald71d2c122012-12-30 23:26:00 +000090/* max1363 only - merely part of channel selects or don't care for others */
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +010091#define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
92
93#define MAX1363_CHANNEL_SEL(a) ((a) << 1)
94
95/* max1363 strictly 0x06 - but doesn't matter */
96#define MAX1363_CHANNEL_SEL_MASK 0x1E
97#define MAX1363_SCAN_MASK 0x60
98#define MAX1363_SE_DE_MASK 0x01
99
100#define MAX1363_MAX_CHANNELS 25
101/**
102 * struct max1363_mode - scan mode information
103 * @conf: The corresponding value of the configuration register
104 * @modemask: Bit mask corresponding to channels enabled in this mode
105 */
106struct max1363_mode {
107 int8_t conf;
108 DECLARE_BITMAP(modemask, MAX1363_MAX_CHANNELS);
109};
110
111/* This must be maintained along side the max1363_mode_table in max1363_core */
112enum max1363_modes {
113 /* Single read of a single channel */
114 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
115 /* Differential single read */
116 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
117 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
118 /* Scan to channel and mid to channel where overlapping */
119 s0to1, s0to2, s2to3, s0to3, s0to4, s0to5, s0to6,
120 s6to7, s0to7, s6to8, s0to8, s6to9,
121 s0to9, s6to10, s0to10, s6to11, s0to11,
122 /* Differential scan to channel and mid to channel where overlapping */
123 d0m1to2m3, d0m1to4m5, d0m1to6m7, d6m7to8m9,
124 d0m1to8m9, d6m7to10m11, d0m1to10m11, d1m0to3m2,
125 d1m0to5m4, d1m0to7m6, d7m6to9m8, d1m0to9m8,
126 d7m6to11m10, d1m0to11m10,
127};
128
129/**
130 * struct max1363_chip_info - chip specifc information
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100131 * @info: iio core function callbacks structure
Jonathan Cameron2593b132012-10-06 14:37:44 +0100132 * @channels: channel specification
133 * @num_channels: number of channels
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100134 * @mode_list: array of available scan modes
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100135 * @default_mode: the scan mode in which the chip starts up
Jonathan Cameron2593b132012-10-06 14:37:44 +0100136 * @int_vref_mv: the internal reference voltage
Peter Meerwald71d2c122012-12-30 23:26:00 +0000137 * @num_modes: number of modes
Jonathan Cameron2593b132012-10-06 14:37:44 +0100138 * @bits: accuracy of the adc in bits
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100139 */
140struct max1363_chip_info {
141 const struct iio_info *info;
Jonathan Cameron2593b132012-10-06 14:37:44 +0100142 const struct iio_chan_spec *channels;
143 int num_channels;
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100144 const enum max1363_modes *mode_list;
145 enum max1363_modes default_mode;
146 u16 int_vref_mv;
147 u8 num_modes;
148 u8 bits;
149};
150
151/**
152 * struct max1363_state - driver instance specific data
153 * @client: i2c_client
154 * @setupbyte: cache of current device setup byte
155 * @configbyte: cache of current device config byte
Peter Meerwald71d2c122012-12-30 23:26:00 +0000156 * @chip_info: chip model specific constants, available modes, etc.
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100157 * @current_mode: the scan mode of this chip
158 * @requestedmask: a valid requested set of channels
159 * @reg: supply regulator
160 * @monitor_on: whether monitor mode is enabled
161 * @monitor_speed: parameter corresponding to device monitor speed setting
162 * @mask_high: bitmask for enabled high thresholds
163 * @mask_low: bitmask for enabled low thresholds
164 * @thresh_high: high threshold values
165 * @thresh_low: low threshold values
166 */
167struct max1363_state {
168 struct i2c_client *client;
169 u8 setupbyte;
170 u8 configbyte;
171 const struct max1363_chip_info *chip_info;
172 const struct max1363_mode *current_mode;
173 u32 requestedmask;
174 struct regulator *reg;
175
176 /* Using monitor modes and buffer at the same time is
177 currently not supported */
178 bool monitor_on;
179 unsigned int monitor_speed:3;
180 u8 mask_high;
181 u8 mask_low;
182 /* 4x unipolar first then the fours bipolar ones */
183 s16 thresh_high[8];
184 s16 thresh_low[8];
185};
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100186
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100187#define MAX1363_MODE_SINGLE(_num, _mask) { \
188 .conf = MAX1363_CHANNEL_SEL(_num) \
189 | MAX1363_CONFIG_SCAN_SINGLE_1 \
190 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100191 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100192 }
193
194#define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
195 .conf = MAX1363_CHANNEL_SEL(_num) \
196 | MAX1363_CONFIG_SCAN_TO_CS \
197 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100198 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100199 }
200
201/* note not available for max1363 hence naming */
202#define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
203 .conf = MAX1363_CHANNEL_SEL(_num) \
204 | MAX1236_SCAN_MID_TO_CHANNEL \
205 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100206 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100207}
208
209#define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
210 .conf = MAX1363_CHANNEL_SEL(_nump) \
211 | MAX1363_CONFIG_SCAN_SINGLE_1 \
212 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100213 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100214 }
215
216/* Can't think how to automate naming so specify for now */
217#define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
218 .conf = MAX1363_CHANNEL_SEL(_num) \
219 | MAX1363_CONFIG_SCAN_TO_CS \
220 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100221 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100222 }
223
224/* note only available for max1363 hence naming */
225#define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
226 .conf = MAX1363_CHANNEL_SEL(_num) \
227 | MAX1236_SCAN_MID_TO_CHANNEL \
228 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100229 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100230}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100231
Jonathan Cameron82020b02010-05-04 14:43:03 +0100232static const struct max1363_mode max1363_mode_table[] = {
233 /* All of the single channel options first */
234 MAX1363_MODE_SINGLE(0, 1 << 0),
235 MAX1363_MODE_SINGLE(1, 1 << 1),
236 MAX1363_MODE_SINGLE(2, 1 << 2),
237 MAX1363_MODE_SINGLE(3, 1 << 3),
238 MAX1363_MODE_SINGLE(4, 1 << 4),
239 MAX1363_MODE_SINGLE(5, 1 << 5),
240 MAX1363_MODE_SINGLE(6, 1 << 6),
241 MAX1363_MODE_SINGLE(7, 1 << 7),
242 MAX1363_MODE_SINGLE(8, 1 << 8),
243 MAX1363_MODE_SINGLE(9, 1 << 9),
244 MAX1363_MODE_SINGLE(10, 1 << 10),
245 MAX1363_MODE_SINGLE(11, 1 << 11),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100246
Jonathan Cameron82020b02010-05-04 14:43:03 +0100247 MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
248 MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
249 MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
250 MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
251 MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
252 MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
253 MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
254 MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
255 MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
256 MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
257 MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
258 MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100259
Jonathan Cameron82020b02010-05-04 14:43:03 +0100260 /* The multichannel scans next */
261 MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
262 MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
263 MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
264 MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
265 MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
266 MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
267 MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
268 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
269 MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
270 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
271 MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
272 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
273 MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
274 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
275 MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
276 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
277 MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100278
Jonathan Cameron82020b02010-05-04 14:43:03 +0100279 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
280 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
281 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
282 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
283 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
284 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
285 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
286 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
287 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
288 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
289 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
290 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
291 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
292 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100293};
294
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100295static const struct max1363_mode
Jonathan Cameronc52cfb62011-12-05 21:37:15 +0000296*max1363_match_mode(const unsigned long *mask,
Peter Meerwald71d2c122012-12-30 23:26:00 +0000297 const struct max1363_chip_info *ci)
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700298{
Jonathan Cameron82020b02010-05-04 14:43:03 +0100299 int i;
300 if (mask)
301 for (i = 0; i < ci->num_modes; i++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100302 if (bitmap_subset(mask,
303 max1363_mode_table[ci->mode_list[i]].
304 modemask,
305 MAX1363_MAX_CHANNELS))
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700306 return &max1363_mode_table[ci->mode_list[i]];
307 return NULL;
Mike Frysingerc608cb02010-05-23 03:10:30 -0400308}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100309
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100310static int max1363_write_basic_config(struct i2c_client *client,
311 unsigned char d1,
312 unsigned char d2)
313{
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100314 u8 tx_buf[2] = {d1, d2};
Jonathan Cameron82020b02010-05-04 14:43:03 +0100315
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100316 return i2c_master_send(client, tx_buf, 2);
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100317}
318
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100319static int max1363_set_scan_mode(struct max1363_state *st)
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100320{
321 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
322 | MAX1363_SCAN_MASK
323 | MAX1363_SE_DE_MASK);
324 st->configbyte |= st->current_mode->conf;
325
326 return max1363_write_basic_config(st->client,
327 st->setupbyte,
328 st->configbyte);
329}
330
Jonathan Cameron298cd972011-05-18 14:40:53 +0100331static int max1363_read_single_chan(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *chan,
333 int *val,
334 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100335{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100336 int ret = 0;
337 s32 data;
Guenter Roeck482bb4e2013-01-31 21:43:00 +0000338 u8 rxbuf[2];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100339 struct max1363_state *st = iio_priv(indio_dev);
340 struct i2c_client *client = st->client;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100341
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100342 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100343 /*
344 * If monitor mode is enabled, the method for reading a single
345 * channel will have to be rather different and has not yet
346 * been implemented.
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000347 *
348 * Also, cannot read directly if buffered capture enabled.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100349 */
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000350 if (st->monitor_on || iio_buffer_enabled(indio_dev)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100351 ret = -EBUSY;
352 goto error_ret;
353 }
354
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000355 /* Check to see if current scan mode is correct */
356 if (st->current_mode != &max1363_mode_table[chan->address]) {
357 /* Update scan mode if needed */
358 st->current_mode = &max1363_mode_table[chan->address];
359 ret = max1363_set_scan_mode(st);
360 if (ret < 0)
361 goto error_ret;
362 }
363 if (st->chip_info->bits != 8) {
364 /* Get reading */
365 data = i2c_master_recv(client, rxbuf, 2);
Jonathan Cameron82020b02010-05-04 14:43:03 +0100366 if (data < 0) {
367 ret = data;
368 goto error_ret;
369 }
Guenter Roeck482bb4e2013-01-31 21:43:00 +0000370 data = (rxbuf[1] | rxbuf[0] << 8) &
371 ((1 << st->chip_info->bits) - 1);
Jonathan Cameron82020b02010-05-04 14:43:03 +0100372 } else {
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000373 /* Get reading */
374 data = i2c_master_recv(client, rxbuf, 1);
375 if (data < 0) {
376 ret = data;
377 goto error_ret;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100378 }
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000379 data = rxbuf[0];
Jonathan Cameron82020b02010-05-04 14:43:03 +0100380 }
Jonathan Cameron298cd972011-05-18 14:40:53 +0100381 *val = data;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100382error_ret:
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100383 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100384 return ret;
385
Jonathan Cameron82020b02010-05-04 14:43:03 +0100386}
387
Jonathan Cameron298cd972011-05-18 14:40:53 +0100388static int max1363_read_raw(struct iio_dev *indio_dev,
389 struct iio_chan_spec const *chan,
390 int *val,
391 int *val2,
392 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100393{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100394 struct max1363_state *st = iio_priv(indio_dev);
395 int ret;
396 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100397 case IIO_CHAN_INFO_RAW:
Jonathan Cameron298cd972011-05-18 14:40:53 +0100398 ret = max1363_read_single_chan(indio_dev, chan, val, m);
Jonathan Cameroncd038012011-08-12 16:55:29 +0100399 if (ret < 0)
Jonathan Cameron298cd972011-05-18 14:40:53 +0100400 return ret;
401 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100402 case IIO_CHAN_INFO_SCALE:
Jonathan Cameron298cd972011-05-18 14:40:53 +0100403 if ((1 << (st->chip_info->bits + 1)) >
404 st->chip_info->int_vref_mv) {
405 *val = 0;
406 *val2 = 500000;
407 return IIO_VAL_INT_PLUS_MICRO;
408 } else {
409 *val = (st->chip_info->int_vref_mv)
410 >> st->chip_info->bits;
411 return IIO_VAL_INT;
412 }
413 default:
414 return -EINVAL;
415 }
416 return 0;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100417}
418
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100419/* Applies to max1363 */
420static const enum max1363_modes max1363_mode_list[] = {
421 _s0, _s1, _s2, _s3,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100422 s0to1, s0to2, s0to3,
423 d0m1, d2m3, d1m0, d3m2,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100424 d0m1to2m3, d1m0to3m2,
425};
426
Jonathan Cameron298cd972011-05-18 14:40:53 +0100427#define MAX1363_EV_M \
428 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) \
429 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100430#define MAX1363_INFO_MASK (IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
431 IIO_CHAN_INFO_SCALE_SHARED_BIT)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100432#define MAX1363_CHAN_U(num, addr, si, bits, evmask) \
433 { \
434 .type = IIO_VOLTAGE, \
435 .indexed = 1, \
436 .channel = num, \
437 .address = addr, \
438 .info_mask = MAX1363_INFO_MASK, \
Jonathan Cameronbc234e72011-12-05 21:56:01 +0000439 .datasheet_name = "AIN"#num, \
Jonathan Cameron18cffbed2011-12-05 21:37:16 +0000440 .scan_type = { \
441 .sign = 'u', \
442 .realbits = bits, \
443 .storagebits = (bits > 8) ? 16 : 8, \
444 .endianness = IIO_BE, \
445 }, \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100446 .scan_index = si, \
447 .event_mask = evmask, \
Jonathan Cameron298cd972011-05-18 14:40:53 +0100448 }
449
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100450/* bipolar channel */
451#define MAX1363_CHAN_B(num, num2, addr, si, bits, evmask) \
452 { \
453 .type = IIO_VOLTAGE, \
454 .differential = 1, \
455 .indexed = 1, \
456 .channel = num, \
457 .channel2 = num2, \
458 .address = addr, \
459 .info_mask = MAX1363_INFO_MASK, \
Jonathan Cameronbc234e72011-12-05 21:56:01 +0000460 .datasheet_name = "AIN"#num"-AIN"#num2, \
Jonathan Cameron18cffbed2011-12-05 21:37:16 +0000461 .scan_type = { \
462 .sign = 's', \
463 .realbits = bits, \
464 .storagebits = (bits > 8) ? 16 : 8, \
465 .endianness = IIO_BE, \
466 }, \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100467 .scan_index = si, \
468 .event_mask = evmask, \
469 }
470
471#define MAX1363_4X_CHANS(bits, em) { \
472 MAX1363_CHAN_U(0, _s0, 0, bits, em), \
473 MAX1363_CHAN_U(1, _s1, 1, bits, em), \
474 MAX1363_CHAN_U(2, _s2, 2, bits, em), \
475 MAX1363_CHAN_U(3, _s3, 3, bits, em), \
476 MAX1363_CHAN_B(0, 1, d0m1, 4, bits, em), \
477 MAX1363_CHAN_B(2, 3, d2m3, 5, bits, em), \
478 MAX1363_CHAN_B(1, 0, d1m0, 6, bits, em), \
479 MAX1363_CHAN_B(3, 2, d3m2, 7, bits, em), \
480 IIO_CHAN_SOFT_TIMESTAMP(8) \
481 }
482
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100483static const struct iio_chan_spec max1036_channels[] = MAX1363_4X_CHANS(8, 0);
484static const struct iio_chan_spec max1136_channels[] = MAX1363_4X_CHANS(10, 0);
485static const struct iio_chan_spec max1236_channels[] = MAX1363_4X_CHANS(12, 0);
486static const struct iio_chan_spec max1361_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100487 MAX1363_4X_CHANS(10, MAX1363_EV_M);
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100488static const struct iio_chan_spec max1363_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100489 MAX1363_4X_CHANS(12, MAX1363_EV_M);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100490
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800491/* Applies to max1236, max1237 */
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100492static const enum max1363_modes max1236_mode_list[] = {
493 _s0, _s1, _s2, _s3,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100494 s0to1, s0to2, s0to3,
495 d0m1, d2m3, d1m0, d3m2,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100496 d0m1to2m3, d1m0to3m2,
497 s2to3,
498};
499
500/* Applies to max1238, max1239 */
501static const enum max1363_modes max1238_mode_list[] = {
502 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100503 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
504 s0to7, s0to8, s0to9, s0to10, s0to11,
505 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
506 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100507 d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
508 d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
509 s6to7, s6to8, s6to9, s6to10, s6to11,
Jonathan Cameron82020b02010-05-04 14:43:03 +0100510 d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100511};
512
Jonathan Cameron298cd972011-05-18 14:40:53 +0100513#define MAX1363_12X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100514 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
515 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
516 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
517 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
518 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
519 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
520 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
521 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
522 MAX1363_CHAN_U(8, _s8, 8, bits, 0), \
523 MAX1363_CHAN_U(9, _s9, 9, bits, 0), \
524 MAX1363_CHAN_U(10, _s10, 10, bits, 0), \
525 MAX1363_CHAN_U(11, _s11, 11, bits, 0), \
526 MAX1363_CHAN_B(0, 1, d0m1, 12, bits, 0), \
527 MAX1363_CHAN_B(2, 3, d2m3, 13, bits, 0), \
528 MAX1363_CHAN_B(4, 5, d4m5, 14, bits, 0), \
529 MAX1363_CHAN_B(6, 7, d6m7, 15, bits, 0), \
530 MAX1363_CHAN_B(8, 9, d8m9, 16, bits, 0), \
531 MAX1363_CHAN_B(10, 11, d10m11, 17, bits, 0), \
532 MAX1363_CHAN_B(1, 0, d1m0, 18, bits, 0), \
533 MAX1363_CHAN_B(3, 2, d3m2, 19, bits, 0), \
534 MAX1363_CHAN_B(5, 4, d5m4, 20, bits, 0), \
535 MAX1363_CHAN_B(7, 6, d7m6, 21, bits, 0), \
536 MAX1363_CHAN_B(9, 8, d9m8, 22, bits, 0), \
537 MAX1363_CHAN_B(11, 10, d11m10, 23, bits, 0), \
Jonathan Cameron298cd972011-05-18 14:40:53 +0100538 IIO_CHAN_SOFT_TIMESTAMP(24) \
539 }
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100540static const struct iio_chan_spec max1038_channels[] = MAX1363_12X_CHANS(8);
541static const struct iio_chan_spec max1138_channels[] = MAX1363_12X_CHANS(10);
542static const struct iio_chan_spec max1238_channels[] = MAX1363_12X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100543
544static const enum max1363_modes max11607_mode_list[] = {
545 _s0, _s1, _s2, _s3,
546 s0to1, s0to2, s0to3,
547 s2to3,
548 d0m1, d2m3, d1m0, d3m2,
549 d0m1to2m3, d1m0to3m2,
550};
551
552static const enum max1363_modes max11608_mode_list[] = {
553 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
554 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
555 s6to7,
556 d0m1, d2m3, d4m5, d6m7,
557 d1m0, d3m2, d5m4, d7m6,
558 d0m1to2m3, d0m1to4m5, d0m1to6m7,
559 d1m0to3m2, d1m0to5m4, d1m0to7m6,
560};
561
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100562#define MAX1363_8X_CHANS(bits) { \
563 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
564 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
565 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
566 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
567 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
568 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
569 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
570 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
571 MAX1363_CHAN_B(0, 1, d0m1, 8, bits, 0), \
572 MAX1363_CHAN_B(2, 3, d2m3, 9, bits, 0), \
573 MAX1363_CHAN_B(4, 5, d4m5, 10, bits, 0), \
574 MAX1363_CHAN_B(6, 7, d6m7, 11, bits, 0), \
575 MAX1363_CHAN_B(1, 0, d1m0, 12, bits, 0), \
576 MAX1363_CHAN_B(3, 2, d3m2, 13, bits, 0), \
577 MAX1363_CHAN_B(5, 4, d5m4, 14, bits, 0), \
578 MAX1363_CHAN_B(7, 6, d7m6, 15, bits, 0), \
579 IIO_CHAN_SOFT_TIMESTAMP(16) \
580}
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100581static const struct iio_chan_spec max11602_channels[] = MAX1363_8X_CHANS(8);
582static const struct iio_chan_spec max11608_channels[] = MAX1363_8X_CHANS(10);
583static const struct iio_chan_spec max11614_channels[] = MAX1363_8X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100584
Jonathan Cameron586d1522011-05-18 14:42:10 +0100585static const enum max1363_modes max11644_mode_list[] = {
586 _s0, _s1, s0to1, d0m1, d1m0,
587};
588
589#define MAX1363_2X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100590 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
591 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
592 MAX1363_CHAN_B(0, 1, d0m1, 2, bits, 0), \
593 MAX1363_CHAN_B(1, 0, d1m0, 3, bits, 0), \
Jonathan Cameron586d1522011-05-18 14:42:10 +0100594 IIO_CHAN_SOFT_TIMESTAMP(4) \
595 }
596
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100597static const struct iio_chan_spec max11646_channels[] = MAX1363_2X_CHANS(10);
598static const struct iio_chan_spec max11644_channels[] = MAX1363_2X_CHANS(12);
Jonathan Cameron586d1522011-05-18 14:42:10 +0100599
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100600enum { max1361,
601 max1362,
602 max1363,
603 max1364,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100604 max1036,
605 max1037,
606 max1038,
607 max1039,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100608 max1136,
609 max1137,
610 max1138,
611 max1139,
612 max1236,
613 max1237,
614 max1238,
615 max1239,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100616 max11600,
617 max11601,
618 max11602,
619 max11603,
620 max11604,
621 max11605,
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100622 max11606,
623 max11607,
624 max11608,
625 max11609,
626 max11610,
627 max11611,
628 max11612,
629 max11613,
630 max11614,
631 max11615,
632 max11616,
633 max11617,
Jonathan Cameron586d1522011-05-18 14:42:10 +0100634 max11644,
635 max11645,
636 max11646,
637 max11647
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100638};
639
Jonathan Cameron440a5202010-06-26 12:54:23 +0100640static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
641 8300, 4200, 2000, 1000 };
642
643static ssize_t max1363_monitor_show_freq(struct device *dev,
644 struct device_attribute *attr,
645 char *buf)
646{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200647 struct max1363_state *st = iio_priv(dev_to_iio_dev(dev));
Jonathan Cameron440a5202010-06-26 12:54:23 +0100648 return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
649}
650
651static ssize_t max1363_monitor_store_freq(struct device *dev,
652 struct device_attribute *attr,
653 const char *buf,
654 size_t len)
655{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200656 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100657 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100658 int i, ret;
659 unsigned long val;
660 bool found = false;
661
662 ret = strict_strtoul(buf, 10, &val);
663 if (ret)
664 return -EINVAL;
665 for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
666 if (val == max1363_monitor_speeds[i]) {
667 found = true;
668 break;
669 }
670 if (!found)
671 return -EINVAL;
672
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100673 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100674 st->monitor_speed = i;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100675 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100676
677 return 0;
678}
679
680static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
681 max1363_monitor_show_freq,
682 max1363_monitor_store_freq);
683
684static IIO_CONST_ATTR(sampling_frequency_available,
685 "133000 665000 33300 16600 8300 4200 2000 1000");
686
Jonathan Cameron298cd972011-05-18 14:40:53 +0100687static int max1363_read_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100688 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100689 int *val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100690{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100691 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100692 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100693 *val = st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron440a5202010-06-26 12:54:23 +0100694 else
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100695 *val = st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100696 return 0;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100697}
698
Jonathan Cameron298cd972011-05-18 14:40:53 +0100699static int max1363_write_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100700 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100701 int val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100702{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100703 struct max1363_state *st = iio_priv(indio_dev);
704 /* make it handle signed correctly as well */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100705 switch (st->chip_info->bits) {
706 case 10:
707 if (val > 0x3FF)
708 return -EINVAL;
709 break;
710 case 12:
711 if (val > 0xFFF)
712 return -EINVAL;
713 break;
714 }
715
Jonathan Cameron298cd972011-05-18 14:40:53 +0100716 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
717 case IIO_EV_DIR_FALLING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100718 st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100719 break;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100720 case IIO_EV_DIR_RISING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100721 st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100722 break;
723 }
724
Jonathan Cameron440a5202010-06-26 12:54:23 +0100725 return 0;
726}
727
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100728static const u64 max1363_event_codes[] = {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100729 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
730 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
731 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
732 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
733 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
734 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
735 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
736 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
737 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
738 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
739 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
740 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
741 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
742 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
743 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
744 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100745};
746
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100747static irqreturn_t max1363_event_handler(int irq, void *private)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100748{
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100749 struct iio_dev *indio_dev = private;
750 struct max1363_state *st = iio_priv(indio_dev);
751 s64 timestamp = iio_get_time_ns();
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100752 unsigned long mask, loc;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100753 u8 rx;
754 u8 tx[2] = { st->setupbyte,
755 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
756
757 i2c_master_recv(st->client, &rx, 1);
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100758 mask = rx;
759 for_each_set_bit(loc, &mask, 8)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100760 iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100761 i2c_master_send(st->client, tx, 2);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100762
763 return IRQ_HANDLED;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100764}
765
Jonathan Cameron298cd972011-05-18 14:40:53 +0100766static int max1363_read_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100767 u64 event_code)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100768{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100769 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100770 int val;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100771 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron0d9fa2c2012-10-06 14:38:11 +0100772
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100773 mutex_lock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100774 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
775 val = (1 << number) & st->mask_low;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100776 else
Jonathan Cameron298cd972011-05-18 14:40:53 +0100777 val = (1 << number) & st->mask_high;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100778 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100779
Jonathan Cameron298cd972011-05-18 14:40:53 +0100780 return val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100781}
782
783static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
784{
785 u8 *tx_buf;
786 int ret, i = 3, j;
787 unsigned long numelements;
788 int len;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100789 const long *modemask;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100790
791 if (!enabled) {
Jonathan Cameronec443722012-10-06 14:19:01 +0100792 /* transition to buffered capture is not currently supported */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100793 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
794 st->configbyte &= ~MAX1363_SCAN_MASK;
795 st->monitor_on = false;
796 return max1363_write_basic_config(st->client,
797 st->setupbyte,
798 st->configbyte);
799 }
800
801 /* Ensure we are in the relevant mode */
802 st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
803 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
804 | MAX1363_SCAN_MASK
805 | MAX1363_SE_DE_MASK);
806 st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
807 if ((st->mask_low | st->mask_high) & 0x0F) {
808 st->configbyte |= max1363_mode_table[s0to3].conf;
809 modemask = max1363_mode_table[s0to3].modemask;
810 } else if ((st->mask_low | st->mask_high) & 0x30) {
811 st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
812 modemask = max1363_mode_table[d0m1to2m3].modemask;
813 } else {
814 st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
815 modemask = max1363_mode_table[d1m0to3m2].modemask;
816 }
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100817 numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100818 len = 3 * numelements + 3;
819 tx_buf = kmalloc(len, GFP_KERNEL);
820 if (!tx_buf) {
821 ret = -ENOMEM;
822 goto error_ret;
823 }
824 tx_buf[0] = st->configbyte;
825 tx_buf[1] = st->setupbyte;
826 tx_buf[2] = (st->monitor_speed << 1);
827
828 /*
829 * So we need to do yet another bit of nefarious scan mode
830 * setup to match what we need.
831 */
832 for (j = 0; j < 8; j++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100833 if (test_bit(j, modemask)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100834 /* Establish the mode is in the scan */
835 if (st->mask_low & (1 << j)) {
836 tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
837 tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
838 } else if (j < 4) {
839 tx_buf[i] = 0;
840 tx_buf[i + 1] = 0;
841 } else {
842 tx_buf[i] = 0x80;
843 tx_buf[i + 1] = 0;
844 }
845 if (st->mask_high & (1 << j)) {
846 tx_buf[i + 1] |=
847 (st->thresh_high[j] >> 8) & 0x0F;
848 tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
849 } else if (j < 4) {
850 tx_buf[i + 1] |= 0x0F;
851 tx_buf[i + 2] = 0xFF;
852 } else {
853 tx_buf[i + 1] |= 0x07;
854 tx_buf[i + 2] = 0xFF;
855 }
856 i += 3;
857 }
858
859
860 ret = i2c_master_send(st->client, tx_buf, len);
861 if (ret < 0)
862 goto error_ret;
863 if (ret != len) {
864 ret = -EIO;
865 goto error_ret;
866 }
867
868 /*
869 * Now that we hopefully have sensible thresholds in place it is
870 * time to turn the interrupts on.
871 * It is unclear from the data sheet if this should be necessary
872 * (i.e. whether monitor mode setup is atomic) but it appears to
873 * be in practice.
874 */
875 tx_buf[0] = st->setupbyte;
876 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
877 ret = i2c_master_send(st->client, tx_buf, 2);
878 if (ret < 0)
879 goto error_ret;
880 if (ret != 2) {
881 ret = -EIO;
882 goto error_ret;
883 }
884 ret = 0;
885 st->monitor_on = true;
886error_ret:
887
888 kfree(tx_buf);
889
890 return ret;
891}
892
893/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300894 * To keep this manageable we always use one of 3 scan modes.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100895 * Scan 0...3, 0-1,2-3 and 1-0,3-2
896 */
Jonathan Cameron298cd972011-05-18 14:40:53 +0100897
Jonathan Cameron440a5202010-06-26 12:54:23 +0100898static inline int __max1363_check_event_mask(int thismask, int checkmask)
899{
900 int ret = 0;
901 /* Is it unipolar */
902 if (thismask < 4) {
903 if (checkmask & ~0x0F) {
904 ret = -EBUSY;
905 goto error_ret;
906 }
907 } else if (thismask < 6) {
908 if (checkmask & ~0x30) {
909 ret = -EBUSY;
910 goto error_ret;
911 }
912 } else if (checkmask & ~0xC0)
913 ret = -EBUSY;
914error_ret:
915 return ret;
916}
917
Jonathan Cameron298cd972011-05-18 14:40:53 +0100918static int max1363_write_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100919 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100920 int state)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100921{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100922 int ret = 0;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100923 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100924 u16 unifiedmask;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100925 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100926
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100927 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100928 unifiedmask = st->mask_low | st->mask_high;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100929 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING) {
930
931 if (state == 0)
932 st->mask_low &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100933 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100934 ret = __max1363_check_event_mask((1 << number),
935 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100936 if (ret)
937 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100938 st->mask_low |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100939 }
940 } else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100941 if (state == 0)
942 st->mask_high &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100943 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100944 ret = __max1363_check_event_mask((1 << number),
945 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100946 if (ret)
947 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100948 st->mask_high |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100949 }
950 }
Jonathan Cameron440a5202010-06-26 12:54:23 +0100951
952 max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
953error_ret:
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100954 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100955
Jonathan Cameron298cd972011-05-18 14:40:53 +0100956 return ret;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100957}
958
Jonathan Cameron440a5202010-06-26 12:54:23 +0100959/*
960 * As with scan_elements, only certain sets of these can
961 * be combined.
962 */
963static struct attribute *max1363_event_attributes[] = {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100964 &iio_dev_attr_sampling_frequency.dev_attr.attr,
965 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Jonathan Cameron440a5202010-06-26 12:54:23 +0100966 NULL,
967};
968
969static struct attribute_group max1363_event_attribute_group = {
970 .attrs = max1363_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100971 .name = "events",
Jonathan Cameron440a5202010-06-26 12:54:23 +0100972};
973
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100974static int max1363_update_scan_mode(struct iio_dev *indio_dev,
975 const unsigned long *scan_mask)
976{
977 struct max1363_state *st = iio_priv(indio_dev);
978
979 /*
980 * Need to figure out the current mode based upon the requested
981 * scan mask in iio_dev
982 */
983 st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
984 if (!st->current_mode)
985 return -EINVAL;
986 max1363_set_scan_mode(st);
987 return 0;
988}
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100989
990static const struct iio_info max1238_info = {
991 .read_raw = &max1363_read_raw,
992 .driver_module = THIS_MODULE,
Jonathan Camerona9ea1b12012-04-10 21:11:05 +0100993 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100994};
995
996static const struct iio_info max1363_info = {
997 .read_event_value = &max1363_read_thresh,
998 .write_event_value = &max1363_write_thresh,
999 .read_event_config = &max1363_read_event_config,
1000 .write_event_config = &max1363_write_event_config,
1001 .read_raw = &max1363_read_raw,
Jonathan Cameronc52cfb62011-12-05 21:37:15 +00001002 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001003 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001004 .event_attrs = &max1363_event_attribute_group,
1005};
1006
1007/* max1363 and max1368 tested - rest from data sheet */
1008static const struct max1363_chip_info max1363_chip_info_tbl[] = {
1009 [max1361] = {
1010 .bits = 10,
1011 .int_vref_mv = 2048,
1012 .mode_list = max1363_mode_list,
1013 .num_modes = ARRAY_SIZE(max1363_mode_list),
1014 .default_mode = s0to3,
1015 .channels = max1361_channels,
1016 .num_channels = ARRAY_SIZE(max1361_channels),
1017 .info = &max1363_info,
1018 },
1019 [max1362] = {
1020 .bits = 10,
1021 .int_vref_mv = 4096,
1022 .mode_list = max1363_mode_list,
1023 .num_modes = ARRAY_SIZE(max1363_mode_list),
1024 .default_mode = s0to3,
1025 .channels = max1361_channels,
1026 .num_channels = ARRAY_SIZE(max1361_channels),
1027 .info = &max1363_info,
1028 },
1029 [max1363] = {
1030 .bits = 12,
1031 .int_vref_mv = 2048,
1032 .mode_list = max1363_mode_list,
1033 .num_modes = ARRAY_SIZE(max1363_mode_list),
1034 .default_mode = s0to3,
1035 .channels = max1363_channels,
1036 .num_channels = ARRAY_SIZE(max1363_channels),
1037 .info = &max1363_info,
1038 },
1039 [max1364] = {
1040 .bits = 12,
1041 .int_vref_mv = 4096,
1042 .mode_list = max1363_mode_list,
1043 .num_modes = ARRAY_SIZE(max1363_mode_list),
1044 .default_mode = s0to3,
1045 .channels = max1363_channels,
1046 .num_channels = ARRAY_SIZE(max1363_channels),
1047 .info = &max1363_info,
1048 },
1049 [max1036] = {
1050 .bits = 8,
1051 .int_vref_mv = 4096,
1052 .mode_list = max1236_mode_list,
1053 .num_modes = ARRAY_SIZE(max1236_mode_list),
1054 .default_mode = s0to3,
1055 .info = &max1238_info,
1056 .channels = max1036_channels,
1057 .num_channels = ARRAY_SIZE(max1036_channels),
1058 },
1059 [max1037] = {
1060 .bits = 8,
1061 .int_vref_mv = 2048,
1062 .mode_list = max1236_mode_list,
1063 .num_modes = ARRAY_SIZE(max1236_mode_list),
1064 .default_mode = s0to3,
1065 .info = &max1238_info,
1066 .channels = max1036_channels,
1067 .num_channels = ARRAY_SIZE(max1036_channels),
1068 },
1069 [max1038] = {
1070 .bits = 8,
1071 .int_vref_mv = 4096,
1072 .mode_list = max1238_mode_list,
1073 .num_modes = ARRAY_SIZE(max1238_mode_list),
1074 .default_mode = s0to11,
1075 .info = &max1238_info,
1076 .channels = max1038_channels,
1077 .num_channels = ARRAY_SIZE(max1038_channels),
1078 },
1079 [max1039] = {
1080 .bits = 8,
1081 .int_vref_mv = 2048,
1082 .mode_list = max1238_mode_list,
1083 .num_modes = ARRAY_SIZE(max1238_mode_list),
1084 .default_mode = s0to11,
1085 .info = &max1238_info,
1086 .channels = max1038_channels,
1087 .num_channels = ARRAY_SIZE(max1038_channels),
1088 },
1089 [max1136] = {
1090 .bits = 10,
1091 .int_vref_mv = 4096,
1092 .mode_list = max1236_mode_list,
1093 .num_modes = ARRAY_SIZE(max1236_mode_list),
1094 .default_mode = s0to3,
1095 .info = &max1238_info,
1096 .channels = max1136_channels,
1097 .num_channels = ARRAY_SIZE(max1136_channels),
1098 },
1099 [max1137] = {
1100 .bits = 10,
1101 .int_vref_mv = 2048,
1102 .mode_list = max1236_mode_list,
1103 .num_modes = ARRAY_SIZE(max1236_mode_list),
1104 .default_mode = s0to3,
1105 .info = &max1238_info,
1106 .channels = max1136_channels,
1107 .num_channels = ARRAY_SIZE(max1136_channels),
1108 },
1109 [max1138] = {
1110 .bits = 10,
1111 .int_vref_mv = 4096,
1112 .mode_list = max1238_mode_list,
1113 .num_modes = ARRAY_SIZE(max1238_mode_list),
1114 .default_mode = s0to11,
1115 .info = &max1238_info,
1116 .channels = max1138_channels,
1117 .num_channels = ARRAY_SIZE(max1138_channels),
1118 },
1119 [max1139] = {
1120 .bits = 10,
1121 .int_vref_mv = 2048,
1122 .mode_list = max1238_mode_list,
1123 .num_modes = ARRAY_SIZE(max1238_mode_list),
1124 .default_mode = s0to11,
1125 .info = &max1238_info,
1126 .channels = max1138_channels,
1127 .num_channels = ARRAY_SIZE(max1138_channels),
1128 },
1129 [max1236] = {
1130 .bits = 12,
1131 .int_vref_mv = 4096,
1132 .mode_list = max1236_mode_list,
1133 .num_modes = ARRAY_SIZE(max1236_mode_list),
1134 .default_mode = s0to3,
1135 .info = &max1238_info,
1136 .channels = max1236_channels,
1137 .num_channels = ARRAY_SIZE(max1236_channels),
1138 },
1139 [max1237] = {
1140 .bits = 12,
1141 .int_vref_mv = 2048,
1142 .mode_list = max1236_mode_list,
1143 .num_modes = ARRAY_SIZE(max1236_mode_list),
1144 .default_mode = s0to3,
1145 .info = &max1238_info,
1146 .channels = max1236_channels,
1147 .num_channels = ARRAY_SIZE(max1236_channels),
1148 },
1149 [max1238] = {
1150 .bits = 12,
1151 .int_vref_mv = 4096,
1152 .mode_list = max1238_mode_list,
1153 .num_modes = ARRAY_SIZE(max1238_mode_list),
1154 .default_mode = s0to11,
1155 .info = &max1238_info,
1156 .channels = max1238_channels,
1157 .num_channels = ARRAY_SIZE(max1238_channels),
1158 },
1159 [max1239] = {
1160 .bits = 12,
1161 .int_vref_mv = 2048,
1162 .mode_list = max1238_mode_list,
1163 .num_modes = ARRAY_SIZE(max1238_mode_list),
1164 .default_mode = s0to11,
1165 .info = &max1238_info,
1166 .channels = max1238_channels,
1167 .num_channels = ARRAY_SIZE(max1238_channels),
1168 },
1169 [max11600] = {
1170 .bits = 8,
1171 .int_vref_mv = 4096,
1172 .mode_list = max11607_mode_list,
1173 .num_modes = ARRAY_SIZE(max11607_mode_list),
1174 .default_mode = s0to3,
1175 .info = &max1238_info,
1176 .channels = max1036_channels,
1177 .num_channels = ARRAY_SIZE(max1036_channels),
1178 },
1179 [max11601] = {
1180 .bits = 8,
1181 .int_vref_mv = 2048,
1182 .mode_list = max11607_mode_list,
1183 .num_modes = ARRAY_SIZE(max11607_mode_list),
1184 .default_mode = s0to3,
1185 .info = &max1238_info,
1186 .channels = max1036_channels,
1187 .num_channels = ARRAY_SIZE(max1036_channels),
1188 },
1189 [max11602] = {
1190 .bits = 8,
1191 .int_vref_mv = 4096,
1192 .mode_list = max11608_mode_list,
1193 .num_modes = ARRAY_SIZE(max11608_mode_list),
1194 .default_mode = s0to7,
1195 .info = &max1238_info,
1196 .channels = max11602_channels,
1197 .num_channels = ARRAY_SIZE(max11602_channels),
1198 },
1199 [max11603] = {
1200 .bits = 8,
1201 .int_vref_mv = 2048,
1202 .mode_list = max11608_mode_list,
1203 .num_modes = ARRAY_SIZE(max11608_mode_list),
1204 .default_mode = s0to7,
1205 .info = &max1238_info,
1206 .channels = max11602_channels,
1207 .num_channels = ARRAY_SIZE(max11602_channels),
1208 },
1209 [max11604] = {
1210 .bits = 8,
1211 .int_vref_mv = 4098,
1212 .mode_list = max1238_mode_list,
1213 .num_modes = ARRAY_SIZE(max1238_mode_list),
1214 .default_mode = s0to11,
1215 .info = &max1238_info,
1216 .channels = max1238_channels,
1217 .num_channels = ARRAY_SIZE(max1238_channels),
1218 },
1219 [max11605] = {
1220 .bits = 8,
1221 .int_vref_mv = 2048,
1222 .mode_list = max1238_mode_list,
1223 .num_modes = ARRAY_SIZE(max1238_mode_list),
1224 .default_mode = s0to11,
1225 .info = &max1238_info,
1226 .channels = max1238_channels,
1227 .num_channels = ARRAY_SIZE(max1238_channels),
1228 },
1229 [max11606] = {
1230 .bits = 10,
1231 .int_vref_mv = 4096,
1232 .mode_list = max11607_mode_list,
1233 .num_modes = ARRAY_SIZE(max11607_mode_list),
1234 .default_mode = s0to3,
1235 .info = &max1238_info,
1236 .channels = max1136_channels,
1237 .num_channels = ARRAY_SIZE(max1136_channels),
1238 },
1239 [max11607] = {
1240 .bits = 10,
1241 .int_vref_mv = 2048,
1242 .mode_list = max11607_mode_list,
1243 .num_modes = ARRAY_SIZE(max11607_mode_list),
1244 .default_mode = s0to3,
1245 .info = &max1238_info,
1246 .channels = max1136_channels,
1247 .num_channels = ARRAY_SIZE(max1136_channels),
1248 },
1249 [max11608] = {
1250 .bits = 10,
1251 .int_vref_mv = 4096,
1252 .mode_list = max11608_mode_list,
1253 .num_modes = ARRAY_SIZE(max11608_mode_list),
1254 .default_mode = s0to7,
1255 .info = &max1238_info,
1256 .channels = max11608_channels,
1257 .num_channels = ARRAY_SIZE(max11608_channels),
1258 },
1259 [max11609] = {
1260 .bits = 10,
1261 .int_vref_mv = 2048,
1262 .mode_list = max11608_mode_list,
1263 .num_modes = ARRAY_SIZE(max11608_mode_list),
1264 .default_mode = s0to7,
1265 .info = &max1238_info,
1266 .channels = max11608_channels,
1267 .num_channels = ARRAY_SIZE(max11608_channels),
1268 },
1269 [max11610] = {
1270 .bits = 10,
1271 .int_vref_mv = 4098,
1272 .mode_list = max1238_mode_list,
1273 .num_modes = ARRAY_SIZE(max1238_mode_list),
1274 .default_mode = s0to11,
1275 .info = &max1238_info,
1276 .channels = max1238_channels,
1277 .num_channels = ARRAY_SIZE(max1238_channels),
1278 },
1279 [max11611] = {
1280 .bits = 10,
1281 .int_vref_mv = 2048,
1282 .mode_list = max1238_mode_list,
1283 .num_modes = ARRAY_SIZE(max1238_mode_list),
1284 .default_mode = s0to11,
1285 .info = &max1238_info,
1286 .channels = max1238_channels,
1287 .num_channels = ARRAY_SIZE(max1238_channels),
1288 },
1289 [max11612] = {
1290 .bits = 12,
1291 .int_vref_mv = 4096,
1292 .mode_list = max11607_mode_list,
1293 .num_modes = ARRAY_SIZE(max11607_mode_list),
1294 .default_mode = s0to3,
1295 .info = &max1238_info,
1296 .channels = max1363_channels,
1297 .num_channels = ARRAY_SIZE(max1363_channels),
1298 },
1299 [max11613] = {
1300 .bits = 12,
1301 .int_vref_mv = 2048,
1302 .mode_list = max11607_mode_list,
1303 .num_modes = ARRAY_SIZE(max11607_mode_list),
1304 .default_mode = s0to3,
1305 .info = &max1238_info,
1306 .channels = max1363_channels,
1307 .num_channels = ARRAY_SIZE(max1363_channels),
1308 },
1309 [max11614] = {
1310 .bits = 12,
1311 .int_vref_mv = 4096,
1312 .mode_list = max11608_mode_list,
1313 .num_modes = ARRAY_SIZE(max11608_mode_list),
1314 .default_mode = s0to7,
1315 .info = &max1238_info,
1316 .channels = max11614_channels,
1317 .num_channels = ARRAY_SIZE(max11614_channels),
1318 },
1319 [max11615] = {
1320 .bits = 12,
1321 .int_vref_mv = 2048,
1322 .mode_list = max11608_mode_list,
1323 .num_modes = ARRAY_SIZE(max11608_mode_list),
1324 .default_mode = s0to7,
1325 .info = &max1238_info,
1326 .channels = max11614_channels,
1327 .num_channels = ARRAY_SIZE(max11614_channels),
1328 },
1329 [max11616] = {
1330 .bits = 12,
1331 .int_vref_mv = 4098,
1332 .mode_list = max1238_mode_list,
1333 .num_modes = ARRAY_SIZE(max1238_mode_list),
1334 .default_mode = s0to11,
1335 .info = &max1238_info,
1336 .channels = max1238_channels,
1337 .num_channels = ARRAY_SIZE(max1238_channels),
1338 },
1339 [max11617] = {
1340 .bits = 12,
1341 .int_vref_mv = 2048,
1342 .mode_list = max1238_mode_list,
1343 .num_modes = ARRAY_SIZE(max1238_mode_list),
1344 .default_mode = s0to11,
1345 .info = &max1238_info,
1346 .channels = max1238_channels,
1347 .num_channels = ARRAY_SIZE(max1238_channels),
1348 },
1349 [max11644] = {
1350 .bits = 12,
1351 .int_vref_mv = 2048,
1352 .mode_list = max11644_mode_list,
1353 .num_modes = ARRAY_SIZE(max11644_mode_list),
1354 .default_mode = s0to1,
1355 .info = &max1238_info,
1356 .channels = max11644_channels,
1357 .num_channels = ARRAY_SIZE(max11644_channels),
1358 },
1359 [max11645] = {
1360 .bits = 12,
1361 .int_vref_mv = 4096,
1362 .mode_list = max11644_mode_list,
1363 .num_modes = ARRAY_SIZE(max11644_mode_list),
1364 .default_mode = s0to1,
1365 .info = &max1238_info,
1366 .channels = max11644_channels,
1367 .num_channels = ARRAY_SIZE(max11644_channels),
1368 },
1369 [max11646] = {
1370 .bits = 10,
1371 .int_vref_mv = 2048,
1372 .mode_list = max11644_mode_list,
1373 .num_modes = ARRAY_SIZE(max11644_mode_list),
1374 .default_mode = s0to1,
1375 .info = &max1238_info,
1376 .channels = max11646_channels,
1377 .num_channels = ARRAY_SIZE(max11646_channels),
1378 },
1379 [max11647] = {
1380 .bits = 10,
1381 .int_vref_mv = 4096,
1382 .mode_list = max11644_mode_list,
1383 .num_modes = ARRAY_SIZE(max11644_mode_list),
1384 .default_mode = s0to1,
1385 .info = &max1238_info,
1386 .channels = max11646_channels,
1387 .num_channels = ARRAY_SIZE(max11646_channels),
1388 },
1389};
1390
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001391static int max1363_initial_setup(struct max1363_state *st)
1392{
1393 st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD
1394 | MAX1363_SETUP_POWER_UP_INT_REF
1395 | MAX1363_SETUP_INT_CLOCK
1396 | MAX1363_SETUP_UNIPOLAR
1397 | MAX1363_SETUP_NORESET;
1398
Peter Meerwald71d2c122012-12-30 23:26:00 +00001399 /* Set scan mode writes the config anyway so wait until then */
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001400 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1401 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1402 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
1403
1404 return max1363_set_scan_mode(st);
1405}
1406
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001407static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001408{
1409 struct max1363_state *st = iio_priv(indio_dev);
1410 unsigned long *masks;
1411 int i;
1412
1413 masks = kzalloc(BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*sizeof(long)*
1414 (st->chip_info->num_modes + 1), GFP_KERNEL);
1415 if (!masks)
1416 return -ENOMEM;
1417
1418 for (i = 0; i < st->chip_info->num_modes; i++)
1419 bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
1420 max1363_mode_table[st->chip_info->mode_list[i]]
1421 .modemask, MAX1363_MAX_CHANNELS);
1422
1423 indio_dev->available_scan_masks = masks;
1424
1425 return 0;
1426}
1427
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001428static irqreturn_t max1363_trigger_handler(int irq, void *p)
1429{
1430 struct iio_poll_func *pf = p;
1431 struct iio_dev *indio_dev = pf->indio_dev;
1432 struct max1363_state *st = iio_priv(indio_dev);
1433 s64 time_ns;
1434 __u8 *rxbuf;
1435 int b_sent;
1436 size_t d_size;
1437 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
1438 MAX1363_MAX_CHANNELS);
1439
1440 /* Ensure the timestamp is 8 byte aligned */
1441 if (st->chip_info->bits != 8)
1442 d_size = numvals*2;
1443 else
1444 d_size = numvals;
1445 if (indio_dev->scan_timestamp) {
1446 d_size += sizeof(s64);
1447 if (d_size % sizeof(s64))
1448 d_size += sizeof(s64) - (d_size % sizeof(s64));
1449 }
1450 /* Monitor mode prevents reading. Whilst not currently implemented
1451 * might as well have this test in here in the meantime as it does
1452 * no harm.
1453 */
1454 if (numvals == 0)
1455 goto done;
1456
1457 rxbuf = kmalloc(d_size, GFP_KERNEL);
1458 if (rxbuf == NULL)
1459 goto done;
1460 if (st->chip_info->bits != 8)
1461 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
1462 else
1463 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
1464 if (b_sent < 0)
1465 goto done_free;
1466
1467 time_ns = iio_get_time_ns();
1468
1469 if (indio_dev->scan_timestamp)
1470 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
1471 iio_push_to_buffers(indio_dev, rxbuf);
1472
1473done_free:
1474 kfree(rxbuf);
1475done:
1476 iio_trigger_notify_done(indio_dev->trig);
1477
1478 return IRQ_HANDLED;
1479}
1480
Jonathan Cameronec443722012-10-06 14:19:01 +01001481static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001482 .postenable = &iio_triggered_buffer_postenable,
1483 .preenable = &iio_sw_buffer_preenable,
1484 .predisable = &iio_triggered_buffer_predisable,
1485};
1486
Jonathan Cameronec443722012-10-06 14:19:01 +01001487static int max1363_register_buffered_funcs_and_init(struct iio_dev *indio_dev)
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001488{
1489 struct max1363_state *st = iio_priv(indio_dev);
1490 int ret = 0;
1491
1492 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
1493 if (!indio_dev->buffer) {
1494 ret = -ENOMEM;
1495 goto error_ret;
1496 }
1497 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
1498 &max1363_trigger_handler,
1499 IRQF_ONESHOT,
1500 indio_dev,
1501 "%s_consumer%d",
1502 st->client->name,
1503 indio_dev->id);
1504 if (indio_dev->pollfunc == NULL) {
1505 ret = -ENOMEM;
1506 goto error_deallocate_sw_rb;
1507 }
Jonathan Cameronec443722012-10-06 14:19:01 +01001508 /* Buffer functions - here trigger setup related */
1509 indio_dev->setup_ops = &max1363_buffered_setup_ops;
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001510
Jonathan Cameronec443722012-10-06 14:19:01 +01001511 /* Flag that polled buffering is possible */
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001512 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
1513
1514 return 0;
1515
1516error_deallocate_sw_rb:
1517 iio_kfifo_free(indio_dev->buffer);
1518error_ret:
1519 return ret;
1520}
1521
Jonathan Cameronec443722012-10-06 14:19:01 +01001522static void max1363_buffer_cleanup(struct iio_dev *indio_dev)
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001523{
1524 /* ensure that the trigger has been detached */
1525 iio_dealloc_pollfunc(indio_dev->pollfunc);
1526 iio_kfifo_free(indio_dev->buffer);
1527}
1528
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001529static int max1363_probe(struct i2c_client *client,
1530 const struct i2c_device_id *id)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001531{
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001532 int ret;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001533 struct max1363_state *st;
1534 struct iio_dev *indio_dev;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001535
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001536 indio_dev = iio_device_alloc(sizeof(struct max1363_state));
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001537 if (indio_dev == NULL) {
1538 ret = -ENOMEM;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001539 goto error_out;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001540 }
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001541
Guenter Roeck315ce4b2013-01-31 21:43:00 +00001542 indio_dev->dev.of_node = client->dev.of_node;
Jonathan Cameron0b27d672012-04-10 21:11:07 +01001543 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
1544 if (ret < 0)
1545 goto error_free_device;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001546
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001547 st = iio_priv(indio_dev);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001548
1549 st->reg = regulator_get(&client->dev, "vcc");
1550 if (IS_ERR(st->reg)) {
1551 ret = PTR_ERR(st->reg);
1552 goto error_unregister_map;
1553 }
1554
1555 ret = regulator_enable(st->reg);
1556 if (ret)
1557 goto error_put_reg;
1558
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001559 /* this is only used for device removal purposes */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001560 i2c_set_clientdata(client, indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001561
Jonathan Cameron962a4cc2010-06-26 12:54:13 +01001562 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001563 st->client = client;
1564
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001565 ret = max1363_alloc_scan_masks(indio_dev);
1566 if (ret)
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001567 goto error_disable_reg;
Jonathan Cameron82020b02010-05-04 14:43:03 +01001568
Peter Meerwald71d2c122012-12-30 23:26:00 +00001569 /* Establish that the iio_dev is a child of the i2c device */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001570 indio_dev->dev.parent = &client->dev;
Jonathan Cameron845bd122011-05-18 14:41:44 +01001571 indio_dev->name = id->name;
Jonathan Cameron8e7d9672011-08-30 12:32:45 +01001572 indio_dev->channels = st->chip_info->channels;
1573 indio_dev->num_channels = st->chip_info->num_channels;
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001574 indio_dev->info = st->chip_info->info;
1575 indio_dev->modes = INDIO_DIRECT_MODE;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001576 ret = max1363_initial_setup(st);
Jonathan Cameron58f0a252011-05-18 14:42:38 +01001577 if (ret < 0)
Jonathan Cameron82020b02010-05-04 14:43:03 +01001578 goto error_free_available_scan_masks;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001579
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001580 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1581 &max1363_trigger_handler, &max1363_buffered_setup_ops);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001582 if (ret)
Jonathan Cameron82020b02010-05-04 14:43:03 +01001583 goto error_free_available_scan_masks;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001584
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001585 if (client->irq) {
Jonathan Cameronaaf370d2011-05-18 14:41:16 +01001586 ret = request_threaded_irq(st->client->irq,
1587 NULL,
1588 &max1363_event_handler,
1589 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1590 "max1363_event",
1591 indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +01001592
Jonathan Cameron440a5202010-06-26 12:54:23 +01001593 if (ret)
Jonathan Cameronec443722012-10-06 14:19:01 +01001594 goto error_uninit_buffer;
Jonathan Cameron440a5202010-06-26 12:54:23 +01001595 }
1596
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001597 ret = iio_device_register(indio_dev);
1598 if (ret < 0)
1599 goto error_free_irq;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001600
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001601 return 0;
1602error_free_irq:
Guenter Roeck678fb422012-12-24 06:24:00 +00001603 if (client->irq)
1604 free_irq(st->client->irq, indio_dev);
Jonathan Cameronec443722012-10-06 14:19:01 +01001605error_uninit_buffer:
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001606 iio_triggered_buffer_cleanup(indio_dev);
Jonathan Cameron82020b02010-05-04 14:43:03 +01001607error_free_available_scan_masks:
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001608 kfree(indio_dev->available_scan_masks);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001609error_disable_reg:
1610 regulator_disable(st->reg);
1611error_put_reg:
1612 regulator_put(st->reg);
Guenter Roeck678fb422012-12-24 06:24:00 +00001613error_unregister_map:
1614 iio_map_array_unregister(indio_dev, client->dev.platform_data);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001615error_free_device:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001616 iio_device_free(indio_dev);
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001617error_out:
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001618 return ret;
1619}
1620
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001621static int max1363_remove(struct i2c_client *client)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001622{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001623 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1624 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +01001625
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001626 iio_device_unregister(indio_dev);
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001627 if (client->irq)
Jonathan Cameronaaf370d2011-05-18 14:41:16 +01001628 free_irq(st->client->irq, indio_dev);
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001629 iio_triggered_buffer_cleanup(indio_dev);
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001630 kfree(indio_dev->available_scan_masks);
Guenter Roeck678fb422012-12-24 06:24:00 +00001631 regulator_disable(st->reg);
1632 regulator_put(st->reg);
Jonathan Cameron0b27d672012-04-10 21:11:07 +01001633 iio_map_array_unregister(indio_dev, client->dev.platform_data);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001634 iio_device_free(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001635
1636 return 0;
1637}
1638
1639static const struct i2c_device_id max1363_id[] = {
1640 { "max1361", max1361 },
1641 { "max1362", max1362 },
1642 { "max1363", max1363 },
1643 { "max1364", max1364 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001644 { "max1036", max1036 },
1645 { "max1037", max1037 },
1646 { "max1038", max1038 },
1647 { "max1039", max1039 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001648 { "max1136", max1136 },
1649 { "max1137", max1137 },
1650 { "max1138", max1138 },
1651 { "max1139", max1139 },
1652 { "max1236", max1236 },
1653 { "max1237", max1237 },
1654 { "max1238", max1238 },
1655 { "max1239", max1239 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001656 { "max11600", max11600 },
1657 { "max11601", max11601 },
1658 { "max11602", max11602 },
1659 { "max11603", max11603 },
1660 { "max11604", max11604 },
1661 { "max11605", max11605 },
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +01001662 { "max11606", max11606 },
1663 { "max11607", max11607 },
1664 { "max11608", max11608 },
1665 { "max11609", max11609 },
1666 { "max11610", max11610 },
1667 { "max11611", max11611 },
1668 { "max11612", max11612 },
1669 { "max11613", max11613 },
1670 { "max11614", max11614 },
1671 { "max11615", max11615 },
1672 { "max11616", max11616 },
1673 { "max11617", max11617 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001674 {}
1675};
1676
1677MODULE_DEVICE_TABLE(i2c, max1363_id);
1678
1679static struct i2c_driver max1363_driver = {
1680 .driver = {
1681 .name = "max1363",
1682 },
1683 .probe = max1363_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001684 .remove = max1363_remove,
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001685 .id_table = max1363_id,
1686};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +01001687module_i2c_driver(max1363_driver);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001688
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01001689MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001690MODULE_DESCRIPTION("Maxim 1363 ADC");
1691MODULE_LICENSE("GPL v2");