blob: 31f3485303d062217d1839d0a1fddb4097671b80 [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>
Jonathan Cameroncdf38702011-08-12 17:08:43 +010042
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +010043#define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
44
45/* There is a fair bit more defined here than currently
46 * used, but the intention is to support everything these
47 * chips do in the long run */
48
49/* see data sheets */
50/* max1363 and max1236, max1237, max1238, max1239 */
51#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 0x00
52#define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF 0x20
53#define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT 0x40
54#define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT 0x60
55#define MAX1363_SETUP_POWER_UP_INT_REF 0x10
56#define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
57
58/* think about includeing max11600 etc - more settings */
59#define MAX1363_SETUP_EXT_CLOCK 0x08
60#define MAX1363_SETUP_INT_CLOCK 0x00
61#define MAX1363_SETUP_UNIPOLAR 0x00
62#define MAX1363_SETUP_BIPOLAR 0x04
63#define MAX1363_SETUP_RESET 0x00
64#define MAX1363_SETUP_NORESET 0x02
65/* max1363 only - though don't care on others.
66 * For now monitor modes are not implemented as the relevant
67 * line is not connected on my test board.
68 * The definitions are here as I intend to add this soon.
69 */
70#define MAX1363_SETUP_MONITOR_SETUP 0x01
71
72/* Specific to the max1363 */
73#define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
74#define MAX1363_MON_INT_ENABLE 0x01
75
76/* defined for readability reasons */
77/* All chips */
78#define MAX1363_CONFIG_BYTE(a) ((a))
79
80#define MAX1363_CONFIG_SE 0x01
81#define MAX1363_CONFIG_DE 0x00
82#define MAX1363_CONFIG_SCAN_TO_CS 0x00
83#define MAX1363_CONFIG_SCAN_SINGLE_8 0x20
84#define MAX1363_CONFIG_SCAN_MONITOR_MODE 0x40
85#define MAX1363_CONFIG_SCAN_SINGLE_1 0x60
86/* max123{6-9} only */
87#define MAX1236_SCAN_MID_TO_CHANNEL 0x40
88
89/* max1363 only - merely part of channel selects or don't care for others*/
90#define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
91
92#define MAX1363_CHANNEL_SEL(a) ((a) << 1)
93
94/* max1363 strictly 0x06 - but doesn't matter */
95#define MAX1363_CHANNEL_SEL_MASK 0x1E
96#define MAX1363_SCAN_MASK 0x60
97#define MAX1363_SE_DE_MASK 0x01
98
99#define MAX1363_MAX_CHANNELS 25
100/**
101 * struct max1363_mode - scan mode information
102 * @conf: The corresponding value of the configuration register
103 * @modemask: Bit mask corresponding to channels enabled in this mode
104 */
105struct max1363_mode {
106 int8_t conf;
107 DECLARE_BITMAP(modemask, MAX1363_MAX_CHANNELS);
108};
109
110/* This must be maintained along side the max1363_mode_table in max1363_core */
111enum max1363_modes {
112 /* Single read of a single channel */
113 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
114 /* Differential single read */
115 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
116 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
117 /* Scan to channel and mid to channel where overlapping */
118 s0to1, s0to2, s2to3, s0to3, s0to4, s0to5, s0to6,
119 s6to7, s0to7, s6to8, s0to8, s6to9,
120 s0to9, s6to10, s0to10, s6to11, s0to11,
121 /* Differential scan to channel and mid to channel where overlapping */
122 d0m1to2m3, d0m1to4m5, d0m1to6m7, d6m7to8m9,
123 d0m1to8m9, d6m7to10m11, d0m1to10m11, d1m0to3m2,
124 d1m0to5m4, d1m0to7m6, d7m6to9m8, d1m0to9m8,
125 d7m6to11m10, d1m0to11m10,
126};
127
128/**
129 * struct max1363_chip_info - chip specifc information
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100130 * @info: iio core function callbacks structure
Jonathan Cameron2593b132012-10-06 14:37:44 +0100131 * @channels: channel specification
132 * @num_channels: number of channels
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100133 * @mode_list: array of available scan modes
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100134 * @default_mode: the scan mode in which the chip starts up
Jonathan Cameron2593b132012-10-06 14:37:44 +0100135 * @int_vref_mv: the internal reference voltage
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100136 * @num_channels: number of channels
Jonathan Cameron2593b132012-10-06 14:37:44 +0100137 * @bits: accuracy of the adc in bits
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100138 */
139struct max1363_chip_info {
140 const struct iio_info *info;
Jonathan Cameron2593b132012-10-06 14:37:44 +0100141 const struct iio_chan_spec *channels;
142 int num_channels;
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100143 const enum max1363_modes *mode_list;
144 enum max1363_modes default_mode;
145 u16 int_vref_mv;
146 u8 num_modes;
147 u8 bits;
148};
149
150/**
151 * struct max1363_state - driver instance specific data
152 * @client: i2c_client
153 * @setupbyte: cache of current device setup byte
154 * @configbyte: cache of current device config byte
155 * @chip_info: chip model specific constants, available modes etc
156 * @current_mode: the scan mode of this chip
157 * @requestedmask: a valid requested set of channels
158 * @reg: supply regulator
159 * @monitor_on: whether monitor mode is enabled
160 * @monitor_speed: parameter corresponding to device monitor speed setting
161 * @mask_high: bitmask for enabled high thresholds
162 * @mask_low: bitmask for enabled low thresholds
163 * @thresh_high: high threshold values
164 * @thresh_low: low threshold values
165 */
166struct max1363_state {
167 struct i2c_client *client;
168 u8 setupbyte;
169 u8 configbyte;
170 const struct max1363_chip_info *chip_info;
171 const struct max1363_mode *current_mode;
172 u32 requestedmask;
173 struct regulator *reg;
174
175 /* Using monitor modes and buffer at the same time is
176 currently not supported */
177 bool monitor_on;
178 unsigned int monitor_speed:3;
179 u8 mask_high;
180 u8 mask_low;
181 /* 4x unipolar first then the fours bipolar ones */
182 s16 thresh_high[8];
183 s16 thresh_low[8];
184};
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100185
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100186#define MAX1363_MODE_SINGLE(_num, _mask) { \
187 .conf = MAX1363_CHANNEL_SEL(_num) \
188 | MAX1363_CONFIG_SCAN_SINGLE_1 \
189 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100190 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100191 }
192
193#define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
194 .conf = MAX1363_CHANNEL_SEL(_num) \
195 | MAX1363_CONFIG_SCAN_TO_CS \
196 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100197 .modemask[0] = _mask, \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100198 }
199
200/* note not available for max1363 hence naming */
201#define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
202 .conf = MAX1363_CHANNEL_SEL(_num) \
203 | MAX1236_SCAN_MID_TO_CHANNEL \
204 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100205 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100206}
207
208#define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
209 .conf = MAX1363_CHANNEL_SEL(_nump) \
210 | MAX1363_CONFIG_SCAN_SINGLE_1 \
211 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100212 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100213 }
214
215/* Can't think how to automate naming so specify for now */
216#define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
217 .conf = MAX1363_CHANNEL_SEL(_num) \
218 | MAX1363_CONFIG_SCAN_TO_CS \
219 | MAX1363_CONFIG_DE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100220 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100221 }
222
223/* note only available for max1363 hence naming */
224#define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
225 .conf = MAX1363_CHANNEL_SEL(_num) \
226 | MAX1236_SCAN_MID_TO_CHANNEL \
227 | MAX1363_CONFIG_SE, \
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100228 .modemask[0] = _mask \
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100229}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100230
Jonathan Cameron82020b02010-05-04 14:43:03 +0100231static const struct max1363_mode max1363_mode_table[] = {
232 /* All of the single channel options first */
233 MAX1363_MODE_SINGLE(0, 1 << 0),
234 MAX1363_MODE_SINGLE(1, 1 << 1),
235 MAX1363_MODE_SINGLE(2, 1 << 2),
236 MAX1363_MODE_SINGLE(3, 1 << 3),
237 MAX1363_MODE_SINGLE(4, 1 << 4),
238 MAX1363_MODE_SINGLE(5, 1 << 5),
239 MAX1363_MODE_SINGLE(6, 1 << 6),
240 MAX1363_MODE_SINGLE(7, 1 << 7),
241 MAX1363_MODE_SINGLE(8, 1 << 8),
242 MAX1363_MODE_SINGLE(9, 1 << 9),
243 MAX1363_MODE_SINGLE(10, 1 << 10),
244 MAX1363_MODE_SINGLE(11, 1 << 11),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100245
Jonathan Cameron82020b02010-05-04 14:43:03 +0100246 MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
247 MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
248 MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
249 MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
250 MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
251 MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
252 MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
253 MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
254 MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
255 MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
256 MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
257 MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100258
Jonathan Cameron82020b02010-05-04 14:43:03 +0100259 /* The multichannel scans next */
260 MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
261 MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
262 MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
263 MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
264 MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
265 MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
266 MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
267 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
268 MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
269 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
270 MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
271 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
272 MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
273 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
274 MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
275 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
276 MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100277
Jonathan Cameron82020b02010-05-04 14:43:03 +0100278 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
279 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
280 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
281 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
282 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
283 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
284 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
285 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
286 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
287 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
288 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
289 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
290 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
291 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100292};
293
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100294static const struct max1363_mode
Jonathan Cameronc52cfb62011-12-05 21:37:15 +0000295*max1363_match_mode(const unsigned long *mask,
296const struct max1363_chip_info *ci)
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700297{
Jonathan Cameron82020b02010-05-04 14:43:03 +0100298 int i;
299 if (mask)
300 for (i = 0; i < ci->num_modes; i++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100301 if (bitmap_subset(mask,
302 max1363_mode_table[ci->mode_list[i]].
303 modemask,
304 MAX1363_MAX_CHANNELS))
Greg Kroah-Hartman1b183e42010-05-04 22:24:05 -0700305 return &max1363_mode_table[ci->mode_list[i]];
306 return NULL;
Mike Frysingerc608cb02010-05-23 03:10:30 -0400307}
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100308
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100309static int max1363_write_basic_config(struct i2c_client *client,
310 unsigned char d1,
311 unsigned char d2)
312{
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100313 u8 tx_buf[2] = {d1, d2};
Jonathan Cameron82020b02010-05-04 14:43:03 +0100314
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100315 return i2c_master_send(client, tx_buf, 2);
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100316}
317
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100318static int max1363_set_scan_mode(struct max1363_state *st)
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100319{
320 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
321 | MAX1363_SCAN_MASK
322 | MAX1363_SE_DE_MASK);
323 st->configbyte |= st->current_mode->conf;
324
325 return max1363_write_basic_config(st->client,
326 st->setupbyte,
327 st->configbyte);
328}
329
Jonathan Cameron298cd972011-05-18 14:40:53 +0100330static int max1363_read_single_chan(struct iio_dev *indio_dev,
331 struct iio_chan_spec const *chan,
332 int *val,
333 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100334{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100335 int ret = 0;
336 s32 data;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100337 char rxbuf[2];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100338 struct max1363_state *st = iio_priv(indio_dev);
339 struct i2c_client *client = st->client;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100340
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100341 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100342 /*
343 * If monitor mode is enabled, the method for reading a single
344 * channel will have to be rather different and has not yet
345 * been implemented.
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000346 *
347 * Also, cannot read directly if buffered capture enabled.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100348 */
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000349 if (st->monitor_on || iio_buffer_enabled(indio_dev)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100350 ret = -EBUSY;
351 goto error_ret;
352 }
353
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000354 /* Check to see if current scan mode is correct */
355 if (st->current_mode != &max1363_mode_table[chan->address]) {
356 /* Update scan mode if needed */
357 st->current_mode = &max1363_mode_table[chan->address];
358 ret = max1363_set_scan_mode(st);
359 if (ret < 0)
360 goto error_ret;
361 }
362 if (st->chip_info->bits != 8) {
363 /* Get reading */
364 data = i2c_master_recv(client, rxbuf, 2);
Jonathan Cameron82020b02010-05-04 14:43:03 +0100365 if (data < 0) {
366 ret = data;
367 goto error_ret;
368 }
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000369 data = (s32)(rxbuf[1]) | ((s32)(rxbuf[0] & 0x0F)) << 8;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100370 } else {
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000371 /* Get reading */
372 data = i2c_master_recv(client, rxbuf, 1);
373 if (data < 0) {
374 ret = data;
375 goto error_ret;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100376 }
Jonathan Cameron3ca34cc2011-12-05 22:18:25 +0000377 data = rxbuf[0];
Jonathan Cameron82020b02010-05-04 14:43:03 +0100378 }
Jonathan Cameron298cd972011-05-18 14:40:53 +0100379 *val = data;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100380error_ret:
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100381 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100382 return ret;
383
Jonathan Cameron82020b02010-05-04 14:43:03 +0100384}
385
Jonathan Cameron298cd972011-05-18 14:40:53 +0100386static int max1363_read_raw(struct iio_dev *indio_dev,
387 struct iio_chan_spec const *chan,
388 int *val,
389 int *val2,
390 long m)
Jonathan Cameron82020b02010-05-04 14:43:03 +0100391{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100392 struct max1363_state *st = iio_priv(indio_dev);
393 int ret;
394 switch (m) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100395 case IIO_CHAN_INFO_RAW:
Jonathan Cameron298cd972011-05-18 14:40:53 +0100396 ret = max1363_read_single_chan(indio_dev, chan, val, m);
Jonathan Cameroncd038012011-08-12 16:55:29 +0100397 if (ret < 0)
Jonathan Cameron298cd972011-05-18 14:40:53 +0100398 return ret;
399 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100400 case IIO_CHAN_INFO_SCALE:
Jonathan Cameron298cd972011-05-18 14:40:53 +0100401 if ((1 << (st->chip_info->bits + 1)) >
402 st->chip_info->int_vref_mv) {
403 *val = 0;
404 *val2 = 500000;
405 return IIO_VAL_INT_PLUS_MICRO;
406 } else {
407 *val = (st->chip_info->int_vref_mv)
408 >> st->chip_info->bits;
409 return IIO_VAL_INT;
410 }
411 default:
412 return -EINVAL;
413 }
414 return 0;
Jonathan Cameron82020b02010-05-04 14:43:03 +0100415}
416
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100417/* Applies to max1363 */
418static const enum max1363_modes max1363_mode_list[] = {
419 _s0, _s1, _s2, _s3,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100420 s0to1, s0to2, s0to3,
421 d0m1, d2m3, d1m0, d3m2,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100422 d0m1to2m3, d1m0to3m2,
423};
424
Jonathan Cameron298cd972011-05-18 14:40:53 +0100425#define MAX1363_EV_M \
426 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) \
427 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100428#define MAX1363_INFO_MASK (IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
429 IIO_CHAN_INFO_SCALE_SHARED_BIT)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100430#define MAX1363_CHAN_U(num, addr, si, bits, evmask) \
431 { \
432 .type = IIO_VOLTAGE, \
433 .indexed = 1, \
434 .channel = num, \
435 .address = addr, \
436 .info_mask = MAX1363_INFO_MASK, \
Jonathan Cameronbc234e72011-12-05 21:56:01 +0000437 .datasheet_name = "AIN"#num, \
Jonathan Cameron18cffbed2011-12-05 21:37:16 +0000438 .scan_type = { \
439 .sign = 'u', \
440 .realbits = bits, \
441 .storagebits = (bits > 8) ? 16 : 8, \
442 .endianness = IIO_BE, \
443 }, \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100444 .scan_index = si, \
445 .event_mask = evmask, \
Jonathan Cameron298cd972011-05-18 14:40:53 +0100446 }
447
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100448/* bipolar channel */
449#define MAX1363_CHAN_B(num, num2, addr, si, bits, evmask) \
450 { \
451 .type = IIO_VOLTAGE, \
452 .differential = 1, \
453 .indexed = 1, \
454 .channel = num, \
455 .channel2 = num2, \
456 .address = addr, \
457 .info_mask = MAX1363_INFO_MASK, \
Jonathan Cameronbc234e72011-12-05 21:56:01 +0000458 .datasheet_name = "AIN"#num"-AIN"#num2, \
Jonathan Cameron18cffbed2011-12-05 21:37:16 +0000459 .scan_type = { \
460 .sign = 's', \
461 .realbits = bits, \
462 .storagebits = (bits > 8) ? 16 : 8, \
463 .endianness = IIO_BE, \
464 }, \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100465 .scan_index = si, \
466 .event_mask = evmask, \
467 }
468
469#define MAX1363_4X_CHANS(bits, em) { \
470 MAX1363_CHAN_U(0, _s0, 0, bits, em), \
471 MAX1363_CHAN_U(1, _s1, 1, bits, em), \
472 MAX1363_CHAN_U(2, _s2, 2, bits, em), \
473 MAX1363_CHAN_U(3, _s3, 3, bits, em), \
474 MAX1363_CHAN_B(0, 1, d0m1, 4, bits, em), \
475 MAX1363_CHAN_B(2, 3, d2m3, 5, bits, em), \
476 MAX1363_CHAN_B(1, 0, d1m0, 6, bits, em), \
477 MAX1363_CHAN_B(3, 2, d3m2, 7, bits, em), \
478 IIO_CHAN_SOFT_TIMESTAMP(8) \
479 }
480
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100481static const struct iio_chan_spec max1036_channels[] = MAX1363_4X_CHANS(8, 0);
482static const struct iio_chan_spec max1136_channels[] = MAX1363_4X_CHANS(10, 0);
483static const struct iio_chan_spec max1236_channels[] = MAX1363_4X_CHANS(12, 0);
484static const struct iio_chan_spec max1361_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100485 MAX1363_4X_CHANS(10, MAX1363_EV_M);
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100486static const struct iio_chan_spec max1363_channels[] =
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100487 MAX1363_4X_CHANS(12, MAX1363_EV_M);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100488
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800489/* Applies to max1236, max1237 */
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100490static const enum max1363_modes max1236_mode_list[] = {
491 _s0, _s1, _s2, _s3,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100492 s0to1, s0to2, s0to3,
493 d0m1, d2m3, d1m0, d3m2,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100494 d0m1to2m3, d1m0to3m2,
495 s2to3,
496};
497
498/* Applies to max1238, max1239 */
499static const enum max1363_modes max1238_mode_list[] = {
500 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100501 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
502 s0to7, s0to8, s0to9, s0to10, s0to11,
503 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
504 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100505 d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
506 d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
507 s6to7, s6to8, s6to9, s6to10, s6to11,
Jonathan Cameron82020b02010-05-04 14:43:03 +0100508 d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100509};
510
Jonathan Cameron298cd972011-05-18 14:40:53 +0100511#define MAX1363_12X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100512 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
513 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
514 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
515 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
516 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
517 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
518 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
519 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
520 MAX1363_CHAN_U(8, _s8, 8, bits, 0), \
521 MAX1363_CHAN_U(9, _s9, 9, bits, 0), \
522 MAX1363_CHAN_U(10, _s10, 10, bits, 0), \
523 MAX1363_CHAN_U(11, _s11, 11, bits, 0), \
524 MAX1363_CHAN_B(0, 1, d0m1, 12, bits, 0), \
525 MAX1363_CHAN_B(2, 3, d2m3, 13, bits, 0), \
526 MAX1363_CHAN_B(4, 5, d4m5, 14, bits, 0), \
527 MAX1363_CHAN_B(6, 7, d6m7, 15, bits, 0), \
528 MAX1363_CHAN_B(8, 9, d8m9, 16, bits, 0), \
529 MAX1363_CHAN_B(10, 11, d10m11, 17, bits, 0), \
530 MAX1363_CHAN_B(1, 0, d1m0, 18, bits, 0), \
531 MAX1363_CHAN_B(3, 2, d3m2, 19, bits, 0), \
532 MAX1363_CHAN_B(5, 4, d5m4, 20, bits, 0), \
533 MAX1363_CHAN_B(7, 6, d7m6, 21, bits, 0), \
534 MAX1363_CHAN_B(9, 8, d9m8, 22, bits, 0), \
535 MAX1363_CHAN_B(11, 10, d11m10, 23, bits, 0), \
Jonathan Cameron298cd972011-05-18 14:40:53 +0100536 IIO_CHAN_SOFT_TIMESTAMP(24) \
537 }
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100538static const struct iio_chan_spec max1038_channels[] = MAX1363_12X_CHANS(8);
539static const struct iio_chan_spec max1138_channels[] = MAX1363_12X_CHANS(10);
540static const struct iio_chan_spec max1238_channels[] = MAX1363_12X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100541
542static const enum max1363_modes max11607_mode_list[] = {
543 _s0, _s1, _s2, _s3,
544 s0to1, s0to2, s0to3,
545 s2to3,
546 d0m1, d2m3, d1m0, d3m2,
547 d0m1to2m3, d1m0to3m2,
548};
549
550static const enum max1363_modes max11608_mode_list[] = {
551 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
552 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
553 s6to7,
554 d0m1, d2m3, d4m5, d6m7,
555 d1m0, d3m2, d5m4, d7m6,
556 d0m1to2m3, d0m1to4m5, d0m1to6m7,
557 d1m0to3m2, d1m0to5m4, d1m0to7m6,
558};
559
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100560#define MAX1363_8X_CHANS(bits) { \
561 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
562 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
563 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
564 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
565 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
566 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
567 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
568 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
569 MAX1363_CHAN_B(0, 1, d0m1, 8, bits, 0), \
570 MAX1363_CHAN_B(2, 3, d2m3, 9, bits, 0), \
571 MAX1363_CHAN_B(4, 5, d4m5, 10, bits, 0), \
572 MAX1363_CHAN_B(6, 7, d6m7, 11, bits, 0), \
573 MAX1363_CHAN_B(1, 0, d1m0, 12, bits, 0), \
574 MAX1363_CHAN_B(3, 2, d3m2, 13, bits, 0), \
575 MAX1363_CHAN_B(5, 4, d5m4, 14, bits, 0), \
576 MAX1363_CHAN_B(7, 6, d7m6, 15, bits, 0), \
577 IIO_CHAN_SOFT_TIMESTAMP(16) \
578}
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100579static const struct iio_chan_spec max11602_channels[] = MAX1363_8X_CHANS(8);
580static const struct iio_chan_spec max11608_channels[] = MAX1363_8X_CHANS(10);
581static const struct iio_chan_spec max11614_channels[] = MAX1363_8X_CHANS(12);
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100582
Jonathan Cameron586d1522011-05-18 14:42:10 +0100583static const enum max1363_modes max11644_mode_list[] = {
584 _s0, _s1, s0to1, d0m1, d1m0,
585};
586
587#define MAX1363_2X_CHANS(bits) { \
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100588 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
589 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
590 MAX1363_CHAN_B(0, 1, d0m1, 2, bits, 0), \
591 MAX1363_CHAN_B(1, 0, d1m0, 3, bits, 0), \
Jonathan Cameron586d1522011-05-18 14:42:10 +0100592 IIO_CHAN_SOFT_TIMESTAMP(4) \
593 }
594
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100595static const struct iio_chan_spec max11646_channels[] = MAX1363_2X_CHANS(10);
596static const struct iio_chan_spec max11644_channels[] = MAX1363_2X_CHANS(12);
Jonathan Cameron586d1522011-05-18 14:42:10 +0100597
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100598enum { max1361,
599 max1362,
600 max1363,
601 max1364,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100602 max1036,
603 max1037,
604 max1038,
605 max1039,
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100606 max1136,
607 max1137,
608 max1138,
609 max1139,
610 max1236,
611 max1237,
612 max1238,
613 max1239,
Jonathan Cameron3bf877c2010-05-04 14:43:10 +0100614 max11600,
615 max11601,
616 max11602,
617 max11603,
618 max11604,
619 max11605,
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +0100620 max11606,
621 max11607,
622 max11608,
623 max11609,
624 max11610,
625 max11611,
626 max11612,
627 max11613,
628 max11614,
629 max11615,
630 max11616,
631 max11617,
Jonathan Cameron586d1522011-05-18 14:42:10 +0100632 max11644,
633 max11645,
634 max11646,
635 max11647
Jonathan Camerond1325cf2009-08-18 18:06:20 +0100636};
637
Jonathan Cameron440a5202010-06-26 12:54:23 +0100638static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
639 8300, 4200, 2000, 1000 };
640
641static ssize_t max1363_monitor_show_freq(struct device *dev,
642 struct device_attribute *attr,
643 char *buf)
644{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200645 struct max1363_state *st = iio_priv(dev_to_iio_dev(dev));
Jonathan Cameron440a5202010-06-26 12:54:23 +0100646 return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
647}
648
649static ssize_t max1363_monitor_store_freq(struct device *dev,
650 struct device_attribute *attr,
651 const char *buf,
652 size_t len)
653{
Lars-Peter Clausen62c51832012-05-12 15:39:42 +0200654 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100655 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100656 int i, ret;
657 unsigned long val;
658 bool found = false;
659
660 ret = strict_strtoul(buf, 10, &val);
661 if (ret)
662 return -EINVAL;
663 for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
664 if (val == max1363_monitor_speeds[i]) {
665 found = true;
666 break;
667 }
668 if (!found)
669 return -EINVAL;
670
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100671 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100672 st->monitor_speed = i;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100673 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100674
675 return 0;
676}
677
678static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
679 max1363_monitor_show_freq,
680 max1363_monitor_store_freq);
681
682static IIO_CONST_ATTR(sampling_frequency_available,
683 "133000 665000 33300 16600 8300 4200 2000 1000");
684
Jonathan Cameron298cd972011-05-18 14:40:53 +0100685static int max1363_read_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100686 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100687 int *val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100688{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100689 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100690 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100691 *val = st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron440a5202010-06-26 12:54:23 +0100692 else
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100693 *val = st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)];
Jonathan Cameron298cd972011-05-18 14:40:53 +0100694 return 0;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100695}
696
Jonathan Cameron298cd972011-05-18 14:40:53 +0100697static int max1363_write_thresh(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100698 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100699 int val)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100700{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100701 struct max1363_state *st = iio_priv(indio_dev);
702 /* make it handle signed correctly as well */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100703 switch (st->chip_info->bits) {
704 case 10:
705 if (val > 0x3FF)
706 return -EINVAL;
707 break;
708 case 12:
709 if (val > 0xFFF)
710 return -EINVAL;
711 break;
712 }
713
Jonathan Cameron298cd972011-05-18 14:40:53 +0100714 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
715 case IIO_EV_DIR_FALLING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100716 st->thresh_low[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100717 break;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100718 case IIO_EV_DIR_RISING:
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100719 st->thresh_high[IIO_EVENT_CODE_EXTRACT_CHAN(event_code)] = val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100720 break;
721 }
722
Jonathan Cameron440a5202010-06-26 12:54:23 +0100723 return 0;
724}
725
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100726static const u64 max1363_event_codes[] = {
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100727 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
728 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
729 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
730 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
731 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
732 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
733 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
734 IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
735 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
736 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
737 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
738 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
739 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
740 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
741 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
742 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100743};
744
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100745static irqreturn_t max1363_event_handler(int irq, void *private)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100746{
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100747 struct iio_dev *indio_dev = private;
748 struct max1363_state *st = iio_priv(indio_dev);
749 s64 timestamp = iio_get_time_ns();
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100750 unsigned long mask, loc;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100751 u8 rx;
752 u8 tx[2] = { st->setupbyte,
753 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
754
755 i2c_master_recv(st->client, &rx, 1);
Jonathan Cameron58f0a252011-05-18 14:42:38 +0100756 mask = rx;
757 for_each_set_bit(loc, &mask, 8)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100758 iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100759 i2c_master_send(st->client, tx, 2);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100760
761 return IRQ_HANDLED;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100762}
763
Jonathan Cameron298cd972011-05-18 14:40:53 +0100764static int max1363_read_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100765 u64 event_code)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100766{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100767 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100768 int val;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100769 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron0d9fa2c2012-10-06 14:38:11 +0100770
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100771 mutex_lock(&indio_dev->mlock);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100772 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING)
773 val = (1 << number) & st->mask_low;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100774 else
Jonathan Cameron298cd972011-05-18 14:40:53 +0100775 val = (1 << number) & st->mask_high;
Jonathan Cameronb6b70682011-04-15 18:55:58 +0100776 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100777
Jonathan Cameron298cd972011-05-18 14:40:53 +0100778 return val;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100779}
780
781static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
782{
783 u8 *tx_buf;
784 int ret, i = 3, j;
785 unsigned long numelements;
786 int len;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100787 const long *modemask;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100788
789 if (!enabled) {
Jonathan Cameronec443722012-10-06 14:19:01 +0100790 /* transition to buffered capture is not currently supported */
Jonathan Cameron440a5202010-06-26 12:54:23 +0100791 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
792 st->configbyte &= ~MAX1363_SCAN_MASK;
793 st->monitor_on = false;
794 return max1363_write_basic_config(st->client,
795 st->setupbyte,
796 st->configbyte);
797 }
798
799 /* Ensure we are in the relevant mode */
800 st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
801 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
802 | MAX1363_SCAN_MASK
803 | MAX1363_SE_DE_MASK);
804 st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
805 if ((st->mask_low | st->mask_high) & 0x0F) {
806 st->configbyte |= max1363_mode_table[s0to3].conf;
807 modemask = max1363_mode_table[s0to3].modemask;
808 } else if ((st->mask_low | st->mask_high) & 0x30) {
809 st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
810 modemask = max1363_mode_table[d0m1to2m3].modemask;
811 } else {
812 st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
813 modemask = max1363_mode_table[d1m0to3m2].modemask;
814 }
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100815 numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100816 len = 3 * numelements + 3;
817 tx_buf = kmalloc(len, GFP_KERNEL);
818 if (!tx_buf) {
819 ret = -ENOMEM;
820 goto error_ret;
821 }
822 tx_buf[0] = st->configbyte;
823 tx_buf[1] = st->setupbyte;
824 tx_buf[2] = (st->monitor_speed << 1);
825
826 /*
827 * So we need to do yet another bit of nefarious scan mode
828 * setup to match what we need.
829 */
830 for (j = 0; j < 8; j++)
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100831 if (test_bit(j, modemask)) {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100832 /* Establish the mode is in the scan */
833 if (st->mask_low & (1 << j)) {
834 tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
835 tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
836 } else if (j < 4) {
837 tx_buf[i] = 0;
838 tx_buf[i + 1] = 0;
839 } else {
840 tx_buf[i] = 0x80;
841 tx_buf[i + 1] = 0;
842 }
843 if (st->mask_high & (1 << j)) {
844 tx_buf[i + 1] |=
845 (st->thresh_high[j] >> 8) & 0x0F;
846 tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
847 } else if (j < 4) {
848 tx_buf[i + 1] |= 0x0F;
849 tx_buf[i + 2] = 0xFF;
850 } else {
851 tx_buf[i + 1] |= 0x07;
852 tx_buf[i + 2] = 0xFF;
853 }
854 i += 3;
855 }
856
857
858 ret = i2c_master_send(st->client, tx_buf, len);
859 if (ret < 0)
860 goto error_ret;
861 if (ret != len) {
862 ret = -EIO;
863 goto error_ret;
864 }
865
866 /*
867 * Now that we hopefully have sensible thresholds in place it is
868 * time to turn the interrupts on.
869 * It is unclear from the data sheet if this should be necessary
870 * (i.e. whether monitor mode setup is atomic) but it appears to
871 * be in practice.
872 */
873 tx_buf[0] = st->setupbyte;
874 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
875 ret = i2c_master_send(st->client, tx_buf, 2);
876 if (ret < 0)
877 goto error_ret;
878 if (ret != 2) {
879 ret = -EIO;
880 goto error_ret;
881 }
882 ret = 0;
883 st->monitor_on = true;
884error_ret:
885
886 kfree(tx_buf);
887
888 return ret;
889}
890
891/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300892 * To keep this manageable we always use one of 3 scan modes.
Jonathan Cameron440a5202010-06-26 12:54:23 +0100893 * Scan 0...3, 0-1,2-3 and 1-0,3-2
894 */
Jonathan Cameron298cd972011-05-18 14:40:53 +0100895
Jonathan Cameron440a5202010-06-26 12:54:23 +0100896static inline int __max1363_check_event_mask(int thismask, int checkmask)
897{
898 int ret = 0;
899 /* Is it unipolar */
900 if (thismask < 4) {
901 if (checkmask & ~0x0F) {
902 ret = -EBUSY;
903 goto error_ret;
904 }
905 } else if (thismask < 6) {
906 if (checkmask & ~0x30) {
907 ret = -EBUSY;
908 goto error_ret;
909 }
910 } else if (checkmask & ~0xC0)
911 ret = -EBUSY;
912error_ret:
913 return ret;
914}
915
Jonathan Cameron298cd972011-05-18 14:40:53 +0100916static int max1363_write_event_config(struct iio_dev *indio_dev,
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100917 u64 event_code,
Jonathan Cameron298cd972011-05-18 14:40:53 +0100918 int state)
Jonathan Cameron440a5202010-06-26 12:54:23 +0100919{
Jonathan Cameron298cd972011-05-18 14:40:53 +0100920 int ret = 0;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100921 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100922 u16 unifiedmask;
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100923 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
Jonathan Cameron298cd972011-05-18 14:40:53 +0100924
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100925 mutex_lock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100926 unifiedmask = st->mask_low | st->mask_high;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100927 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_FALLING) {
928
929 if (state == 0)
930 st->mask_low &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100931 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100932 ret = __max1363_check_event_mask((1 << number),
933 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100934 if (ret)
935 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100936 st->mask_low |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100937 }
938 } else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100939 if (state == 0)
940 st->mask_high &= ~(1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100941 else {
Jonathan Cameron298cd972011-05-18 14:40:53 +0100942 ret = __max1363_check_event_mask((1 << number),
943 unifiedmask);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100944 if (ret)
945 goto error_ret;
Jonathan Cameron298cd972011-05-18 14:40:53 +0100946 st->mask_high |= (1 << number);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100947 }
948 }
Jonathan Cameron440a5202010-06-26 12:54:23 +0100949
950 max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
951error_ret:
Jonathan Cameron3dba81b2011-04-15 18:55:59 +0100952 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron440a5202010-06-26 12:54:23 +0100953
Jonathan Cameron298cd972011-05-18 14:40:53 +0100954 return ret;
Jonathan Cameron440a5202010-06-26 12:54:23 +0100955}
956
Jonathan Cameron440a5202010-06-26 12:54:23 +0100957/*
958 * As with scan_elements, only certain sets of these can
959 * be combined.
960 */
961static struct attribute *max1363_event_attributes[] = {
Jonathan Cameron440a5202010-06-26 12:54:23 +0100962 &iio_dev_attr_sampling_frequency.dev_attr.attr,
963 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
Jonathan Cameron440a5202010-06-26 12:54:23 +0100964 NULL,
965};
966
967static struct attribute_group max1363_event_attribute_group = {
968 .attrs = max1363_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100969 .name = "events",
Jonathan Cameron440a5202010-06-26 12:54:23 +0100970};
971
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +0100972static int max1363_update_scan_mode(struct iio_dev *indio_dev,
973 const unsigned long *scan_mask)
974{
975 struct max1363_state *st = iio_priv(indio_dev);
976
977 /*
978 * Need to figure out the current mode based upon the requested
979 * scan mask in iio_dev
980 */
981 st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
982 if (!st->current_mode)
983 return -EINVAL;
984 max1363_set_scan_mode(st);
985 return 0;
986}
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100987
988static const struct iio_info max1238_info = {
989 .read_raw = &max1363_read_raw,
990 .driver_module = THIS_MODULE,
Jonathan Camerona9ea1b12012-04-10 21:11:05 +0100991 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100992};
993
994static const struct iio_info max1363_info = {
995 .read_event_value = &max1363_read_thresh,
996 .write_event_value = &max1363_write_thresh,
997 .read_event_config = &max1363_read_event_config,
998 .write_event_config = &max1363_write_event_config,
999 .read_raw = &max1363_read_raw,
Jonathan Cameronc52cfb62011-12-05 21:37:15 +00001000 .update_scan_mode = &max1363_update_scan_mode,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001001 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001002 .event_attrs = &max1363_event_attribute_group,
1003};
1004
1005/* max1363 and max1368 tested - rest from data sheet */
1006static const struct max1363_chip_info max1363_chip_info_tbl[] = {
1007 [max1361] = {
1008 .bits = 10,
1009 .int_vref_mv = 2048,
1010 .mode_list = max1363_mode_list,
1011 .num_modes = ARRAY_SIZE(max1363_mode_list),
1012 .default_mode = s0to3,
1013 .channels = max1361_channels,
1014 .num_channels = ARRAY_SIZE(max1361_channels),
1015 .info = &max1363_info,
1016 },
1017 [max1362] = {
1018 .bits = 10,
1019 .int_vref_mv = 4096,
1020 .mode_list = max1363_mode_list,
1021 .num_modes = ARRAY_SIZE(max1363_mode_list),
1022 .default_mode = s0to3,
1023 .channels = max1361_channels,
1024 .num_channels = ARRAY_SIZE(max1361_channels),
1025 .info = &max1363_info,
1026 },
1027 [max1363] = {
1028 .bits = 12,
1029 .int_vref_mv = 2048,
1030 .mode_list = max1363_mode_list,
1031 .num_modes = ARRAY_SIZE(max1363_mode_list),
1032 .default_mode = s0to3,
1033 .channels = max1363_channels,
1034 .num_channels = ARRAY_SIZE(max1363_channels),
1035 .info = &max1363_info,
1036 },
1037 [max1364] = {
1038 .bits = 12,
1039 .int_vref_mv = 4096,
1040 .mode_list = max1363_mode_list,
1041 .num_modes = ARRAY_SIZE(max1363_mode_list),
1042 .default_mode = s0to3,
1043 .channels = max1363_channels,
1044 .num_channels = ARRAY_SIZE(max1363_channels),
1045 .info = &max1363_info,
1046 },
1047 [max1036] = {
1048 .bits = 8,
1049 .int_vref_mv = 4096,
1050 .mode_list = max1236_mode_list,
1051 .num_modes = ARRAY_SIZE(max1236_mode_list),
1052 .default_mode = s0to3,
1053 .info = &max1238_info,
1054 .channels = max1036_channels,
1055 .num_channels = ARRAY_SIZE(max1036_channels),
1056 },
1057 [max1037] = {
1058 .bits = 8,
1059 .int_vref_mv = 2048,
1060 .mode_list = max1236_mode_list,
1061 .num_modes = ARRAY_SIZE(max1236_mode_list),
1062 .default_mode = s0to3,
1063 .info = &max1238_info,
1064 .channels = max1036_channels,
1065 .num_channels = ARRAY_SIZE(max1036_channels),
1066 },
1067 [max1038] = {
1068 .bits = 8,
1069 .int_vref_mv = 4096,
1070 .mode_list = max1238_mode_list,
1071 .num_modes = ARRAY_SIZE(max1238_mode_list),
1072 .default_mode = s0to11,
1073 .info = &max1238_info,
1074 .channels = max1038_channels,
1075 .num_channels = ARRAY_SIZE(max1038_channels),
1076 },
1077 [max1039] = {
1078 .bits = 8,
1079 .int_vref_mv = 2048,
1080 .mode_list = max1238_mode_list,
1081 .num_modes = ARRAY_SIZE(max1238_mode_list),
1082 .default_mode = s0to11,
1083 .info = &max1238_info,
1084 .channels = max1038_channels,
1085 .num_channels = ARRAY_SIZE(max1038_channels),
1086 },
1087 [max1136] = {
1088 .bits = 10,
1089 .int_vref_mv = 4096,
1090 .mode_list = max1236_mode_list,
1091 .num_modes = ARRAY_SIZE(max1236_mode_list),
1092 .default_mode = s0to3,
1093 .info = &max1238_info,
1094 .channels = max1136_channels,
1095 .num_channels = ARRAY_SIZE(max1136_channels),
1096 },
1097 [max1137] = {
1098 .bits = 10,
1099 .int_vref_mv = 2048,
1100 .mode_list = max1236_mode_list,
1101 .num_modes = ARRAY_SIZE(max1236_mode_list),
1102 .default_mode = s0to3,
1103 .info = &max1238_info,
1104 .channels = max1136_channels,
1105 .num_channels = ARRAY_SIZE(max1136_channels),
1106 },
1107 [max1138] = {
1108 .bits = 10,
1109 .int_vref_mv = 4096,
1110 .mode_list = max1238_mode_list,
1111 .num_modes = ARRAY_SIZE(max1238_mode_list),
1112 .default_mode = s0to11,
1113 .info = &max1238_info,
1114 .channels = max1138_channels,
1115 .num_channels = ARRAY_SIZE(max1138_channels),
1116 },
1117 [max1139] = {
1118 .bits = 10,
1119 .int_vref_mv = 2048,
1120 .mode_list = max1238_mode_list,
1121 .num_modes = ARRAY_SIZE(max1238_mode_list),
1122 .default_mode = s0to11,
1123 .info = &max1238_info,
1124 .channels = max1138_channels,
1125 .num_channels = ARRAY_SIZE(max1138_channels),
1126 },
1127 [max1236] = {
1128 .bits = 12,
1129 .int_vref_mv = 4096,
1130 .mode_list = max1236_mode_list,
1131 .num_modes = ARRAY_SIZE(max1236_mode_list),
1132 .default_mode = s0to3,
1133 .info = &max1238_info,
1134 .channels = max1236_channels,
1135 .num_channels = ARRAY_SIZE(max1236_channels),
1136 },
1137 [max1237] = {
1138 .bits = 12,
1139 .int_vref_mv = 2048,
1140 .mode_list = max1236_mode_list,
1141 .num_modes = ARRAY_SIZE(max1236_mode_list),
1142 .default_mode = s0to3,
1143 .info = &max1238_info,
1144 .channels = max1236_channels,
1145 .num_channels = ARRAY_SIZE(max1236_channels),
1146 },
1147 [max1238] = {
1148 .bits = 12,
1149 .int_vref_mv = 4096,
1150 .mode_list = max1238_mode_list,
1151 .num_modes = ARRAY_SIZE(max1238_mode_list),
1152 .default_mode = s0to11,
1153 .info = &max1238_info,
1154 .channels = max1238_channels,
1155 .num_channels = ARRAY_SIZE(max1238_channels),
1156 },
1157 [max1239] = {
1158 .bits = 12,
1159 .int_vref_mv = 2048,
1160 .mode_list = max1238_mode_list,
1161 .num_modes = ARRAY_SIZE(max1238_mode_list),
1162 .default_mode = s0to11,
1163 .info = &max1238_info,
1164 .channels = max1238_channels,
1165 .num_channels = ARRAY_SIZE(max1238_channels),
1166 },
1167 [max11600] = {
1168 .bits = 8,
1169 .int_vref_mv = 4096,
1170 .mode_list = max11607_mode_list,
1171 .num_modes = ARRAY_SIZE(max11607_mode_list),
1172 .default_mode = s0to3,
1173 .info = &max1238_info,
1174 .channels = max1036_channels,
1175 .num_channels = ARRAY_SIZE(max1036_channels),
1176 },
1177 [max11601] = {
1178 .bits = 8,
1179 .int_vref_mv = 2048,
1180 .mode_list = max11607_mode_list,
1181 .num_modes = ARRAY_SIZE(max11607_mode_list),
1182 .default_mode = s0to3,
1183 .info = &max1238_info,
1184 .channels = max1036_channels,
1185 .num_channels = ARRAY_SIZE(max1036_channels),
1186 },
1187 [max11602] = {
1188 .bits = 8,
1189 .int_vref_mv = 4096,
1190 .mode_list = max11608_mode_list,
1191 .num_modes = ARRAY_SIZE(max11608_mode_list),
1192 .default_mode = s0to7,
1193 .info = &max1238_info,
1194 .channels = max11602_channels,
1195 .num_channels = ARRAY_SIZE(max11602_channels),
1196 },
1197 [max11603] = {
1198 .bits = 8,
1199 .int_vref_mv = 2048,
1200 .mode_list = max11608_mode_list,
1201 .num_modes = ARRAY_SIZE(max11608_mode_list),
1202 .default_mode = s0to7,
1203 .info = &max1238_info,
1204 .channels = max11602_channels,
1205 .num_channels = ARRAY_SIZE(max11602_channels),
1206 },
1207 [max11604] = {
1208 .bits = 8,
1209 .int_vref_mv = 4098,
1210 .mode_list = max1238_mode_list,
1211 .num_modes = ARRAY_SIZE(max1238_mode_list),
1212 .default_mode = s0to11,
1213 .info = &max1238_info,
1214 .channels = max1238_channels,
1215 .num_channels = ARRAY_SIZE(max1238_channels),
1216 },
1217 [max11605] = {
1218 .bits = 8,
1219 .int_vref_mv = 2048,
1220 .mode_list = max1238_mode_list,
1221 .num_modes = ARRAY_SIZE(max1238_mode_list),
1222 .default_mode = s0to11,
1223 .info = &max1238_info,
1224 .channels = max1238_channels,
1225 .num_channels = ARRAY_SIZE(max1238_channels),
1226 },
1227 [max11606] = {
1228 .bits = 10,
1229 .int_vref_mv = 4096,
1230 .mode_list = max11607_mode_list,
1231 .num_modes = ARRAY_SIZE(max11607_mode_list),
1232 .default_mode = s0to3,
1233 .info = &max1238_info,
1234 .channels = max1136_channels,
1235 .num_channels = ARRAY_SIZE(max1136_channels),
1236 },
1237 [max11607] = {
1238 .bits = 10,
1239 .int_vref_mv = 2048,
1240 .mode_list = max11607_mode_list,
1241 .num_modes = ARRAY_SIZE(max11607_mode_list),
1242 .default_mode = s0to3,
1243 .info = &max1238_info,
1244 .channels = max1136_channels,
1245 .num_channels = ARRAY_SIZE(max1136_channels),
1246 },
1247 [max11608] = {
1248 .bits = 10,
1249 .int_vref_mv = 4096,
1250 .mode_list = max11608_mode_list,
1251 .num_modes = ARRAY_SIZE(max11608_mode_list),
1252 .default_mode = s0to7,
1253 .info = &max1238_info,
1254 .channels = max11608_channels,
1255 .num_channels = ARRAY_SIZE(max11608_channels),
1256 },
1257 [max11609] = {
1258 .bits = 10,
1259 .int_vref_mv = 2048,
1260 .mode_list = max11608_mode_list,
1261 .num_modes = ARRAY_SIZE(max11608_mode_list),
1262 .default_mode = s0to7,
1263 .info = &max1238_info,
1264 .channels = max11608_channels,
1265 .num_channels = ARRAY_SIZE(max11608_channels),
1266 },
1267 [max11610] = {
1268 .bits = 10,
1269 .int_vref_mv = 4098,
1270 .mode_list = max1238_mode_list,
1271 .num_modes = ARRAY_SIZE(max1238_mode_list),
1272 .default_mode = s0to11,
1273 .info = &max1238_info,
1274 .channels = max1238_channels,
1275 .num_channels = ARRAY_SIZE(max1238_channels),
1276 },
1277 [max11611] = {
1278 .bits = 10,
1279 .int_vref_mv = 2048,
1280 .mode_list = max1238_mode_list,
1281 .num_modes = ARRAY_SIZE(max1238_mode_list),
1282 .default_mode = s0to11,
1283 .info = &max1238_info,
1284 .channels = max1238_channels,
1285 .num_channels = ARRAY_SIZE(max1238_channels),
1286 },
1287 [max11612] = {
1288 .bits = 12,
1289 .int_vref_mv = 4096,
1290 .mode_list = max11607_mode_list,
1291 .num_modes = ARRAY_SIZE(max11607_mode_list),
1292 .default_mode = s0to3,
1293 .info = &max1238_info,
1294 .channels = max1363_channels,
1295 .num_channels = ARRAY_SIZE(max1363_channels),
1296 },
1297 [max11613] = {
1298 .bits = 12,
1299 .int_vref_mv = 2048,
1300 .mode_list = max11607_mode_list,
1301 .num_modes = ARRAY_SIZE(max11607_mode_list),
1302 .default_mode = s0to3,
1303 .info = &max1238_info,
1304 .channels = max1363_channels,
1305 .num_channels = ARRAY_SIZE(max1363_channels),
1306 },
1307 [max11614] = {
1308 .bits = 12,
1309 .int_vref_mv = 4096,
1310 .mode_list = max11608_mode_list,
1311 .num_modes = ARRAY_SIZE(max11608_mode_list),
1312 .default_mode = s0to7,
1313 .info = &max1238_info,
1314 .channels = max11614_channels,
1315 .num_channels = ARRAY_SIZE(max11614_channels),
1316 },
1317 [max11615] = {
1318 .bits = 12,
1319 .int_vref_mv = 2048,
1320 .mode_list = max11608_mode_list,
1321 .num_modes = ARRAY_SIZE(max11608_mode_list),
1322 .default_mode = s0to7,
1323 .info = &max1238_info,
1324 .channels = max11614_channels,
1325 .num_channels = ARRAY_SIZE(max11614_channels),
1326 },
1327 [max11616] = {
1328 .bits = 12,
1329 .int_vref_mv = 4098,
1330 .mode_list = max1238_mode_list,
1331 .num_modes = ARRAY_SIZE(max1238_mode_list),
1332 .default_mode = s0to11,
1333 .info = &max1238_info,
1334 .channels = max1238_channels,
1335 .num_channels = ARRAY_SIZE(max1238_channels),
1336 },
1337 [max11617] = {
1338 .bits = 12,
1339 .int_vref_mv = 2048,
1340 .mode_list = max1238_mode_list,
1341 .num_modes = ARRAY_SIZE(max1238_mode_list),
1342 .default_mode = s0to11,
1343 .info = &max1238_info,
1344 .channels = max1238_channels,
1345 .num_channels = ARRAY_SIZE(max1238_channels),
1346 },
1347 [max11644] = {
1348 .bits = 12,
1349 .int_vref_mv = 2048,
1350 .mode_list = max11644_mode_list,
1351 .num_modes = ARRAY_SIZE(max11644_mode_list),
1352 .default_mode = s0to1,
1353 .info = &max1238_info,
1354 .channels = max11644_channels,
1355 .num_channels = ARRAY_SIZE(max11644_channels),
1356 },
1357 [max11645] = {
1358 .bits = 12,
1359 .int_vref_mv = 4096,
1360 .mode_list = max11644_mode_list,
1361 .num_modes = ARRAY_SIZE(max11644_mode_list),
1362 .default_mode = s0to1,
1363 .info = &max1238_info,
1364 .channels = max11644_channels,
1365 .num_channels = ARRAY_SIZE(max11644_channels),
1366 },
1367 [max11646] = {
1368 .bits = 10,
1369 .int_vref_mv = 2048,
1370 .mode_list = max11644_mode_list,
1371 .num_modes = ARRAY_SIZE(max11644_mode_list),
1372 .default_mode = s0to1,
1373 .info = &max1238_info,
1374 .channels = max11646_channels,
1375 .num_channels = ARRAY_SIZE(max11646_channels),
1376 },
1377 [max11647] = {
1378 .bits = 10,
1379 .int_vref_mv = 4096,
1380 .mode_list = max11644_mode_list,
1381 .num_modes = ARRAY_SIZE(max11644_mode_list),
1382 .default_mode = s0to1,
1383 .info = &max1238_info,
1384 .channels = max11646_channels,
1385 .num_channels = ARRAY_SIZE(max11646_channels),
1386 },
1387};
1388
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001389static int max1363_initial_setup(struct max1363_state *st)
1390{
1391 st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD
1392 | MAX1363_SETUP_POWER_UP_INT_REF
1393 | MAX1363_SETUP_INT_CLOCK
1394 | MAX1363_SETUP_UNIPOLAR
1395 | MAX1363_SETUP_NORESET;
1396
1397 /* Set scan mode writes the config anyway so wait until then*/
1398 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1399 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1400 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
1401
1402 return max1363_set_scan_mode(st);
1403}
1404
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001405static int __devinit max1363_alloc_scan_masks(struct iio_dev *indio_dev)
1406{
1407 struct max1363_state *st = iio_priv(indio_dev);
1408 unsigned long *masks;
1409 int i;
1410
1411 masks = kzalloc(BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*sizeof(long)*
1412 (st->chip_info->num_modes + 1), GFP_KERNEL);
1413 if (!masks)
1414 return -ENOMEM;
1415
1416 for (i = 0; i < st->chip_info->num_modes; i++)
1417 bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
1418 max1363_mode_table[st->chip_info->mode_list[i]]
1419 .modemask, MAX1363_MAX_CHANNELS);
1420
1421 indio_dev->available_scan_masks = masks;
1422
1423 return 0;
1424}
1425
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001426
1427static irqreturn_t max1363_trigger_handler(int irq, void *p)
1428{
1429 struct iio_poll_func *pf = p;
1430 struct iio_dev *indio_dev = pf->indio_dev;
1431 struct max1363_state *st = iio_priv(indio_dev);
1432 s64 time_ns;
1433 __u8 *rxbuf;
1434 int b_sent;
1435 size_t d_size;
1436 unsigned long numvals = bitmap_weight(st->current_mode->modemask,
1437 MAX1363_MAX_CHANNELS);
1438
1439 /* Ensure the timestamp is 8 byte aligned */
1440 if (st->chip_info->bits != 8)
1441 d_size = numvals*2;
1442 else
1443 d_size = numvals;
1444 if (indio_dev->scan_timestamp) {
1445 d_size += sizeof(s64);
1446 if (d_size % sizeof(s64))
1447 d_size += sizeof(s64) - (d_size % sizeof(s64));
1448 }
1449 /* Monitor mode prevents reading. Whilst not currently implemented
1450 * might as well have this test in here in the meantime as it does
1451 * no harm.
1452 */
1453 if (numvals == 0)
1454 goto done;
1455
1456 rxbuf = kmalloc(d_size, GFP_KERNEL);
1457 if (rxbuf == NULL)
1458 goto done;
1459 if (st->chip_info->bits != 8)
1460 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
1461 else
1462 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
1463 if (b_sent < 0)
1464 goto done_free;
1465
1466 time_ns = iio_get_time_ns();
1467
1468 if (indio_dev->scan_timestamp)
1469 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
1470 iio_push_to_buffers(indio_dev, rxbuf);
1471
1472done_free:
1473 kfree(rxbuf);
1474done:
1475 iio_trigger_notify_done(indio_dev->trig);
1476
1477 return IRQ_HANDLED;
1478}
1479
Jonathan Cameronec443722012-10-06 14:19:01 +01001480static const struct iio_buffer_setup_ops max1363_buffered_setup_ops = {
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001481 .postenable = &iio_triggered_buffer_postenable,
1482 .preenable = &iio_sw_buffer_preenable,
1483 .predisable = &iio_triggered_buffer_predisable,
1484};
1485
Jonathan Cameronec443722012-10-06 14:19:01 +01001486static int max1363_register_buffered_funcs_and_init(struct iio_dev *indio_dev)
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001487{
1488 struct max1363_state *st = iio_priv(indio_dev);
1489 int ret = 0;
1490
1491 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
1492 if (!indio_dev->buffer) {
1493 ret = -ENOMEM;
1494 goto error_ret;
1495 }
1496 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
1497 &max1363_trigger_handler,
1498 IRQF_ONESHOT,
1499 indio_dev,
1500 "%s_consumer%d",
1501 st->client->name,
1502 indio_dev->id);
1503 if (indio_dev->pollfunc == NULL) {
1504 ret = -ENOMEM;
1505 goto error_deallocate_sw_rb;
1506 }
Jonathan Cameronec443722012-10-06 14:19:01 +01001507 /* Buffer functions - here trigger setup related */
1508 indio_dev->setup_ops = &max1363_buffered_setup_ops;
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001509
Jonathan Cameronec443722012-10-06 14:19:01 +01001510 /* Flag that polled buffering is possible */
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001511 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
1512
1513 return 0;
1514
1515error_deallocate_sw_rb:
1516 iio_kfifo_free(indio_dev->buffer);
1517error_ret:
1518 return ret;
1519}
1520
Jonathan Cameronec443722012-10-06 14:19:01 +01001521static void max1363_buffer_cleanup(struct iio_dev *indio_dev)
Jonathan Cameronb3bcbfc2012-10-06 14:10:05 +01001522{
1523 /* ensure that the trigger has been detached */
1524 iio_dealloc_pollfunc(indio_dev->pollfunc);
1525 iio_kfifo_free(indio_dev->buffer);
1526}
1527
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001528static int __devinit max1363_probe(struct i2c_client *client,
1529 const struct i2c_device_id *id)
1530{
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001531 int ret;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001532 struct max1363_state *st;
1533 struct iio_dev *indio_dev;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001534
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001535 indio_dev = iio_device_alloc(sizeof(struct max1363_state));
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001536 if (indio_dev == NULL) {
1537 ret = -ENOMEM;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001538 goto error_out;
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001539 }
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001540
Jonathan Cameron0b27d672012-04-10 21:11:07 +01001541 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
1542 if (ret < 0)
1543 goto error_free_device;
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001544
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001545 st = iio_priv(indio_dev);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001546
1547 st->reg = regulator_get(&client->dev, "vcc");
1548 if (IS_ERR(st->reg)) {
1549 ret = PTR_ERR(st->reg);
1550 goto error_unregister_map;
1551 }
1552
1553 ret = regulator_enable(st->reg);
1554 if (ret)
1555 goto error_put_reg;
1556
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001557 /* this is only used for device removal purposes */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001558 i2c_set_clientdata(client, indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001559
Jonathan Cameron962a4cc2010-06-26 12:54:13 +01001560 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001561 st->client = client;
1562
Michael Hennerichcd4361c2012-02-22 13:16:49 +01001563 ret = max1363_alloc_scan_masks(indio_dev);
1564 if (ret)
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001565 goto error_disable_reg;
Jonathan Cameron82020b02010-05-04 14:43:03 +01001566
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001567 /* Estabilish that the iio_dev is a child of the i2c device */
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001568 indio_dev->dev.parent = &client->dev;
Jonathan Cameron845bd122011-05-18 14:41:44 +01001569 indio_dev->name = id->name;
Jonathan Cameron8e7d9672011-08-30 12:32:45 +01001570 indio_dev->channels = st->chip_info->channels;
1571 indio_dev->num_channels = st->chip_info->num_channels;
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001572 indio_dev->info = st->chip_info->info;
1573 indio_dev->modes = INDIO_DIRECT_MODE;
Jonathan Cameroncd038012011-08-12 16:55:29 +01001574 indio_dev->channels = st->chip_info->channels;
1575 indio_dev->num_channels = st->chip_info->num_channels;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001576 ret = max1363_initial_setup(st);
Jonathan Cameron58f0a252011-05-18 14:42:38 +01001577 if (ret < 0)
Jonathan Cameron82020b02010-05-04 14:43:03 +01001578 goto error_free_available_scan_masks;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001579
Jonathan Cameronec443722012-10-06 14:19:01 +01001580 ret = max1363_register_buffered_funcs_and_init(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001581 if (ret)
Jonathan Cameron82020b02010-05-04 14:43:03 +01001582 goto error_free_available_scan_masks;
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001583
Jonathan Cameron14555b12011-09-21 11:15:57 +01001584 ret = iio_buffer_register(indio_dev,
1585 st->chip_info->channels,
1586 st->chip_info->num_channels);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001587 if (ret)
Jonathan Cameronec443722012-10-06 14:19:01 +01001588 goto error_cleanup_buffer;
Jonathan Cameron440a5202010-06-26 12:54:23 +01001589
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001590 if (client->irq) {
Jonathan Cameronaaf370d2011-05-18 14:41:16 +01001591 ret = request_threaded_irq(st->client->irq,
1592 NULL,
1593 &max1363_event_handler,
1594 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1595 "max1363_event",
1596 indio_dev);
Jonathan Cameron298cd972011-05-18 14:40:53 +01001597
Jonathan Cameron440a5202010-06-26 12:54:23 +01001598 if (ret)
Jonathan Cameronec443722012-10-06 14:19:01 +01001599 goto error_uninit_buffer;
Jonathan Cameron440a5202010-06-26 12:54:23 +01001600 }
1601
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001602 ret = iio_device_register(indio_dev);
1603 if (ret < 0)
1604 goto error_free_irq;
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001605
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001606 return 0;
1607error_free_irq:
Guenter Roeck678fb422012-12-24 06:24:00 +00001608 if (client->irq)
1609 free_irq(st->client->irq, indio_dev);
Jonathan Cameronec443722012-10-06 14:19:01 +01001610error_uninit_buffer:
Jonathan Cameron14555b12011-09-21 11:15:57 +01001611 iio_buffer_unregister(indio_dev);
Jonathan Cameronec443722012-10-06 14:19:01 +01001612error_cleanup_buffer:
1613 max1363_buffer_cleanup(indio_dev);
Jonathan Cameron82020b02010-05-04 14:43:03 +01001614error_free_available_scan_masks:
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001615 kfree(indio_dev->available_scan_masks);
Jonathan Cameronb77b8f82012-10-06 14:39:19 +01001616error_disable_reg:
1617 regulator_disable(st->reg);
1618error_put_reg:
1619 regulator_put(st->reg);
Guenter Roeck678fb422012-12-24 06:24:00 +00001620error_unregister_map:
1621 iio_map_array_unregister(indio_dev, client->dev.platform_data);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001622error_free_device:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001623 iio_device_free(indio_dev);
Dan Carpenterf88af7e2011-06-08 09:42:21 +03001624error_out:
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001625 return ret;
1626}
1627
Lars-Peter Clausen8e828752012-08-17 18:29:00 +01001628static int __devexit max1363_remove(struct i2c_client *client)
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001629{
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001630 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1631 struct max1363_state *st = iio_priv(indio_dev);
Jonathan Cameron440a5202010-06-26 12:54:23 +01001632
Jonathan Camerond2fffd62011-10-14 14:46:58 +01001633 iio_device_unregister(indio_dev);
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001634 if (client->irq)
Jonathan Cameronaaf370d2011-05-18 14:41:16 +01001635 free_irq(st->client->irq, indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +01001636 iio_buffer_unregister(indio_dev);
Jonathan Cameronec443722012-10-06 14:19:01 +01001637 max1363_buffer_cleanup(indio_dev);
Jonathan Cameron3dba81b2011-04-15 18:55:59 +01001638 kfree(indio_dev->available_scan_masks);
Guenter Roeck678fb422012-12-24 06:24:00 +00001639 regulator_disable(st->reg);
1640 regulator_put(st->reg);
Jonathan Cameron0b27d672012-04-10 21:11:07 +01001641 iio_map_array_unregister(indio_dev, client->dev.platform_data);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001642 iio_device_free(indio_dev);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001643
1644 return 0;
1645}
1646
1647static const struct i2c_device_id max1363_id[] = {
1648 { "max1361", max1361 },
1649 { "max1362", max1362 },
1650 { "max1363", max1363 },
1651 { "max1364", max1364 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001652 { "max1036", max1036 },
1653 { "max1037", max1037 },
1654 { "max1038", max1038 },
1655 { "max1039", max1039 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001656 { "max1136", max1136 },
1657 { "max1137", max1137 },
1658 { "max1138", max1138 },
1659 { "max1139", max1139 },
1660 { "max1236", max1236 },
1661 { "max1237", max1237 },
1662 { "max1238", max1238 },
1663 { "max1239", max1239 },
Jonathan Cameron3bf877c2010-05-04 14:43:10 +01001664 { "max11600", max11600 },
1665 { "max11601", max11601 },
1666 { "max11602", max11602 },
1667 { "max11603", max11603 },
1668 { "max11604", max11604 },
1669 { "max11605", max11605 },
Jonathan Cameronc3fa0fdd2010-05-04 14:43:09 +01001670 { "max11606", max11606 },
1671 { "max11607", max11607 },
1672 { "max11608", max11608 },
1673 { "max11609", max11609 },
1674 { "max11610", max11610 },
1675 { "max11611", max11611 },
1676 { "max11612", max11612 },
1677 { "max11613", max11613 },
1678 { "max11614", max11614 },
1679 { "max11615", max11615 },
1680 { "max11616", max11616 },
1681 { "max11617", max11617 },
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001682 {}
1683};
1684
1685MODULE_DEVICE_TABLE(i2c, max1363_id);
1686
1687static struct i2c_driver max1363_driver = {
1688 .driver = {
1689 .name = "max1363",
1690 },
1691 .probe = max1363_probe,
Lars-Peter Clausen8e828752012-08-17 18:29:00 +01001692 .remove = __devexit_p(max1363_remove),
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001693 .id_table = max1363_id,
1694};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +01001695module_i2c_driver(max1363_driver);
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001696
Jonathan Cameron0f8c9622012-09-02 21:34:59 +01001697MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond1325cf2009-08-18 18:06:20 +01001698MODULE_DESCRIPTION("Maxim 1363 ADC");
1699MODULE_LICENSE("GPL v2");