blob: 4fb35d1d74943b72a77e9a5affe4f8f751382b22 [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
Guenter Roecka405b002013-02-03 00:59:00 +0000166 * @vref: Reference voltage regulator
167 * @vref_uv: Actual (external or internal) reference voltage
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100168 */
169struct max1363_state {
170 struct i2c_client *client;
171 u8 setupbyte;
172 u8 configbyte;
173 const struct max1363_chip_info *chip_info;
174 const struct max1363_mode *current_mode;
175 u32 requestedmask;
176 struct regulator *reg;
177
178 /* Using monitor modes and buffer at the same time is
179 currently not supported */
180 bool monitor_on;
181 unsigned int monitor_speed:3;
182 u8 mask_high;
183 u8 mask_low;
184 /* 4x unipolar first then the fours bipolar ones */
185 s16 thresh_high[8];
186 s16 thresh_low[8];
Guenter Roecka405b002013-02-03 00:59:00 +0000187 struct regulator *vref;
188 u32 vref_uv;
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100189};
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100190
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100191#define MAX1363_MODE_SINGLE(_num, _mask) { \
192 .conf = MAX1363_CHANNEL_SEL(_num) \
193 | MAX1363_CONFIG_SCAN_SINGLE_1 \
194 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100195 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100196 }
197
198#define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
199 .conf = MAX1363_CHANNEL_SEL(_num) \
200 | MAX1363_CONFIG_SCAN_TO_CS \
201 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100202 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100203 }
204
205/* note not available for max1363 hence naming */
206#define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
207 .conf = MAX1363_CHANNEL_SEL(_num) \
208 | MAX1236_SCAN_MID_TO_CHANNEL \
209 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100210 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100211}
212
213#define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
214 .conf = MAX1363_CHANNEL_SEL(_nump) \
215 | MAX1363_CONFIG_SCAN_SINGLE_1 \
216 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100217 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100218 }
219
220/* Can't think how to automate naming so specify for now */
221#define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
222 .conf = MAX1363_CHANNEL_SEL(_num) \
223 | MAX1363_CONFIG_SCAN_TO_CS \
224 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100225 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100226 }
227
228/* note only available for max1363 hence naming */
229#define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
230 .conf = MAX1363_CHANNEL_SEL(_num) \
231 | MAX1236_SCAN_MID_TO_CHANNEL \
232 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100233 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100234}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100235
Jonathan Cameron82020b02010-05-04 14:43:03 +0100236static const struct max1363_mode max1363_mode_table[] = {
237 /* All of the single channel options first */
238 MAX1363_MODE_SINGLE(0, 1 << 0),
239 MAX1363_MODE_SINGLE(1, 1 << 1),
240 MAX1363_MODE_SINGLE(2, 1 << 2),
241 MAX1363_MODE_SINGLE(3, 1 << 3),
242 MAX1363_MODE_SINGLE(4, 1 << 4),
243 MAX1363_MODE_SINGLE(5, 1 << 5),
244 MAX1363_MODE_SINGLE(6, 1 << 6),
245 MAX1363_MODE_SINGLE(7, 1 << 7),
246 MAX1363_MODE_SINGLE(8, 1 << 8),
247 MAX1363_MODE_SINGLE(9, 1 << 9),
248 MAX1363_MODE_SINGLE(10, 1 << 10),
249 MAX1363_MODE_SINGLE(11, 1 << 11),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100250
Jonathan Cameron82020b02010-05-04 14:43:03 +0100251 MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
252 MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
253 MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
254 MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
255 MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
256 MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
257 MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
258 MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
259 MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
260 MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
261 MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
262 MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100263
Jonathan Cameron82020b02010-05-04 14:43:03 +0100264 /* The multichannel scans next */
265 MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
266 MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
267 MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
268 MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
269 MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
270 MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
271 MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
272 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
273 MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
274 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
275 MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
276 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
277 MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
278 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
279 MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
280 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
281 MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100282
Jonathan Cameron82020b02010-05-04 14:43:03 +0100283 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
284 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
285 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
286 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
287 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
288 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
289 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
290 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
291 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
292 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
293 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
294 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
295 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
296 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100297};
298
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100299static const struct max1363_mode
Jonathan Cameronc52cfb62011-12-05 21:37:15 +0000300*max1363_match_mode(const unsigned long *mask,
Peter Meerwald71d2c122012-12-30 23:26:00 +0000301 const struct max1363_chip_info *ci)
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700302{
Jonathan Cameron82020b02010-05-04 14:43:03 +0100303 int i;
304 if (mask)
305 for (i = 0; i < ci->num_modes; i++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100306 if (bitmap_subset(mask,
307 max1363_mode_table[ci->mode_list[i]].
308 modemask,
309 MAX1363_MAX_CHANNELS))
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700310 return &max1363_mode_table[ci->mode_list[i]];
311 return NULL;
Mike Frysingerc608cb02010-05-23 03:10:30 -0400312}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100313
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100314static int max1363_write_basic_config(struct i2c_client *client,
315 unsigned char d1,
316 unsigned char d2)
317{
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100318 u8 tx_buf[2] = {d1, d2};
Jonathan Cameron82020b02010-05-04 14:43:03 +0100319
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100320 return i2c_master_send(client, tx_buf, 2);
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100321}
322
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100323static int max1363_set_scan_mode(struct max1363_state *st)
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100324{
325 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
326 | MAX1363_SCAN_MASK
327 | MAX1363_SE_DE_MASK);
328 st->configbyte |= st->current_mode->conf;
329
330 return max1363_write_basic_config(st->client,
331 st->setupbyte,
332 st->configbyte);
333}
334
Jonathan Cameron298cd972011-05-18 14:40:53 +0100335static int max1363_read_single_chan(struct iio_dev *indio_dev,
336 struct iio_chan_spec const *chan,
337 int *val,
338 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100339{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100340 int ret = 0;
341 s32 data;
Guenter Roeck482bb4e2013-01-31 21:43:00 +0000342 u8 rxbuf[2];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100343 struct max1363_state *st = iio_priv(indio_dev);
344 struct i2c_client *client = st->client;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100345
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100346 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100347 /*
348 * If monitor mode is enabled, the method for reading a single
349 * channel will have to be rather different and has not yet
350 * been implemented.
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000351 *
352 * Also, cannot read directly if buffered capture enabled.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100353 */
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000354 if (st->monitor_on || iio_buffer_enabled(indio_dev)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100355 ret = -EBUSY;
356 goto error_ret;
357 }
358
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000359 /* Check to see if current scan mode is correct */
360 if (st->current_mode != &max1363_mode_table[chan->address]) {
361 /* Update scan mode if needed */
362 st->current_mode = &max1363_mode_table[chan->address];
363 ret = max1363_set_scan_mode(st);
364 if (ret < 0)
365 goto error_ret;
366 }
367 if (st->chip_info->bits != 8) {
368 /* Get reading */
369 data = i2c_master_recv(client, rxbuf, 2);
Jonathan Cameron82020b02010-05-04 14:43:03 +0100370 if (data < 0) {
371 ret = data;
372 goto error_ret;
373 }
Guenter Roeck482bb4e2013-01-31 21:43:00 +0000374 data = (rxbuf[1] | rxbuf[0] << 8) &
375 ((1 << st->chip_info->bits) - 1);
Jonathan Cameron82020b02010-05-04 14:43:03 +0100376 } else {
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000377 /* Get reading */
378 data = i2c_master_recv(client, rxbuf, 1);
379 if (data < 0) {
380 ret = data;
381 goto error_ret;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100382 }
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000383 data = rxbuf[0];
Jonathan Cameron82020b02010-05-04 14:43:03 +0100384 }
Jonathan Cameron298cd972011-05-18 14:40:53 +0100385 *val = data;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100386error_ret:
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100387 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100388 return ret;
389
Jonathan Cameron82020b02010-05-04 14:43:03 +0100390}
391
Jonathan Cameron298cd972011-05-18 14:40:53 +0100392static int max1363_read_raw(struct iio_dev *indio_dev,
393 struct iio_chan_spec const *chan,
394 int *val,
395 int *val2,
396 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100397{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100398 struct max1363_state *st = iio_priv(indio_dev);
399 int ret;
Guenter Roecka405b002013-02-03 00:59:00 +0000400 unsigned long scale_uv;
401
Jonathan Cameron298cd972011-05-18 14:40:53 +0100402 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100403 case IIO_CHAN_INFO_RAW:
Jonathan Cameron298cd972011-05-18 14:40:53 +0100404 ret = max1363_read_single_chan(indio_dev, chan, val, m);
Jonathan Cameroncd038012011-08-12 16:55:29 +0100405 if (ret < 0)
Jonathan Cameron298cd972011-05-18 14:40:53 +0100406 return ret;
407 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100408 case IIO_CHAN_INFO_SCALE:
Guenter Roecka405b002013-02-03 00:59:00 +0000409 scale_uv = st->vref_uv >> st->chip_info->bits;
410 *val = scale_uv / 1000;
411 *val2 = (scale_uv % 1000) * 1000;
412 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100413 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 Cameronf6e52e52013-02-19 21:11:35 +0000430
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100431#define MAX1363_CHAN_U(num, addr, si, bits, evmask) \
432 { \
433 .type = IIO_VOLTAGE, \
434 .indexed = 1, \
435 .channel = num, \
436 .address = addr, \
Jonathan Cameronf6e52e52013-02-19 21:11:35 +0000437 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
438 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
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, \
Jonathan Cameronf6e52e52013-02-19 21:11:35 +0000459 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
460 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameronbc234e72011-12-05 21:56:01 +0000461 .datasheet_name = "AIN"#num"-AIN"#num2, \
Jonathan Cameron18cffbed2011-12-05 21:37:16 +0000462 .scan_type = { \
463 .sign = 's', \
464 .realbits = bits, \
465 .storagebits = (bits > 8) ? 16 : 8, \
466 .endianness = IIO_BE, \
467 }, \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100468 .scan_index = si, \
469 .event_mask = evmask, \
470 }
471
472#define MAX1363_4X_CHANS(bits, em) { \
473 MAX1363_CHAN_U(0, _s0, 0, bits, em), \
474 MAX1363_CHAN_U(1, _s1, 1, bits, em), \
475 MAX1363_CHAN_U(2, _s2, 2, bits, em), \
476 MAX1363_CHAN_U(3, _s3, 3, bits, em), \
477 MAX1363_CHAN_B(0, 1, d0m1, 4, bits, em), \
478 MAX1363_CHAN_B(2, 3, d2m3, 5, bits, em), \
479 MAX1363_CHAN_B(1, 0, d1m0, 6, bits, em), \
480 MAX1363_CHAN_B(3, 2, d3m2, 7, bits, em), \
481 IIO_CHAN_SOFT_TIMESTAMP(8) \
482 }
483
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100484static const struct iio_chan_spec max1036_channels[] = MAX1363_4X_CHANS(8, 0);
485static const struct iio_chan_spec max1136_channels[] = MAX1363_4X_CHANS(10, 0);
486static const struct iio_chan_spec max1236_channels[] = MAX1363_4X_CHANS(12, 0);
487static const struct iio_chan_spec max1361_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100488 MAX1363_4X_CHANS(10, MAX1363_EV_M);
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100489static const struct iio_chan_spec max1363_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100490 MAX1363_4X_CHANS(12, MAX1363_EV_M);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100491
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800492/* Applies to max1236, max1237 */
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100493static const enum max1363_modes max1236_mode_list[] = {
494 _s0, _s1, _s2, _s3,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100495 s0to1, s0to2, s0to3,
496 d0m1, d2m3, d1m0, d3m2,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100497 d0m1to2m3, d1m0to3m2,
498 s2to3,
499};
500
501/* Applies to max1238, max1239 */
502static const enum max1363_modes max1238_mode_list[] = {
503 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100504 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
505 s0to7, s0to8, s0to9, s0to10, s0to11,
506 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
507 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100508 d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
509 d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
510 s6to7, s6to8, s6to9, s6to10, s6to11,
Jonathan Cameron82020b02010-05-04 14:43:03 +0100511 d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100512};
513
Jonathan Cameron298cd972011-05-18 14:40:53 +0100514#define MAX1363_12X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100515 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
516 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
517 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
518 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
519 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
520 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
521 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
522 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
523 MAX1363_CHAN_U(8, _s8, 8, bits, 0), \
524 MAX1363_CHAN_U(9, _s9, 9, bits, 0), \
525 MAX1363_CHAN_U(10, _s10, 10, bits, 0), \
526 MAX1363_CHAN_U(11, _s11, 11, bits, 0), \
527 MAX1363_CHAN_B(0, 1, d0m1, 12, bits, 0), \
528 MAX1363_CHAN_B(2, 3, d2m3, 13, bits, 0), \
529 MAX1363_CHAN_B(4, 5, d4m5, 14, bits, 0), \
530 MAX1363_CHAN_B(6, 7, d6m7, 15, bits, 0), \
531 MAX1363_CHAN_B(8, 9, d8m9, 16, bits, 0), \
532 MAX1363_CHAN_B(10, 11, d10m11, 17, bits, 0), \
533 MAX1363_CHAN_B(1, 0, d1m0, 18, bits, 0), \
534 MAX1363_CHAN_B(3, 2, d3m2, 19, bits, 0), \
535 MAX1363_CHAN_B(5, 4, d5m4, 20, bits, 0), \
536 MAX1363_CHAN_B(7, 6, d7m6, 21, bits, 0), \
537 MAX1363_CHAN_B(9, 8, d9m8, 22, bits, 0), \
538 MAX1363_CHAN_B(11, 10, d11m10, 23, bits, 0), \
Jonathan Cameron298cd972011-05-18 14:40:53 +0100539 IIO_CHAN_SOFT_TIMESTAMP(24) \
540 }
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100541static const struct iio_chan_spec max1038_channels[] = MAX1363_12X_CHANS(8);
542static const struct iio_chan_spec max1138_channels[] = MAX1363_12X_CHANS(10);
543static const struct iio_chan_spec max1238_channels[] = MAX1363_12X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100544
545static const enum max1363_modes max11607_mode_list[] = {
546 _s0, _s1, _s2, _s3,
547 s0to1, s0to2, s0to3,
548 s2to3,
549 d0m1, d2m3, d1m0, d3m2,
550 d0m1to2m3, d1m0to3m2,
551};
552
553static const enum max1363_modes max11608_mode_list[] = {
554 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
555 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
556 s6to7,
557 d0m1, d2m3, d4m5, d6m7,
558 d1m0, d3m2, d5m4, d7m6,
559 d0m1to2m3, d0m1to4m5, d0m1to6m7,
560 d1m0to3m2, d1m0to5m4, d1m0to7m6,
561};
562
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100563#define MAX1363_8X_CHANS(bits) { \
564 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
565 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
566 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
567 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
568 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
569 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
570 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
571 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
572 MAX1363_CHAN_B(0, 1, d0m1, 8, bits, 0), \
573 MAX1363_CHAN_B(2, 3, d2m3, 9, bits, 0), \
574 MAX1363_CHAN_B(4, 5, d4m5, 10, bits, 0), \
575 MAX1363_CHAN_B(6, 7, d6m7, 11, bits, 0), \
576 MAX1363_CHAN_B(1, 0, d1m0, 12, bits, 0), \
577 MAX1363_CHAN_B(3, 2, d3m2, 13, bits, 0), \
578 MAX1363_CHAN_B(5, 4, d5m4, 14, bits, 0), \
579 MAX1363_CHAN_B(7, 6, d7m6, 15, bits, 0), \
580 IIO_CHAN_SOFT_TIMESTAMP(16) \
581}
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100582static const struct iio_chan_spec max11602_channels[] = MAX1363_8X_CHANS(8);
583static const struct iio_chan_spec max11608_channels[] = MAX1363_8X_CHANS(10);
584static const struct iio_chan_spec max11614_channels[] = MAX1363_8X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100585
Jonathan Cameron586d1522011-05-18 14:42:10 +0100586static const enum max1363_modes max11644_mode_list[] = {
587 _s0, _s1, s0to1, d0m1, d1m0,
588};
589
590#define MAX1363_2X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100591 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
592 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
593 MAX1363_CHAN_B(0, 1, d0m1, 2, bits, 0), \
594 MAX1363_CHAN_B(1, 0, d1m0, 3, bits, 0), \
Jonathan Cameron586d1522011-05-18 14:42:10 +0100595 IIO_CHAN_SOFT_TIMESTAMP(4) \
596 }
597
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100598static const struct iio_chan_spec max11646_channels[] = MAX1363_2X_CHANS(10);
599static const struct iio_chan_spec max11644_channels[] = MAX1363_2X_CHANS(12);
Jonathan Cameron586d1522011-05-18 14:42:10 +0100600
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100601enum { max1361,
602 max1362,
603 max1363,
604 max1364,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100605 max1036,
606 max1037,
607 max1038,
608 max1039,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100609 max1136,
610 max1137,
611 max1138,
612 max1139,
613 max1236,
614 max1237,
615 max1238,
616 max1239,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100617 max11600,
618 max11601,
619 max11602,
620 max11603,
621 max11604,
622 max11605,
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100623 max11606,
624 max11607,
625 max11608,
626 max11609,
627 max11610,
628 max11611,
629 max11612,
630 max11613,
631 max11614,
632 max11615,
633 max11616,
634 max11617,
Jonathan Cameron586d1522011-05-18 14:42:10 +0100635 max11644,
636 max11645,
637 max11646,
638 max11647
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100639};
640
Jonathan Cameron440a5202010-06-26 12:54:23 +0100641static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
642 8300, 4200, 2000, 1000 };
643
644static ssize_t max1363_monitor_show_freq(struct device *dev,
645 struct device_attribute *attr,
646 char *buf)
647{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200648 struct max1363_state *st = iio_priv(dev_to_iio_dev(dev));
Jonathan Cameron440a5202010-06-26 12:54:23 +0100649 return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
650}
651
652static ssize_t max1363_monitor_store_freq(struct device *dev,
653 struct device_attribute *attr,
654 const char *buf,
655 size_t len)
656{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200657 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100658 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100659 int i, ret;
660 unsigned long val;
661 bool found = false;
662
Jingoo Handdeb64f2013-06-01 08:10:00 +0100663 ret = kstrtoul(buf, 10, &val);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100664 if (ret)
665 return -EINVAL;
666 for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
667 if (val == max1363_monitor_speeds[i]) {
668 found = true;
669 break;
670 }
671 if (!found)
672 return -EINVAL;
673
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100674 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100675 st->monitor_speed = i;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100676 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100677
678 return 0;
679}
680
681static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
682 max1363_monitor_show_freq,
683 max1363_monitor_store_freq);
684
685static IIO_CONST_ATTR(sampling_frequency_available,
686 "133000 665000 33300 16600 8300 4200 2000 1000");
687
Jonathan Cameron298cd972011-05-18 14:40:53 +0100688static int max1363_read_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100689 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100690 int *val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100691{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100692 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100693 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100694 *val = st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron440a5202010-06-26 12:54:23 +0100695 else
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100696 *val = st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100697 return 0;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100698}
699
Jonathan Cameron298cd972011-05-18 14:40:53 +0100700static int max1363_write_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100701 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100702 int val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100703{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100704 struct max1363_state *st = iio_priv(indio_dev);
705 /* make it handle signed correctly as well */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100706 switch (st->chip_info->bits) {
707 case 10:
708 if (val > 0x3FF)
709 return -EINVAL;
710 break;
711 case 12:
712 if (val > 0xFFF)
713 return -EINVAL;
714 break;
715 }
716
Jonathan Cameron298cd972011-05-18 14:40:53 +0100717 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
718 case IIO_EV_DIR_FALLING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100719 st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100720 break;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100721 case IIO_EV_DIR_RISING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100722 st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100723 break;
724 }
725
Jonathan Cameron440a5202010-06-26 12:54:23 +0100726 return 0;
727}
728
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100729static const u64 max1363_event_codes[] = {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100730 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
731 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
732 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
733 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
734 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
735 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
736 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
737 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
738 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
739 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
740 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
741 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
742 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
743 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
744 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
745 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100746};
747
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100748static irqreturn_t max1363_event_handler(int irq, void *private)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100749{
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100750 struct iio_dev *indio_dev = private;
751 struct max1363_state *st = iio_priv(indio_dev);
752 s64 timestamp = iio_get_time_ns();
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100753 unsigned long mask, loc;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100754 u8 rx;
755 u8 tx[2] = { st->setupbyte,
756 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
757
758 i2c_master_recv(st->client, &rx, 1);
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100759 mask = rx;
760 for_each_set_bit(loc, &mask, 8)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100761 iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100762 i2c_master_send(st->client, tx, 2);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100763
764 return IRQ_HANDLED;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100765}
766
Jonathan Cameron298cd972011-05-18 14:40:53 +0100767static int max1363_read_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100768 u64 event_code)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100769{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100770 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100771 int val;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100772 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron0d9fa2c2012-10-06 14:38:11 +0100773
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100774 mutex_lock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100775 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
776 val = (1 << number) & st->mask_low;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100777 else
Jonathan Cameron298cd972011-05-18 14:40:53 +0100778 val = (1 << number) & st->mask_high;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100779 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100780
Jonathan Cameron298cd972011-05-18 14:40:53 +0100781 return val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100782}
783
784static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
785{
786 u8 *tx_buf;
787 int ret, i = 3, j;
788 unsigned long numelements;
789 int len;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100790 const long *modemask;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100791
792 if (!enabled) {
Jonathan Cameronec443722012-10-06 14:19:01 +0100793 /* transition to buffered capture is not currently supported */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100794 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
795 st->configbyte &= ~MAX1363_SCAN_MASK;
796 st->monitor_on = false;
797 return max1363_write_basic_config(st->client,
798 st->setupbyte,
799 st->configbyte);
800 }
801
802 /* Ensure we are in the relevant mode */
803 st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
804 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
805 | MAX1363_SCAN_MASK
806 | MAX1363_SE_DE_MASK);
807 st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
808 if ((st->mask_low | st->mask_high) & 0x0F) {
809 st->configbyte |= max1363_mode_table[s0to3].conf;
810 modemask = max1363_mode_table[s0to3].modemask;
811 } else if ((st->mask_low | st->mask_high) & 0x30) {
812 st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
813 modemask = max1363_mode_table[d0m1to2m3].modemask;
814 } else {
815 st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
816 modemask = max1363_mode_table[d1m0to3m2].modemask;
817 }
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100818 numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100819 len = 3 * numelements + 3;
820 tx_buf = kmalloc(len, GFP_KERNEL);
821 if (!tx_buf) {
822 ret = -ENOMEM;
823 goto error_ret;
824 }
825 tx_buf[0] = st->configbyte;
826 tx_buf[1] = st->setupbyte;
827 tx_buf[2] = (st->monitor_speed << 1);
828
829 /*
830 * So we need to do yet another bit of nefarious scan mode
831 * setup to match what we need.
832 */
833 for (j = 0; j < 8; j++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100834 if (test_bit(j, modemask)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100835 /* Establish the mode is in the scan */
836 if (st->mask_low & (1 << j)) {
837 tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
838 tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
839 } else if (j < 4) {
840 tx_buf[i] = 0;
841 tx_buf[i + 1] = 0;
842 } else {
843 tx_buf[i] = 0x80;
844 tx_buf[i + 1] = 0;
845 }
846 if (st->mask_high & (1 << j)) {
847 tx_buf[i + 1] |=
848 (st->thresh_high[j] >> 8) & 0x0F;
849 tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
850 } else if (j < 4) {
851 tx_buf[i + 1] |= 0x0F;
852 tx_buf[i + 2] = 0xFF;
853 } else {
854 tx_buf[i + 1] |= 0x07;
855 tx_buf[i + 2] = 0xFF;
856 }
857 i += 3;
858 }
859
860
861 ret = i2c_master_send(st->client, tx_buf, len);
862 if (ret < 0)
863 goto error_ret;
864 if (ret != len) {
865 ret = -EIO;
866 goto error_ret;
867 }
868
869 /*
870 * Now that we hopefully have sensible thresholds in place it is
871 * time to turn the interrupts on.
872 * It is unclear from the data sheet if this should be necessary
873 * (i.e. whether monitor mode setup is atomic) but it appears to
874 * be in practice.
875 */
876 tx_buf[0] = st->setupbyte;
877 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
878 ret = i2c_master_send(st->client, tx_buf, 2);
879 if (ret < 0)
880 goto error_ret;
881 if (ret != 2) {
882 ret = -EIO;
883 goto error_ret;
884 }
885 ret = 0;
886 st->monitor_on = true;
887error_ret:
888
889 kfree(tx_buf);
890
891 return ret;
892}
893
894/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300895 * To keep this manageable we always use one of 3 scan modes.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100896 * Scan 0...3, 0-1,2-3 and 1-0,3-2
897 */
Jonathan Cameron298cd972011-05-18 14:40:53 +0100898
Jonathan Cameron440a5202010-06-26 12:54:23 +0100899static inline int __max1363_check_event_mask(int thismask, int checkmask)
900{
901 int ret = 0;
902 /* Is it unipolar */
903 if (thismask < 4) {
904 if (checkmask & ~0x0F) {
905 ret = -EBUSY;
906 goto error_ret;
907 }
908 } else if (thismask < 6) {
909 if (checkmask & ~0x30) {
910 ret = -EBUSY;
911 goto error_ret;
912 }
913 } else if (checkmask & ~0xC0)
914 ret = -EBUSY;
915error_ret:
916 return ret;
917}
918
Jonathan Cameron298cd972011-05-18 14:40:53 +0100919static int max1363_write_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100920 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100921 int state)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100922{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100923 int ret = 0;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100924 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100925 u16 unifiedmask;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100926 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100927
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100928 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100929 unifiedmask = st->mask_low | st->mask_high;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100930 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING) {
931
932 if (state == 0)
933 st->mask_low &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100934 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100935 ret = __max1363_check_event_mask((1 << number),
936 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100937 if (ret)
938 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100939 st->mask_low |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100940 }
941 } else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100942 if (state == 0)
943 st->mask_high &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100944 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100945 ret = __max1363_check_event_mask((1 << number),
946 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100947 if (ret)
948 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100949 st->mask_high |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100950 }
951 }
Jonathan Cameron440a5202010-06-26 12:54:23 +0100952
953 max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
954error_ret:
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100955 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100956
Jonathan Cameron298cd972011-05-18 14:40:53 +0100957 return ret;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100958}
959
Jonathan Cameron440a5202010-06-26 12:54:23 +0100960/*
961 * As with scan_elements, only certain sets of these can
962 * be combined.
963 */
964static struct attribute *max1363_event_attributes[] = {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100965 &iio_dev_attr_sampling_frequency.dev_attr.attr,
966 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Jonathan Cameron440a5202010-06-26 12:54:23 +0100967 NULL,
968};
969
970static struct attribute_group max1363_event_attribute_group = {
971 .attrs = max1363_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100972 .name = "events",
Jonathan Cameron440a5202010-06-26 12:54:23 +0100973};
974
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100975static int max1363_update_scan_mode(struct iio_dev *indio_dev,
976 const unsigned long *scan_mask)
977{
978 struct max1363_state *st = iio_priv(indio_dev);
979
980 /*
981 * Need to figure out the current mode based upon the requested
982 * scan mask in iio_dev
983 */
984 st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
985 if (!st->current_mode)
986 return -EINVAL;
987 max1363_set_scan_mode(st);
988 return 0;
989}
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100990
991static const struct iio_info max1238_info = {
992 .read_raw = &max1363_read_raw,
993 .driver_module = THIS_MODULE,
Jonathan Camerona9ea1b12012-04-10 21:11:05 +0100994 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100995};
996
997static const struct iio_info max1363_info = {
998 .read_event_value = &max1363_read_thresh,
999 .write_event_value = &max1363_write_thresh,
1000 .read_event_config = &max1363_read_event_config,
1001 .write_event_config = &max1363_write_event_config,
1002 .read_raw = &max1363_read_raw,
Jonathan Cameronc52cfb62011-12-05 21:37:15 +00001003 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001004 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001005 .event_attrs = &max1363_event_attribute_group,
1006};
1007
1008/* max1363 and max1368 tested - rest from data sheet */
1009static const struct max1363_chip_info max1363_chip_info_tbl[] = {
1010 [max1361] = {
1011 .bits = 10,
1012 .int_vref_mv = 2048,
1013 .mode_list = max1363_mode_list,
1014 .num_modes = ARRAY_SIZE(max1363_mode_list),
1015 .default_mode = s0to3,
1016 .channels = max1361_channels,
1017 .num_channels = ARRAY_SIZE(max1361_channels),
1018 .info = &max1363_info,
1019 },
1020 [max1362] = {
1021 .bits = 10,
1022 .int_vref_mv = 4096,
1023 .mode_list = max1363_mode_list,
1024 .num_modes = ARRAY_SIZE(max1363_mode_list),
1025 .default_mode = s0to3,
1026 .channels = max1361_channels,
1027 .num_channels = ARRAY_SIZE(max1361_channels),
1028 .info = &max1363_info,
1029 },
1030 [max1363] = {
1031 .bits = 12,
1032 .int_vref_mv = 2048,
1033 .mode_list = max1363_mode_list,
1034 .num_modes = ARRAY_SIZE(max1363_mode_list),
1035 .default_mode = s0to3,
1036 .channels = max1363_channels,
1037 .num_channels = ARRAY_SIZE(max1363_channels),
1038 .info = &max1363_info,
1039 },
1040 [max1364] = {
1041 .bits = 12,
1042 .int_vref_mv = 4096,
1043 .mode_list = max1363_mode_list,
1044 .num_modes = ARRAY_SIZE(max1363_mode_list),
1045 .default_mode = s0to3,
1046 .channels = max1363_channels,
1047 .num_channels = ARRAY_SIZE(max1363_channels),
1048 .info = &max1363_info,
1049 },
1050 [max1036] = {
1051 .bits = 8,
1052 .int_vref_mv = 4096,
1053 .mode_list = max1236_mode_list,
1054 .num_modes = ARRAY_SIZE(max1236_mode_list),
1055 .default_mode = s0to3,
1056 .info = &max1238_info,
1057 .channels = max1036_channels,
1058 .num_channels = ARRAY_SIZE(max1036_channels),
1059 },
1060 [max1037] = {
1061 .bits = 8,
1062 .int_vref_mv = 2048,
1063 .mode_list = max1236_mode_list,
1064 .num_modes = ARRAY_SIZE(max1236_mode_list),
1065 .default_mode = s0to3,
1066 .info = &max1238_info,
1067 .channels = max1036_channels,
1068 .num_channels = ARRAY_SIZE(max1036_channels),
1069 },
1070 [max1038] = {
1071 .bits = 8,
1072 .int_vref_mv = 4096,
1073 .mode_list = max1238_mode_list,
1074 .num_modes = ARRAY_SIZE(max1238_mode_list),
1075 .default_mode = s0to11,
1076 .info = &max1238_info,
1077 .channels = max1038_channels,
1078 .num_channels = ARRAY_SIZE(max1038_channels),
1079 },
1080 [max1039] = {
1081 .bits = 8,
1082 .int_vref_mv = 2048,
1083 .mode_list = max1238_mode_list,
1084 .num_modes = ARRAY_SIZE(max1238_mode_list),
1085 .default_mode = s0to11,
1086 .info = &max1238_info,
1087 .channels = max1038_channels,
1088 .num_channels = ARRAY_SIZE(max1038_channels),
1089 },
1090 [max1136] = {
1091 .bits = 10,
1092 .int_vref_mv = 4096,
1093 .mode_list = max1236_mode_list,
1094 .num_modes = ARRAY_SIZE(max1236_mode_list),
1095 .default_mode = s0to3,
1096 .info = &max1238_info,
1097 .channels = max1136_channels,
1098 .num_channels = ARRAY_SIZE(max1136_channels),
1099 },
1100 [max1137] = {
1101 .bits = 10,
1102 .int_vref_mv = 2048,
1103 .mode_list = max1236_mode_list,
1104 .num_modes = ARRAY_SIZE(max1236_mode_list),
1105 .default_mode = s0to3,
1106 .info = &max1238_info,
1107 .channels = max1136_channels,
1108 .num_channels = ARRAY_SIZE(max1136_channels),
1109 },
1110 [max1138] = {
1111 .bits = 10,
1112 .int_vref_mv = 4096,
1113 .mode_list = max1238_mode_list,
1114 .num_modes = ARRAY_SIZE(max1238_mode_list),
1115 .default_mode = s0to11,
1116 .info = &max1238_info,
1117 .channels = max1138_channels,
1118 .num_channels = ARRAY_SIZE(max1138_channels),
1119 },
1120 [max1139] = {
1121 .bits = 10,
1122 .int_vref_mv = 2048,
1123 .mode_list = max1238_mode_list,
1124 .num_modes = ARRAY_SIZE(max1238_mode_list),
1125 .default_mode = s0to11,
1126 .info = &max1238_info,
1127 .channels = max1138_channels,
1128 .num_channels = ARRAY_SIZE(max1138_channels),
1129 },
1130 [max1236] = {
1131 .bits = 12,
1132 .int_vref_mv = 4096,
1133 .mode_list = max1236_mode_list,
1134 .num_modes = ARRAY_SIZE(max1236_mode_list),
1135 .default_mode = s0to3,
1136 .info = &max1238_info,
1137 .channels = max1236_channels,
1138 .num_channels = ARRAY_SIZE(max1236_channels),
1139 },
1140 [max1237] = {
1141 .bits = 12,
1142 .int_vref_mv = 2048,
1143 .mode_list = max1236_mode_list,
1144 .num_modes = ARRAY_SIZE(max1236_mode_list),
1145 .default_mode = s0to3,
1146 .info = &max1238_info,
1147 .channels = max1236_channels,
1148 .num_channels = ARRAY_SIZE(max1236_channels),
1149 },
1150 [max1238] = {
1151 .bits = 12,
1152 .int_vref_mv = 4096,
1153 .mode_list = max1238_mode_list,
1154 .num_modes = ARRAY_SIZE(max1238_mode_list),
1155 .default_mode = s0to11,
1156 .info = &max1238_info,
1157 .channels = max1238_channels,
1158 .num_channels = ARRAY_SIZE(max1238_channels),
1159 },
1160 [max1239] = {
1161 .bits = 12,
1162 .int_vref_mv = 2048,
1163 .mode_list = max1238_mode_list,
1164 .num_modes = ARRAY_SIZE(max1238_mode_list),
1165 .default_mode = s0to11,
1166 .info = &max1238_info,
1167 .channels = max1238_channels,
1168 .num_channels = ARRAY_SIZE(max1238_channels),
1169 },
1170 [max11600] = {
1171 .bits = 8,
1172 .int_vref_mv = 4096,
1173 .mode_list = max11607_mode_list,
1174 .num_modes = ARRAY_SIZE(max11607_mode_list),
1175 .default_mode = s0to3,
1176 .info = &max1238_info,
1177 .channels = max1036_channels,
1178 .num_channels = ARRAY_SIZE(max1036_channels),
1179 },
1180 [max11601] = {
1181 .bits = 8,
1182 .int_vref_mv = 2048,
1183 .mode_list = max11607_mode_list,
1184 .num_modes = ARRAY_SIZE(max11607_mode_list),
1185 .default_mode = s0to3,
1186 .info = &max1238_info,
1187 .channels = max1036_channels,
1188 .num_channels = ARRAY_SIZE(max1036_channels),
1189 },
1190 [max11602] = {
1191 .bits = 8,
1192 .int_vref_mv = 4096,
1193 .mode_list = max11608_mode_list,
1194 .num_modes = ARRAY_SIZE(max11608_mode_list),
1195 .default_mode = s0to7,
1196 .info = &max1238_info,
1197 .channels = max11602_channels,
1198 .num_channels = ARRAY_SIZE(max11602_channels),
1199 },
1200 [max11603] = {
1201 .bits = 8,
1202 .int_vref_mv = 2048,
1203 .mode_list = max11608_mode_list,
1204 .num_modes = ARRAY_SIZE(max11608_mode_list),
1205 .default_mode = s0to7,
1206 .info = &max1238_info,
1207 .channels = max11602_channels,
1208 .num_channels = ARRAY_SIZE(max11602_channels),
1209 },
1210 [max11604] = {
1211 .bits = 8,
1212 .int_vref_mv = 4098,
1213 .mode_list = max1238_mode_list,
1214 .num_modes = ARRAY_SIZE(max1238_mode_list),
1215 .default_mode = s0to11,
1216 .info = &max1238_info,
1217 .channels = max1238_channels,
1218 .num_channels = ARRAY_SIZE(max1238_channels),
1219 },
1220 [max11605] = {
1221 .bits = 8,
1222 .int_vref_mv = 2048,
1223 .mode_list = max1238_mode_list,
1224 .num_modes = ARRAY_SIZE(max1238_mode_list),
1225 .default_mode = s0to11,
1226 .info = &max1238_info,
1227 .channels = max1238_channels,
1228 .num_channels = ARRAY_SIZE(max1238_channels),
1229 },
1230 [max11606] = {
1231 .bits = 10,
1232 .int_vref_mv = 4096,
1233 .mode_list = max11607_mode_list,
1234 .num_modes = ARRAY_SIZE(max11607_mode_list),
1235 .default_mode = s0to3,
1236 .info = &max1238_info,
1237 .channels = max1136_channels,
1238 .num_channels = ARRAY_SIZE(max1136_channels),
1239 },
1240 [max11607] = {
1241 .bits = 10,
1242 .int_vref_mv = 2048,
1243 .mode_list = max11607_mode_list,
1244 .num_modes = ARRAY_SIZE(max11607_mode_list),
1245 .default_mode = s0to3,
1246 .info = &max1238_info,
1247 .channels = max1136_channels,
1248 .num_channels = ARRAY_SIZE(max1136_channels),
1249 },
1250 [max11608] = {
1251 .bits = 10,
1252 .int_vref_mv = 4096,
1253 .mode_list = max11608_mode_list,
1254 .num_modes = ARRAY_SIZE(max11608_mode_list),
1255 .default_mode = s0to7,
1256 .info = &max1238_info,
1257 .channels = max11608_channels,
1258 .num_channels = ARRAY_SIZE(max11608_channels),
1259 },
1260 [max11609] = {
1261 .bits = 10,
1262 .int_vref_mv = 2048,
1263 .mode_list = max11608_mode_list,
1264 .num_modes = ARRAY_SIZE(max11608_mode_list),
1265 .default_mode = s0to7,
1266 .info = &max1238_info,
1267 .channels = max11608_channels,
1268 .num_channels = ARRAY_SIZE(max11608_channels),
1269 },
1270 [max11610] = {
1271 .bits = 10,
1272 .int_vref_mv = 4098,
1273 .mode_list = max1238_mode_list,
1274 .num_modes = ARRAY_SIZE(max1238_mode_list),
1275 .default_mode = s0to11,
1276 .info = &max1238_info,
1277 .channels = max1238_channels,
1278 .num_channels = ARRAY_SIZE(max1238_channels),
1279 },
1280 [max11611] = {
1281 .bits = 10,
1282 .int_vref_mv = 2048,
1283 .mode_list = max1238_mode_list,
1284 .num_modes = ARRAY_SIZE(max1238_mode_list),
1285 .default_mode = s0to11,
1286 .info = &max1238_info,
1287 .channels = max1238_channels,
1288 .num_channels = ARRAY_SIZE(max1238_channels),
1289 },
1290 [max11612] = {
1291 .bits = 12,
1292 .int_vref_mv = 4096,
1293 .mode_list = max11607_mode_list,
1294 .num_modes = ARRAY_SIZE(max11607_mode_list),
1295 .default_mode = s0to3,
1296 .info = &max1238_info,
1297 .channels = max1363_channels,
1298 .num_channels = ARRAY_SIZE(max1363_channels),
1299 },
1300 [max11613] = {
1301 .bits = 12,
1302 .int_vref_mv = 2048,
1303 .mode_list = max11607_mode_list,
1304 .num_modes = ARRAY_SIZE(max11607_mode_list),
1305 .default_mode = s0to3,
1306 .info = &max1238_info,
1307 .channels = max1363_channels,
1308 .num_channels = ARRAY_SIZE(max1363_channels),
1309 },
1310 [max11614] = {
1311 .bits = 12,
1312 .int_vref_mv = 4096,
1313 .mode_list = max11608_mode_list,
1314 .num_modes = ARRAY_SIZE(max11608_mode_list),
1315 .default_mode = s0to7,
1316 .info = &max1238_info,
1317 .channels = max11614_channels,
1318 .num_channels = ARRAY_SIZE(max11614_channels),
1319 },
1320 [max11615] = {
1321 .bits = 12,
1322 .int_vref_mv = 2048,
1323 .mode_list = max11608_mode_list,
1324 .num_modes = ARRAY_SIZE(max11608_mode_list),
1325 .default_mode = s0to7,
1326 .info = &max1238_info,
1327 .channels = max11614_channels,
1328 .num_channels = ARRAY_SIZE(max11614_channels),
1329 },
1330 [max11616] = {
1331 .bits = 12,
1332 .int_vref_mv = 4098,
1333 .mode_list = max1238_mode_list,
1334 .num_modes = ARRAY_SIZE(max1238_mode_list),
1335 .default_mode = s0to11,
1336 .info = &max1238_info,
1337 .channels = max1238_channels,
1338 .num_channels = ARRAY_SIZE(max1238_channels),
1339 },
1340 [max11617] = {
1341 .bits = 12,
1342 .int_vref_mv = 2048,
1343 .mode_list = max1238_mode_list,
1344 .num_modes = ARRAY_SIZE(max1238_mode_list),
1345 .default_mode = s0to11,
1346 .info = &max1238_info,
1347 .channels = max1238_channels,
1348 .num_channels = ARRAY_SIZE(max1238_channels),
1349 },
1350 [max11644] = {
1351 .bits = 12,
1352 .int_vref_mv = 2048,
1353 .mode_list = max11644_mode_list,
1354 .num_modes = ARRAY_SIZE(max11644_mode_list),
1355 .default_mode = s0to1,
1356 .info = &max1238_info,
1357 .channels = max11644_channels,
1358 .num_channels = ARRAY_SIZE(max11644_channels),
1359 },
1360 [max11645] = {
1361 .bits = 12,
1362 .int_vref_mv = 4096,
1363 .mode_list = max11644_mode_list,
1364 .num_modes = ARRAY_SIZE(max11644_mode_list),
1365 .default_mode = s0to1,
1366 .info = &max1238_info,
1367 .channels = max11644_channels,
1368 .num_channels = ARRAY_SIZE(max11644_channels),
1369 },
1370 [max11646] = {
1371 .bits = 10,
1372 .int_vref_mv = 2048,
1373 .mode_list = max11644_mode_list,
1374 .num_modes = ARRAY_SIZE(max11644_mode_list),
1375 .default_mode = s0to1,
1376 .info = &max1238_info,
1377 .channels = max11646_channels,
1378 .num_channels = ARRAY_SIZE(max11646_channels),
1379 },
1380 [max11647] = {
1381 .bits = 10,
1382 .int_vref_mv = 4096,
1383 .mode_list = max11644_mode_list,
1384 .num_modes = ARRAY_SIZE(max11644_mode_list),
1385 .default_mode = s0to1,
1386 .info = &max1238_info,
1387 .channels = max11646_channels,
1388 .num_channels = ARRAY_SIZE(max11646_channels),
1389 },
1390};
1391
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001392static int max1363_initial_setup(struct max1363_state *st)
1393{
Guenter Roecka405b002013-02-03 00:59:00 +00001394 st->setupbyte = MAX1363_SETUP_INT_CLOCK
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001395 | MAX1363_SETUP_UNIPOLAR
1396 | MAX1363_SETUP_NORESET;
1397
Guenter Roecka405b002013-02-03 00:59:00 +00001398 if (st->vref)
1399 st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
1400 else
1401 st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
1402 | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
1403
Peter Meerwald71d2c122012-12-30 23:26:00 +00001404 /* Set scan mode writes the config anyway so wait until then */
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001405 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1406 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1407 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
1408
1409 return max1363_set_scan_mode(st);
1410}
1411
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001412static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001413{
1414 struct max1363_state *st = iio_priv(indio_dev);
1415 unsigned long *masks;
1416 int i;
1417
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001418 masks = devm_kzalloc(&indio_dev->dev,
1419 BITS_TO_LONGS(MAX1363_MAX_CHANNELS) * sizeof(long) *
1420 (st->chip_info->num_modes + 1), GFP_KERNEL);
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001421 if (!masks)
1422 return -ENOMEM;
1423
1424 for (i = 0; i < st->chip_info->num_modes; i++)
1425 bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
1426 max1363_mode_table[st->chip_info->mode_list[i]]
1427 .modemask, MAX1363_MAX_CHANNELS);
1428
1429 indio_dev->available_scan_masks = masks;
1430
1431 return 0;
1432}
1433
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001434static irqreturn_t max1363_trigger_handler(int irq, void *p)
1435{
1436 struct iio_poll_func *pf = p;
1437 struct iio_dev *indio_dev = pf->indio_dev;
1438 struct max1363_state *st = iio_priv(indio_dev);
1439 s64 time_ns;
1440 __u8 *rxbuf;
1441 int b_sent;
1442 size_t d_size;
1443 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
1444 MAX1363_MAX_CHANNELS);
1445
1446 /* Ensure the timestamp is 8 byte aligned */
1447 if (st->chip_info->bits != 8)
1448 d_size = numvals*2;
1449 else
1450 d_size = numvals;
1451 if (indio_dev->scan_timestamp) {
1452 d_size += sizeof(s64);
1453 if (d_size % sizeof(s64))
1454 d_size += sizeof(s64) - (d_size % sizeof(s64));
1455 }
1456 /* Monitor mode prevents reading. Whilst not currently implemented
1457 * might as well have this test in here in the meantime as it does
1458 * no harm.
1459 */
1460 if (numvals == 0)
1461 goto done;
1462
1463 rxbuf = kmalloc(d_size, GFP_KERNEL);
1464 if (rxbuf == NULL)
1465 goto done;
1466 if (st->chip_info->bits != 8)
1467 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
1468 else
1469 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
1470 if (b_sent < 0)
1471 goto done_free;
1472
1473 time_ns = iio_get_time_ns();
1474
1475 if (indio_dev->scan_timestamp)
1476 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
1477 iio_push_to_buffers(indio_dev, rxbuf);
1478
1479done_free:
1480 kfree(rxbuf);
1481done:
1482 iio_trigger_notify_done(indio_dev->trig);
1483
1484 return IRQ_HANDLED;
1485}
1486
Jonathan Cameronec443722012-10-06 14:19:01 +01001487static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001488 .postenable = &iio_triggered_buffer_postenable,
1489 .preenable = &iio_sw_buffer_preenable,
1490 .predisable = &iio_triggered_buffer_predisable,
1491};
1492
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001493static int max1363_probe(struct i2c_client *client,
1494 const struct i2c_device_id *id)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001495{
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001496 int ret;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001497 struct max1363_state *st;
1498 struct iio_dev *indio_dev;
Guenter Roecka405b002013-02-03 00:59:00 +00001499 struct regulator *vref;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001500
Sachin Kamat6917e1d2013-07-22 12:02:00 +01001501 indio_dev = devm_iio_device_alloc(&client->dev,
1502 sizeof(struct max1363_state));
1503 if (!indio_dev)
1504 return -ENOMEM;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001505
Guenter Roeck315ce4b2013-01-31 21:43:00 +00001506 indio_dev->dev.of_node = client->dev.of_node;
Jonathan Cameron0b27d672012-04-10 21:11:07 +01001507 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
1508 if (ret < 0)
Sachin Kamat6917e1d2013-07-22 12:02:00 +01001509 return ret;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001510
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001511 st = iio_priv(indio_dev);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001512
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001513 st->reg = devm_regulator_get(&client->dev, "vcc");
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001514 if (IS_ERR(st->reg)) {
1515 ret = PTR_ERR(st->reg);
1516 goto error_unregister_map;
1517 }
1518
1519 ret = regulator_enable(st->reg);
1520 if (ret)
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001521 goto error_unregister_map;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001522
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001523 /* this is only used for device removal purposes */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001524 i2c_set_clientdata(client, indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001525
Jonathan Cameron962a4cc2010-06-26 12:54:13 +01001526 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001527 st->client = client;
1528
Guenter Roecka405b002013-02-03 00:59:00 +00001529 st->vref_uv = st->chip_info->int_vref_mv * 1000;
1530 vref = devm_regulator_get(&client->dev, "vref");
1531 if (!IS_ERR(vref)) {
1532 int vref_uv;
1533
1534 ret = regulator_enable(vref);
1535 if (ret)
1536 goto error_disable_reg;
1537 st->vref = vref;
1538 vref_uv = regulator_get_voltage(vref);
1539 if (vref_uv <= 0) {
1540 ret = -EINVAL;
1541 goto error_disable_reg;
1542 }
1543 st->vref_uv = vref_uv;
1544 }
1545
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001546 ret = max1363_alloc_scan_masks(indio_dev);
1547 if (ret)
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001548 goto error_disable_reg;
Jonathan Cameron82020b02010-05-04 14:43:03 +01001549
Peter Meerwald71d2c122012-12-30 23:26:00 +00001550 /* Establish that the iio_dev is a child of the i2c device */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001551 indio_dev->dev.parent = &client->dev;
Jonathan Cameron845bd122011-05-18 14:41:44 +01001552 indio_dev->name = id->name;
Jonathan Cameron8e7d9672011-08-30 12:32:45 +01001553 indio_dev->channels = st->chip_info->channels;
1554 indio_dev->num_channels = st->chip_info->num_channels;
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001555 indio_dev->info = st->chip_info->info;
1556 indio_dev->modes = INDIO_DIRECT_MODE;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001557 ret = max1363_initial_setup(st);
Jonathan Cameron58f0a252011-05-18 14:42:38 +01001558 if (ret < 0)
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001559 goto error_disable_reg;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001560
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001561 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1562 &max1363_trigger_handler, &max1363_buffered_setup_ops);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001563 if (ret)
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001564 goto error_disable_reg;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001565
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001566 if (client->irq) {
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001567 ret = devm_request_threaded_irq(&client->dev, st->client->irq,
Jonathan Cameronaaf370d2011-05-18 14:41:16 +01001568 NULL,
1569 &max1363_event_handler,
1570 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1571 "max1363_event",
1572 indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +01001573
Jonathan Cameron440a5202010-06-26 12:54:23 +01001574 if (ret)
Jonathan Cameronec443722012-10-06 14:19:01 +01001575 goto error_uninit_buffer;
Jonathan Cameron440a5202010-06-26 12:54:23 +01001576 }
1577
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001578 ret = iio_device_register(indio_dev);
1579 if (ret < 0)
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001580 goto error_uninit_buffer;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001581
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001582 return 0;
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001583
Jonathan Cameronec443722012-10-06 14:19:01 +01001584error_uninit_buffer:
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001585 iio_triggered_buffer_cleanup(indio_dev);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001586error_disable_reg:
Guenter Roecka405b002013-02-03 00:59:00 +00001587 if (st->vref)
1588 regulator_disable(st->vref);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001589 regulator_disable(st->reg);
Guenter Roeck678fb422012-12-24 06:24:00 +00001590error_unregister_map:
Guenter Roeck6cb2afd2013-01-31 21:43:00 +00001591 iio_map_array_unregister(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001592 return ret;
1593}
1594
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001595static int max1363_remove(struct i2c_client *client)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001596{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001597 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1598 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +01001599
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001600 iio_device_unregister(indio_dev);
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001601 iio_triggered_buffer_cleanup(indio_dev);
Guenter Roecka405b002013-02-03 00:59:00 +00001602 if (st->vref)
1603 regulator_disable(st->vref);
Guenter Roeck678fb422012-12-24 06:24:00 +00001604 regulator_disable(st->reg);
Guenter Roeck6cb2afd2013-01-31 21:43:00 +00001605 iio_map_array_unregister(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001606
1607 return 0;
1608}
1609
1610static const struct i2c_device_id max1363_id[] = {
1611 { "max1361", max1361 },
1612 { "max1362", max1362 },
1613 { "max1363", max1363 },
1614 { "max1364", max1364 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001615 { "max1036", max1036 },
1616 { "max1037", max1037 },
1617 { "max1038", max1038 },
1618 { "max1039", max1039 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001619 { "max1136", max1136 },
1620 { "max1137", max1137 },
1621 { "max1138", max1138 },
1622 { "max1139", max1139 },
1623 { "max1236", max1236 },
1624 { "max1237", max1237 },
1625 { "max1238", max1238 },
1626 { "max1239", max1239 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001627 { "max11600", max11600 },
1628 { "max11601", max11601 },
1629 { "max11602", max11602 },
1630 { "max11603", max11603 },
1631 { "max11604", max11604 },
1632 { "max11605", max11605 },
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +01001633 { "max11606", max11606 },
1634 { "max11607", max11607 },
1635 { "max11608", max11608 },
1636 { "max11609", max11609 },
1637 { "max11610", max11610 },
1638 { "max11611", max11611 },
1639 { "max11612", max11612 },
1640 { "max11613", max11613 },
1641 { "max11614", max11614 },
1642 { "max11615", max11615 },
1643 { "max11616", max11616 },
1644 { "max11617", max11617 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001645 {}
1646};
1647
1648MODULE_DEVICE_TABLE(i2c, max1363_id);
1649
1650static struct i2c_driver max1363_driver = {
1651 .driver = {
1652 .name = "max1363",
1653 },
1654 .probe = max1363_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001655 .remove = max1363_remove,
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001656 .id_table = max1363_id,
1657};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +01001658module_i2c_driver(max1363_driver);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001659
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01001660MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001661MODULE_DESCRIPTION("Maxim 1363 ADC");
1662MODULE_LICENSE("GPL v2");