blob: 6c1cfb74bdfc394c0a04b6653d23669edfd1568b [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 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{
Guenter Roecka405b002013-02-03 00:59:00 +00001393 st->setupbyte = MAX1363_SETUP_INT_CLOCK
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001394 | MAX1363_SETUP_UNIPOLAR
1395 | MAX1363_SETUP_NORESET;
1396
Guenter Roecka405b002013-02-03 00:59:00 +00001397 if (st->vref)
1398 st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
1399 else
1400 st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
1401 | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
1402
Peter Meerwald71d2c122012-12-30 23:26:00 +00001403 /* Set scan mode writes the config anyway so wait until then */
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001404 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1405 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1406 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
1407
1408 return max1363_set_scan_mode(st);
1409}
1410
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001411static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001412{
1413 struct max1363_state *st = iio_priv(indio_dev);
1414 unsigned long *masks;
1415 int i;
1416
Guenter Roeck7c3e8672013-02-03 00:59:00 +00001417 masks = devm_kzalloc(&indio_dev->dev,
1418 BITS_TO_LONGS(MAX1363_MAX_CHANNELS) * sizeof(long) *
1419 (st->chip_info->num_modes + 1), GFP_KERNEL);
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001420 if (!masks)
1421 return -ENOMEM;
1422
1423 for (i = 0; i < st->chip_info->num_modes; i++)
1424 bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
1425 max1363_mode_table[st->chip_info->mode_list[i]]
1426 .modemask, MAX1363_MAX_CHANNELS);
1427
1428 indio_dev->available_scan_masks = masks;
1429
1430 return 0;
1431}
1432
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001433static irqreturn_t max1363_trigger_handler(int irq, void *p)
1434{
1435 struct iio_poll_func *pf = p;
1436 struct iio_dev *indio_dev = pf->indio_dev;
1437 struct max1363_state *st = iio_priv(indio_dev);
1438 s64 time_ns;
1439 __u8 *rxbuf;
1440 int b_sent;
1441 size_t d_size;
1442 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
1443 MAX1363_MAX_CHANNELS);
1444
1445 /* Ensure the timestamp is 8 byte aligned */
1446 if (st->chip_info->bits != 8)
1447 d_size = numvals*2;
1448 else
1449 d_size = numvals;
1450 if (indio_dev->scan_timestamp) {
1451 d_size += sizeof(s64);
1452 if (d_size % sizeof(s64))
1453 d_size += sizeof(s64) - (d_size % sizeof(s64));
1454 }
1455 /* Monitor mode prevents reading. Whilst not currently implemented
1456 * might as well have this test in here in the meantime as it does
1457 * no harm.
1458 */
1459 if (numvals == 0)
1460 goto done;
1461
1462 rxbuf = kmalloc(d_size, GFP_KERNEL);
1463 if (rxbuf == NULL)
1464 goto done;
1465 if (st->chip_info->bits != 8)
1466 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
1467 else
1468 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
1469 if (b_sent < 0)
1470 goto done_free;
1471
1472 time_ns = iio_get_time_ns();
1473
1474 if (indio_dev->scan_timestamp)
1475 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
1476 iio_push_to_buffers(indio_dev, rxbuf);
1477
1478done_free:
1479 kfree(rxbuf);
1480done:
1481 iio_trigger_notify_done(indio_dev->trig);
1482
1483 return IRQ_HANDLED;
1484}
1485
Jonathan Cameronec443722012-10-06 14:19:01 +01001486static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001487 .postenable = &iio_triggered_buffer_postenable,
1488 .preenable = &iio_sw_buffer_preenable,
1489 .predisable = &iio_triggered_buffer_predisable,
1490};
1491
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001492static int max1363_probe(struct i2c_client *client,
1493 const struct i2c_device_id *id)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001494{
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001495 int ret;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001496 struct max1363_state *st;
1497 struct iio_dev *indio_dev;
Guenter Roecka405b002013-02-03 00:59:00 +00001498 struct regulator *vref;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001499
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001500 indio_dev = iio_device_alloc(sizeof(struct max1363_state));
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001501 if (indio_dev == NULL) {
1502 ret = -ENOMEM;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001503 goto error_out;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001504 }
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)
1509 goto error_free_device;
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 +01001592error_free_device:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001593 iio_device_free(indio_dev);
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001594error_out:
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001595 return ret;
1596}
1597
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001598static int max1363_remove(struct i2c_client *client)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001599{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001600 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1601 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +01001602
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001603 iio_device_unregister(indio_dev);
Peter Meerwald4389fbe2012-12-30 23:26:00 +00001604 iio_triggered_buffer_cleanup(indio_dev);
Guenter Roecka405b002013-02-03 00:59:00 +00001605 if (st->vref)
1606 regulator_disable(st->vref);
Guenter Roeck678fb422012-12-24 06:24:00 +00001607 regulator_disable(st->reg);
Guenter Roeck6cb2afd2013-01-31 21:43:00 +00001608 iio_map_array_unregister(indio_dev);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001609 iio_device_free(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001610
1611 return 0;
1612}
1613
1614static const struct i2c_device_id max1363_id[] = {
1615 { "max1361", max1361 },
1616 { "max1362", max1362 },
1617 { "max1363", max1363 },
1618 { "max1364", max1364 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001619 { "max1036", max1036 },
1620 { "max1037", max1037 },
1621 { "max1038", max1038 },
1622 { "max1039", max1039 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001623 { "max1136", max1136 },
1624 { "max1137", max1137 },
1625 { "max1138", max1138 },
1626 { "max1139", max1139 },
1627 { "max1236", max1236 },
1628 { "max1237", max1237 },
1629 { "max1238", max1238 },
1630 { "max1239", max1239 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001631 { "max11600", max11600 },
1632 { "max11601", max11601 },
1633 { "max11602", max11602 },
1634 { "max11603", max11603 },
1635 { "max11604", max11604 },
1636 { "max11605", max11605 },
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +01001637 { "max11606", max11606 },
1638 { "max11607", max11607 },
1639 { "max11608", max11608 },
1640 { "max11609", max11609 },
1641 { "max11610", max11610 },
1642 { "max11611", max11611 },
1643 { "max11612", max11612 },
1644 { "max11613", max11613 },
1645 { "max11614", max11614 },
1646 { "max11615", max11615 },
1647 { "max11616", max11616 },
1648 { "max11617", max11617 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001649 {}
1650};
1651
1652MODULE_DEVICE_TABLE(i2c, max1363_id);
1653
1654static struct i2c_driver max1363_driver = {
1655 .driver = {
1656 .name = "max1363",
1657 },
1658 .probe = max1363_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -08001659 .remove = max1363_remove,
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001660 .id_table = max1363_id,
1661};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +01001662module_i2c_driver(max1363_driver);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001663
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01001664MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001665MODULE_DESCRIPTION("Maxim 1363 ADC");
1666MODULE_LICENSE("GPL v2");