3 * Copyright (C) 2008-2010 Jonathan Cameron
5 * based on linux/drivers/i2c/chips/max123x
6 * Copyright (C) 2002-2004 Stefan Eletzhofer
8 * based on linux/drivers/acron/char/pcf8583.c
9 * Copyright (C) 2000 Russell King
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.
17 * Partial support for max1363 and similar chips.
19 * Not currently implemented.
21 * - Control of internal reference.
24 #include <linux/interrupt.h>
25 #include <linux/workqueue.h>
26 #include <linux/device.h>
27 #include <linux/kernel.h>
28 #include <linux/sysfs.h>
29 #include <linux/list.h>
30 #include <linux/i2c.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/slab.h>
33 #include <linux/err.h>
38 #include "../ring_generic.h"
42 /* Here we claim all are 16 bits. This currently does no harm and saves
43 * us a lot of scan element listings */
45 #define MAX1363_SCAN_EL(number) \
46 IIO_SCAN_EL_C(in##number, number, 0, NULL);
47 #define MAX1363_SCAN_EL_D(p, n, number) \
48 IIO_SCAN_NAMED_EL_C(in##p##m##in##n, in##p-in##n, number, 0, NULL);
50 static MAX1363_SCAN_EL(0);
51 static MAX1363_SCAN_EL(1);
52 static MAX1363_SCAN_EL(2);
53 static MAX1363_SCAN_EL(3);
54 static MAX1363_SCAN_EL(4);
55 static MAX1363_SCAN_EL(5);
56 static MAX1363_SCAN_EL(6);
57 static MAX1363_SCAN_EL(7);
58 static MAX1363_SCAN_EL(8);
59 static MAX1363_SCAN_EL(9);
60 static MAX1363_SCAN_EL(10);
61 static MAX1363_SCAN_EL(11);
62 static MAX1363_SCAN_EL_D(0, 1, 12);
63 static MAX1363_SCAN_EL_D(2, 3, 13);
64 static MAX1363_SCAN_EL_D(4, 5, 14);
65 static MAX1363_SCAN_EL_D(6, 7, 15);
66 static MAX1363_SCAN_EL_D(8, 9, 16);
67 static MAX1363_SCAN_EL_D(10, 11, 17);
68 static MAX1363_SCAN_EL_D(1, 0, 18);
69 static MAX1363_SCAN_EL_D(3, 2, 19);
70 static MAX1363_SCAN_EL_D(5, 4, 20);
71 static MAX1363_SCAN_EL_D(7, 6, 21);
72 static MAX1363_SCAN_EL_D(9, 8, 22);
73 static MAX1363_SCAN_EL_D(11, 10, 23);
75 static const struct max1363_mode max1363_mode_table[] = {
76 /* All of the single channel options first */
77 MAX1363_MODE_SINGLE(0, 1 << 0),
78 MAX1363_MODE_SINGLE(1, 1 << 1),
79 MAX1363_MODE_SINGLE(2, 1 << 2),
80 MAX1363_MODE_SINGLE(3, 1 << 3),
81 MAX1363_MODE_SINGLE(4, 1 << 4),
82 MAX1363_MODE_SINGLE(5, 1 << 5),
83 MAX1363_MODE_SINGLE(6, 1 << 6),
84 MAX1363_MODE_SINGLE(7, 1 << 7),
85 MAX1363_MODE_SINGLE(8, 1 << 8),
86 MAX1363_MODE_SINGLE(9, 1 << 9),
87 MAX1363_MODE_SINGLE(10, 1 << 10),
88 MAX1363_MODE_SINGLE(11, 1 << 11),
90 MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
91 MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
92 MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
93 MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
94 MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
95 MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
96 MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
97 MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
98 MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
99 MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
100 MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
101 MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
103 /* The multichannel scans next */
104 MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
105 MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
106 MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
107 MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
108 MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
109 MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
110 MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
111 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
112 MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
113 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
114 MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
115 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
116 MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
117 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
118 MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
119 MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
120 MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
122 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
123 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
124 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
125 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
126 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
127 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
128 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
129 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
130 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
131 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
132 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
133 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
134 MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
135 MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
138 const struct max1363_mode
139 *max1363_match_mode(u32 mask, const struct max1363_chip_info *ci)
143 for (i = 0; i < ci->num_modes; i++)
144 if (!((~max1363_mode_table[ci->mode_list[i]].modemask) &
146 return &max1363_mode_table[ci->mode_list[i]];
150 static ssize_t max1363_show_precision_u(struct device *dev,
151 struct device_attribute *attr,
154 struct iio_dev *dev_info = dev_get_drvdata(dev);
155 struct max1363_state *st = iio_dev_get_devdata(dev_info);
156 return sprintf(buf, "u%d/16\n", st->chip_info->bits);
159 static ssize_t max1363_show_precision_s(struct device *dev,
160 struct device_attribute *attr,
163 struct iio_dev *dev_info = dev_get_drvdata(dev);
164 struct max1363_state *st = iio_dev_get_devdata(dev_info);
165 return sprintf(buf, "s%d/16\n", st->chip_info->bits);
168 #define MAX1363_SCAN_TYPE(n) \
169 DEVICE_ATTR(in##n##_type, S_IRUGO, \
170 max1363_show_precision_u, NULL);
171 #define MAX1363_SCAN_TYPE_D(p, n) \
172 struct device_attribute dev_attr_in##p##m##in##n##_type = \
173 __ATTR(in##p-in##n##_type, S_IRUGO, \
174 max1363_show_precision_s, NULL);
176 static MAX1363_SCAN_TYPE(0);
177 static MAX1363_SCAN_TYPE(1);
178 static MAX1363_SCAN_TYPE(2);
179 static MAX1363_SCAN_TYPE(3);
180 static MAX1363_SCAN_TYPE(4);
181 static MAX1363_SCAN_TYPE(5);
182 static MAX1363_SCAN_TYPE(6);
183 static MAX1363_SCAN_TYPE(7);
184 static MAX1363_SCAN_TYPE(8);
185 static MAX1363_SCAN_TYPE(9);
186 static MAX1363_SCAN_TYPE(10);
187 static MAX1363_SCAN_TYPE(11);
189 static MAX1363_SCAN_TYPE_D(0, 1);
190 static MAX1363_SCAN_TYPE_D(2, 3);
191 static MAX1363_SCAN_TYPE_D(4, 5);
192 static MAX1363_SCAN_TYPE_D(6, 7);
193 static MAX1363_SCAN_TYPE_D(8, 9);
194 static MAX1363_SCAN_TYPE_D(10, 11);
195 static MAX1363_SCAN_TYPE_D(1, 0);
196 static MAX1363_SCAN_TYPE_D(3, 2);
197 static MAX1363_SCAN_TYPE_D(5, 4);
198 static MAX1363_SCAN_TYPE_D(7, 6);
199 static MAX1363_SCAN_TYPE_D(9, 8);
200 static MAX1363_SCAN_TYPE_D(11, 10);
202 static int max1363_write_basic_config(struct i2c_client *client,
207 u8 *tx_buf = kmalloc(2, GFP_KERNEL);
214 ret = i2c_master_send(client, tx_buf, 2);
217 return (ret > 0) ? 0 : ret;
220 int max1363_set_scan_mode(struct max1363_state *st)
222 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
224 | MAX1363_SE_DE_MASK);
225 st->configbyte |= st->current_mode->conf;
227 return max1363_write_basic_config(st->client,
232 static ssize_t max1363_read_single_channel(struct device *dev,
233 struct device_attribute *attr,
236 struct iio_dev *dev_info = dev_get_drvdata(dev);
237 struct max1363_state *st = iio_dev_get_devdata(dev_info);
238 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
239 struct i2c_client *client = st->client;
240 int ret = 0, len = 0;
245 mutex_lock(&dev_info->mlock);
247 * If monitor mode is enabled, the method for reading a single
248 * channel will have to be rather different and has not yet
251 if (st->monitor_on) {
256 /* If ring buffer capture is occuring, query the buffer */
257 if (iio_ring_enabled(dev_info)) {
258 mask = max1363_mode_table[this_attr->address].modemask;
259 data = max1363_single_channel_from_ring(mask, st);
265 /* Check to see if current scan mode is correct */
266 if (st->current_mode !=
267 &max1363_mode_table[this_attr->address]) {
268 /* Update scan mode if needed */
270 = &max1363_mode_table[this_attr->address];
271 ret = max1363_set_scan_mode(st);
275 if (st->chip_info->bits != 8) {
277 data = i2c_master_recv(client, rxbuf, 2);
283 data = (s32)(rxbuf[1]) | ((s32)(rxbuf[0] & 0x0F)) << 8;
286 data = i2c_master_recv(client, rxbuf, 1);
294 /* Pretty print the result */
295 len = sprintf(buf, "%u\n", data);
298 mutex_unlock(&dev_info->mlock);
299 return ret ? ret : len;
302 /* Direct read attribtues */
303 static IIO_DEV_ATTR_IN_RAW(0, max1363_read_single_channel, _s0);
304 static IIO_DEV_ATTR_IN_RAW(1, max1363_read_single_channel, _s1);
305 static IIO_DEV_ATTR_IN_RAW(2, max1363_read_single_channel, _s2);
306 static IIO_DEV_ATTR_IN_RAW(3, max1363_read_single_channel, _s3);
307 static IIO_DEV_ATTR_IN_RAW(4, max1363_read_single_channel, _s4);
308 static IIO_DEV_ATTR_IN_RAW(5, max1363_read_single_channel, _s5);
309 static IIO_DEV_ATTR_IN_RAW(6, max1363_read_single_channel, _s6);
310 static IIO_DEV_ATTR_IN_RAW(7, max1363_read_single_channel, _s7);
311 static IIO_DEV_ATTR_IN_RAW(8, max1363_read_single_channel, _s8);
312 static IIO_DEV_ATTR_IN_RAW(9, max1363_read_single_channel, _s9);
313 static IIO_DEV_ATTR_IN_RAW(10, max1363_read_single_channel, _s10);
314 static IIO_DEV_ATTR_IN_RAW(11, max1363_read_single_channel, _s11);
316 static IIO_DEV_ATTR_IN_DIFF_RAW(0, 1, max1363_read_single_channel, d0m1);
317 static IIO_DEV_ATTR_IN_DIFF_RAW(2, 3, max1363_read_single_channel, d2m3);
318 static IIO_DEV_ATTR_IN_DIFF_RAW(4, 5, max1363_read_single_channel, d4m5);
319 static IIO_DEV_ATTR_IN_DIFF_RAW(6, 7, max1363_read_single_channel, d6m7);
320 static IIO_DEV_ATTR_IN_DIFF_RAW(8, 9, max1363_read_single_channel, d8m9);
321 static IIO_DEV_ATTR_IN_DIFF_RAW(10, 11, max1363_read_single_channel, d10m11);
322 static IIO_DEV_ATTR_IN_DIFF_RAW(1, 0, max1363_read_single_channel, d1m0);
323 static IIO_DEV_ATTR_IN_DIFF_RAW(3, 2, max1363_read_single_channel, d3m2);
324 static IIO_DEV_ATTR_IN_DIFF_RAW(5, 4, max1363_read_single_channel, d5m4);
325 static IIO_DEV_ATTR_IN_DIFF_RAW(7, 6, max1363_read_single_channel, d7m6);
326 static IIO_DEV_ATTR_IN_DIFF_RAW(9, 8, max1363_read_single_channel, d9m8);
327 static IIO_DEV_ATTR_IN_DIFF_RAW(11, 10, max1363_read_single_channel, d11m10);
330 static ssize_t max1363_show_scale(struct device *dev,
331 struct device_attribute *attr,
334 /* Driver currently only support internal vref */
335 struct iio_dev *dev_info = dev_get_drvdata(dev);
336 struct max1363_state *st = iio_dev_get_devdata(dev_info);
337 /* Corresponds to Vref / 2^(bits) */
339 if ((1 << (st->chip_info->bits + 1))
340 > st->chip_info->int_vref_mv)
341 return sprintf(buf, "0.5\n");
343 return sprintf(buf, "%d\n",
344 st->chip_info->int_vref_mv >> st->chip_info->bits);
347 static IIO_DEVICE_ATTR(in_scale, S_IRUGO, max1363_show_scale, NULL, 0);
349 static ssize_t max1363_show_name(struct device *dev,
350 struct device_attribute *attr,
353 struct iio_dev *dev_info = dev_get_drvdata(dev);
354 struct max1363_state *st = iio_dev_get_devdata(dev_info);
355 return sprintf(buf, "%s\n", st->client->name);
358 static IIO_DEVICE_ATTR(name, S_IRUGO, max1363_show_name, NULL, 0);
360 /* Applies to max1363 */
361 static const enum max1363_modes max1363_mode_list[] = {
364 d0m1, d2m3, d1m0, d3m2,
365 d0m1to2m3, d1m0to3m2,
368 static struct attribute *max1363_device_attrs[] = {
369 &iio_dev_attr_in0_raw.dev_attr.attr,
370 &iio_dev_attr_in1_raw.dev_attr.attr,
371 &iio_dev_attr_in2_raw.dev_attr.attr,
372 &iio_dev_attr_in3_raw.dev_attr.attr,
373 &iio_dev_attr_in0min1_raw.dev_attr.attr,
374 &iio_dev_attr_in2min3_raw.dev_attr.attr,
375 &iio_dev_attr_in1min0_raw.dev_attr.attr,
376 &iio_dev_attr_in3min2_raw.dev_attr.attr,
377 &iio_dev_attr_name.dev_attr.attr,
378 &iio_dev_attr_in_scale.dev_attr.attr,
382 static struct attribute_group max1363_dev_attr_group = {
383 .attrs = max1363_device_attrs,
386 static struct attribute *max1363_scan_el_attrs[] = {
387 &iio_scan_el_in0.dev_attr.attr, &dev_attr_in0_type.attr,
388 &iio_scan_el_in1.dev_attr.attr, &dev_attr_in1_type.attr,
389 &iio_scan_el_in2.dev_attr.attr, &dev_attr_in2_type.attr,
390 &iio_scan_el_in3.dev_attr.attr, &dev_attr_in3_type.attr,
391 &iio_scan_el_in0min1.dev_attr.attr, &dev_attr_in0min1_type.attr,
392 &iio_scan_el_in2min3.dev_attr.attr, &dev_attr_in2min3_type.attr,
393 &iio_scan_el_in1min0.dev_attr.attr, &dev_attr_in1min0_type.attr,
394 &iio_scan_el_in3min2.dev_attr.attr, &dev_attr_in3min2_type.attr,
398 static struct attribute_group max1363_scan_el_group = {
399 .name = "scan_elements",
400 .attrs = max1363_scan_el_attrs,
403 /* Appies to max1236, max1237 */
404 static const enum max1363_modes max1236_mode_list[] = {
407 d0m1, d2m3, d1m0, d3m2,
408 d0m1to2m3, d1m0to3m2,
412 /* Applies to max1238, max1239 */
413 static const enum max1363_modes max1238_mode_list[] = {
414 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
415 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
416 s0to7, s0to8, s0to9, s0to10, s0to11,
417 d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
418 d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
419 d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
420 d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
421 s6to7, s6to8, s6to9, s6to10, s6to11,
422 d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
425 static struct attribute *max1238_device_attrs[] = {
426 &iio_dev_attr_in0_raw.dev_attr.attr,
427 &iio_dev_attr_in1_raw.dev_attr.attr,
428 &iio_dev_attr_in2_raw.dev_attr.attr,
429 &iio_dev_attr_in3_raw.dev_attr.attr,
430 &iio_dev_attr_in4_raw.dev_attr.attr,
431 &iio_dev_attr_in5_raw.dev_attr.attr,
432 &iio_dev_attr_in6_raw.dev_attr.attr,
433 &iio_dev_attr_in7_raw.dev_attr.attr,
434 &iio_dev_attr_in8_raw.dev_attr.attr,
435 &iio_dev_attr_in9_raw.dev_attr.attr,
436 &iio_dev_attr_in10_raw.dev_attr.attr,
437 &iio_dev_attr_in11_raw.dev_attr.attr,
438 &iio_dev_attr_in0min1_raw.dev_attr.attr,
439 &iio_dev_attr_in2min3_raw.dev_attr.attr,
440 &iio_dev_attr_in4min5_raw.dev_attr.attr,
441 &iio_dev_attr_in6min7_raw.dev_attr.attr,
442 &iio_dev_attr_in8min9_raw.dev_attr.attr,
443 &iio_dev_attr_in10min11_raw.dev_attr.attr,
444 &iio_dev_attr_in1min0_raw.dev_attr.attr,
445 &iio_dev_attr_in3min2_raw.dev_attr.attr,
446 &iio_dev_attr_in5min4_raw.dev_attr.attr,
447 &iio_dev_attr_in7min6_raw.dev_attr.attr,
448 &iio_dev_attr_in9min8_raw.dev_attr.attr,
449 &iio_dev_attr_in11min10_raw.dev_attr.attr,
450 &iio_dev_attr_name.dev_attr.attr,
451 &iio_dev_attr_in_scale.dev_attr.attr,
455 static struct attribute_group max1238_dev_attr_group = {
456 .attrs = max1238_device_attrs,
459 static struct attribute *max1238_scan_el_attrs[] = {
460 &iio_scan_el_in0.dev_attr.attr, &dev_attr_in0_type.attr,
461 &iio_scan_el_in1.dev_attr.attr, &dev_attr_in1_type.attr,
462 &iio_scan_el_in2.dev_attr.attr, &dev_attr_in2_type.attr,
463 &iio_scan_el_in3.dev_attr.attr, &dev_attr_in3_type.attr,
464 &iio_scan_el_in4.dev_attr.attr, &dev_attr_in4_type.attr,
465 &iio_scan_el_in5.dev_attr.attr, &dev_attr_in5_type.attr,
466 &iio_scan_el_in6.dev_attr.attr, &dev_attr_in6_type.attr,
467 &iio_scan_el_in7.dev_attr.attr, &dev_attr_in7_type.attr,
468 &iio_scan_el_in8.dev_attr.attr, &dev_attr_in8_type.attr,
469 &iio_scan_el_in9.dev_attr.attr, &dev_attr_in9_type.attr,
470 &iio_scan_el_in10.dev_attr.attr, &dev_attr_in10_type.attr,
471 &iio_scan_el_in11.dev_attr.attr, &dev_attr_in11_type.attr,
472 &iio_scan_el_in0min1.dev_attr.attr, &dev_attr_in0min1_type.attr,
473 &iio_scan_el_in2min3.dev_attr.attr, &dev_attr_in2min3_type.attr,
474 &iio_scan_el_in4min5.dev_attr.attr, &dev_attr_in4min5_type.attr,
475 &iio_scan_el_in6min7.dev_attr.attr, &dev_attr_in6min7_type.attr,
476 &iio_scan_el_in8min9.dev_attr.attr, &dev_attr_in8min9_type.attr,
477 &iio_scan_el_in10min11.dev_attr.attr, &dev_attr_in10min11_type.attr,
478 &iio_scan_el_in1min0.dev_attr.attr, &dev_attr_in1min0_type.attr,
479 &iio_scan_el_in3min2.dev_attr.attr, &dev_attr_in3min2_type.attr,
480 &iio_scan_el_in5min4.dev_attr.attr, &dev_attr_in5min4_type.attr,
481 &iio_scan_el_in7min6.dev_attr.attr, &dev_attr_in7min6_type.attr,
482 &iio_scan_el_in9min8.dev_attr.attr, &dev_attr_in9min8_type.attr,
483 &iio_scan_el_in11min10.dev_attr.attr, &dev_attr_in11min10_type.attr,
487 static struct attribute_group max1238_scan_el_group = {
488 .name = "scan_elements",
489 .attrs = max1238_scan_el_attrs,
493 static const enum max1363_modes max11607_mode_list[] = {
497 d0m1, d2m3, d1m0, d3m2,
498 d0m1to2m3, d1m0to3m2,
501 static const enum max1363_modes max11608_mode_list[] = {
502 _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
503 s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
505 d0m1, d2m3, d4m5, d6m7,
506 d1m0, d3m2, d5m4, d7m6,
507 d0m1to2m3, d0m1to4m5, d0m1to6m7,
508 d1m0to3m2, d1m0to5m4, d1m0to7m6,
511 static struct attribute *max11608_device_attrs[] = {
512 &iio_dev_attr_in0_raw.dev_attr.attr,
513 &iio_dev_attr_in1_raw.dev_attr.attr,
514 &iio_dev_attr_in2_raw.dev_attr.attr,
515 &iio_dev_attr_in3_raw.dev_attr.attr,
516 &iio_dev_attr_in4_raw.dev_attr.attr,
517 &iio_dev_attr_in5_raw.dev_attr.attr,
518 &iio_dev_attr_in6_raw.dev_attr.attr,
519 &iio_dev_attr_in7_raw.dev_attr.attr,
520 &iio_dev_attr_in0min1_raw.dev_attr.attr,
521 &iio_dev_attr_in2min3_raw.dev_attr.attr,
522 &iio_dev_attr_in4min5_raw.dev_attr.attr,
523 &iio_dev_attr_in6min7_raw.dev_attr.attr,
524 &iio_dev_attr_in1min0_raw.dev_attr.attr,
525 &iio_dev_attr_in3min2_raw.dev_attr.attr,
526 &iio_dev_attr_in5min4_raw.dev_attr.attr,
527 &iio_dev_attr_in7min6_raw.dev_attr.attr,
528 &iio_dev_attr_name.dev_attr.attr,
529 &iio_dev_attr_in_scale.dev_attr.attr,
533 static struct attribute_group max11608_dev_attr_group = {
534 .attrs = max11608_device_attrs,
537 static struct attribute *max11608_scan_el_attrs[] = {
538 &iio_scan_el_in0.dev_attr.attr, &dev_attr_in0_type.attr,
539 &iio_scan_el_in1.dev_attr.attr, &dev_attr_in1_type.attr,
540 &iio_scan_el_in2.dev_attr.attr, &dev_attr_in2_type.attr,
541 &iio_scan_el_in3.dev_attr.attr, &dev_attr_in3_type.attr,
542 &iio_scan_el_in4.dev_attr.attr, &dev_attr_in4_type.attr,
543 &iio_scan_el_in5.dev_attr.attr, &dev_attr_in5_type.attr,
544 &iio_scan_el_in6.dev_attr.attr, &dev_attr_in6_type.attr,
545 &iio_scan_el_in7.dev_attr.attr, &dev_attr_in7_type.attr,
546 &iio_scan_el_in0min1.dev_attr.attr, &dev_attr_in0min1_type.attr,
547 &iio_scan_el_in2min3.dev_attr.attr, &dev_attr_in2min3_type.attr,
548 &iio_scan_el_in4min5.dev_attr.attr, &dev_attr_in4min5_type.attr,
549 &iio_scan_el_in6min7.dev_attr.attr, &dev_attr_in6min7_type.attr,
550 &iio_scan_el_in1min0.dev_attr.attr, &dev_attr_in1min0_type.attr,
551 &iio_scan_el_in3min2.dev_attr.attr, &dev_attr_in3min2_type.attr,
552 &iio_scan_el_in5min4.dev_attr.attr, &dev_attr_in5min4_type.attr,
553 &iio_scan_el_in7min6.dev_attr.attr, &dev_attr_in7min6_type.attr,
556 static struct attribute_group max11608_scan_el_group = {
557 .name = "scan_elements",
558 .attrs = max11608_scan_el_attrs,
597 /* max1363 and max1368 tested - rest from data sheet */
598 static const struct max1363_chip_info max1363_chip_info_tbl[] = {
604 .mode_list = max1363_mode_list,
605 .num_modes = ARRAY_SIZE(max1363_mode_list),
606 .default_mode = s0to3,
607 .dev_attrs = &max1363_dev_attr_group,
608 .scan_attrs = &max1363_scan_el_group,
615 .mode_list = max1363_mode_list,
616 .num_modes = ARRAY_SIZE(max1363_mode_list),
617 .default_mode = s0to3,
618 .dev_attrs = &max1363_dev_attr_group,
619 .scan_attrs = &max1363_scan_el_group,
626 .mode_list = max1363_mode_list,
627 .num_modes = ARRAY_SIZE(max1363_mode_list),
628 .default_mode = s0to3,
629 .dev_attrs = &max1363_dev_attr_group,
630 .scan_attrs = &max1363_scan_el_group,
637 .mode_list = max1363_mode_list,
638 .num_modes = ARRAY_SIZE(max1363_mode_list),
639 .default_mode = s0to3,
640 .dev_attrs = &max1363_dev_attr_group,
641 .scan_attrs = &max1363_scan_el_group,
647 .mode_list = max1236_mode_list,
648 .num_modes = ARRAY_SIZE(max1236_mode_list),
649 .default_mode = s0to3,
650 .dev_attrs = &max1363_dev_attr_group,
651 .scan_attrs = &max1363_scan_el_group,
657 .mode_list = max1236_mode_list,
658 .num_modes = ARRAY_SIZE(max1236_mode_list),
659 .default_mode = s0to3,
660 .dev_attrs = &max1363_dev_attr_group,
661 .scan_attrs = &max1363_scan_el_group,
667 .mode_list = max1238_mode_list,
668 .num_modes = ARRAY_SIZE(max1238_mode_list),
669 .default_mode = s0to11,
670 .dev_attrs = &max1238_dev_attr_group,
671 .scan_attrs = &max1238_scan_el_group,
677 .mode_list = max1238_mode_list,
678 .num_modes = ARRAY_SIZE(max1238_mode_list),
679 .default_mode = s0to11,
680 .dev_attrs = &max1238_dev_attr_group,
681 .scan_attrs = &max1238_scan_el_group,
687 .mode_list = max1236_mode_list,
688 .num_modes = ARRAY_SIZE(max1236_mode_list),
689 .default_mode = s0to3,
690 .dev_attrs = &max1363_dev_attr_group,
691 .scan_attrs = &max1363_scan_el_group,
697 .mode_list = max1236_mode_list,
698 .num_modes = ARRAY_SIZE(max1236_mode_list),
699 .default_mode = s0to3,
700 .dev_attrs = &max1363_dev_attr_group,
701 .scan_attrs = &max1363_scan_el_group,
707 .mode_list = max1238_mode_list,
708 .num_modes = ARRAY_SIZE(max1238_mode_list),
709 .default_mode = s0to11,
710 .dev_attrs = &max1238_dev_attr_group,
711 .scan_attrs = &max1238_scan_el_group,
717 .mode_list = max1238_mode_list,
718 .num_modes = ARRAY_SIZE(max1238_mode_list),
719 .default_mode = s0to11,
720 .dev_attrs = &max1238_dev_attr_group,
721 .scan_attrs = &max1238_scan_el_group,
727 .mode_list = max1236_mode_list,
728 .num_modes = ARRAY_SIZE(max1236_mode_list),
729 .default_mode = s0to3,
730 .dev_attrs = &max1363_dev_attr_group,
731 .scan_attrs = &max1363_scan_el_group,
737 .mode_list = max1236_mode_list,
738 .num_modes = ARRAY_SIZE(max1236_mode_list),
739 .default_mode = s0to3,
740 .dev_attrs = &max1363_dev_attr_group,
741 .scan_attrs = &max1363_scan_el_group,
747 .mode_list = max1238_mode_list,
748 .num_modes = ARRAY_SIZE(max1238_mode_list),
749 .default_mode = s0to11,
750 .dev_attrs = &max1238_dev_attr_group,
751 .scan_attrs = &max1238_scan_el_group,
757 .mode_list = max1238_mode_list,
758 .num_modes = ARRAY_SIZE(max1238_mode_list),
759 .default_mode = s0to11,
760 .dev_attrs = &max1238_dev_attr_group,
761 .scan_attrs = &max1238_scan_el_group,
767 .mode_list = max11607_mode_list,
768 .num_modes = ARRAY_SIZE(max11607_mode_list),
769 .default_mode = s0to3,
770 .dev_attrs = &max1363_dev_attr_group,
771 .scan_attrs = &max1363_scan_el_group,
777 .mode_list = max11607_mode_list,
778 .num_modes = ARRAY_SIZE(max11607_mode_list),
779 .default_mode = s0to3,
780 .dev_attrs = &max1363_dev_attr_group,
781 .scan_attrs = &max1363_scan_el_group,
787 .mode_list = max11608_mode_list,
788 .num_modes = ARRAY_SIZE(max11608_mode_list),
789 .default_mode = s0to7,
790 .dev_attrs = &max11608_dev_attr_group,
791 .scan_attrs = &max11608_scan_el_group,
797 .mode_list = max11608_mode_list,
798 .num_modes = ARRAY_SIZE(max11608_mode_list),
799 .default_mode = s0to7,
800 .dev_attrs = &max11608_dev_attr_group,
801 .scan_attrs = &max11608_scan_el_group,
807 .mode_list = max1238_mode_list,
808 .num_modes = ARRAY_SIZE(max1238_mode_list),
809 .default_mode = s0to11,
810 .dev_attrs = &max1238_dev_attr_group,
811 .scan_attrs = &max1238_scan_el_group,
817 .mode_list = max1238_mode_list,
818 .num_modes = ARRAY_SIZE(max1238_mode_list),
819 .default_mode = s0to11,
820 .dev_attrs = &max1238_dev_attr_group,
821 .scan_attrs = &max1238_scan_el_group,
827 .mode_list = max11607_mode_list,
828 .num_modes = ARRAY_SIZE(max11607_mode_list),
829 .default_mode = s0to3,
830 .dev_attrs = &max1363_dev_attr_group,
831 .scan_attrs = &max1363_scan_el_group,
837 .mode_list = max11607_mode_list,
838 .num_modes = ARRAY_SIZE(max11607_mode_list),
839 .default_mode = s0to3,
840 .dev_attrs = &max1363_dev_attr_group,
841 .scan_attrs = &max1363_scan_el_group,
847 .mode_list = max11608_mode_list,
848 .num_modes = ARRAY_SIZE(max11608_mode_list),
849 .default_mode = s0to7,
850 .dev_attrs = &max11608_dev_attr_group,
851 .scan_attrs = &max11608_scan_el_group,
857 .mode_list = max11608_mode_list,
858 .num_modes = ARRAY_SIZE(max11608_mode_list),
859 .default_mode = s0to7,
860 .dev_attrs = &max11608_dev_attr_group,
861 .scan_attrs = &max11608_scan_el_group,
867 .mode_list = max1238_mode_list,
868 .num_modes = ARRAY_SIZE(max1238_mode_list),
869 .default_mode = s0to11,
870 .dev_attrs = &max1238_dev_attr_group,
871 .scan_attrs = &max1238_scan_el_group,
877 .mode_list = max1238_mode_list,
878 .num_modes = ARRAY_SIZE(max1238_mode_list),
879 .default_mode = s0to11,
880 .dev_attrs = &max1238_dev_attr_group,
881 .scan_attrs = &max1238_scan_el_group,
887 .mode_list = max11607_mode_list,
888 .num_modes = ARRAY_SIZE(max11607_mode_list),
889 .default_mode = s0to3,
890 .dev_attrs = &max1363_dev_attr_group,
891 .scan_attrs = &max1363_scan_el_group,
897 .mode_list = max11607_mode_list,
898 .num_modes = ARRAY_SIZE(max11607_mode_list),
899 .default_mode = s0to3,
900 .dev_attrs = &max1363_dev_attr_group,
901 .scan_attrs = &max1363_scan_el_group,
907 .mode_list = max11608_mode_list,
908 .num_modes = ARRAY_SIZE(max11608_mode_list),
909 .default_mode = s0to7,
910 .dev_attrs = &max11608_dev_attr_group,
911 .scan_attrs = &max11608_scan_el_group,
917 .mode_list = max11608_mode_list,
918 .num_modes = ARRAY_SIZE(max11608_mode_list),
919 .default_mode = s0to7,
920 .dev_attrs = &max11608_dev_attr_group,
921 .scan_attrs = &max11608_scan_el_group,
927 .mode_list = max1238_mode_list,
928 .num_modes = ARRAY_SIZE(max1238_mode_list),
929 .default_mode = s0to11,
930 .dev_attrs = &max1238_dev_attr_group,
931 .scan_attrs = &max1238_scan_el_group,
937 .mode_list = max1238_mode_list,
938 .num_modes = ARRAY_SIZE(max1238_mode_list),
939 .default_mode = s0to11,
940 .dev_attrs = &max1238_dev_attr_group,
941 .scan_attrs = &max1238_scan_el_group,
945 static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
946 8300, 4200, 2000, 1000 };
948 static ssize_t max1363_monitor_show_freq(struct device *dev,
949 struct device_attribute *attr,
952 struct iio_dev *dev_info = dev_get_drvdata(dev);
953 struct max1363_state *st = iio_dev_get_devdata(dev_info);
954 return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
957 static ssize_t max1363_monitor_store_freq(struct device *dev,
958 struct device_attribute *attr,
962 struct iio_dev *dev_info = dev_get_drvdata(dev);
963 struct max1363_state *st = iio_dev_get_devdata(dev_info);
968 ret = strict_strtoul(buf, 10, &val);
971 for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
972 if (val == max1363_monitor_speeds[i]) {
979 mutex_lock(&dev_info->mlock);
980 st->monitor_speed = i;
981 mutex_unlock(&dev_info->mlock);
986 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
987 max1363_monitor_show_freq,
988 max1363_monitor_store_freq);
990 static IIO_CONST_ATTR(sampling_frequency_available,
991 "133000 665000 33300 16600 8300 4200 2000 1000");
993 static ssize_t max1363_show_thresh(struct device *dev,
994 struct device_attribute *attr,
998 struct iio_dev *dev_info = dev_get_drvdata(dev);
999 struct max1363_state *st = iio_dev_get_devdata(dev_info);
1000 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1003 return sprintf(buf, "%d\n",
1004 st->thresh_high[this_attr->address]);
1006 return sprintf(buf, "%d\n",
1007 st->thresh_low[this_attr->address & 0x7]);
1010 static ssize_t max1363_show_thresh_low(struct device *dev,
1011 struct device_attribute *attr,
1014 return max1363_show_thresh(dev, attr, buf, false);
1017 static ssize_t max1363_show_thresh_high(struct device *dev,
1018 struct device_attribute *attr,
1021 return max1363_show_thresh(dev, attr, buf, true);
1024 static ssize_t max1363_store_thresh_unsigned(struct device *dev,
1025 struct device_attribute *attr,
1030 struct iio_dev *dev_info = dev_get_drvdata(dev);
1031 struct max1363_state *st = iio_dev_get_devdata(dev_info);
1032 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1036 ret = strict_strtoul(buf, 10, &val);
1039 switch (st->chip_info->bits) {
1052 st->thresh_high[this_attr->address] = val;
1055 st->thresh_low[this_attr->address & 0x7] = val;
1062 static ssize_t max1363_store_thresh_high_unsigned(struct device *dev,
1063 struct device_attribute *attr,
1067 return max1363_store_thresh_unsigned(dev, attr, buf, len, true);
1070 static ssize_t max1363_store_thresh_low_unsigned(struct device *dev,
1071 struct device_attribute *attr,
1075 return max1363_store_thresh_unsigned(dev, attr, buf, len, false);
1078 static ssize_t max1363_store_thresh_signed(struct device *dev,
1079 struct device_attribute *attr,
1084 struct iio_dev *dev_info = dev_get_drvdata(dev);
1085 struct max1363_state *st = iio_dev_get_devdata(dev_info);
1086 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1090 ret = strict_strtol(buf, 10, &val);
1093 switch (st->chip_info->bits) {
1095 if (val < -512 || val > 511)
1099 if (val < -2048 || val > 2047)
1106 st->thresh_high[this_attr->address] = val;
1109 st->thresh_low[this_attr->address & 0x7] = val;
1116 static ssize_t max1363_store_thresh_high_signed(struct device *dev,
1117 struct device_attribute *attr,
1121 return max1363_store_thresh_signed(dev, attr, buf, len, true);
1124 static ssize_t max1363_store_thresh_low_signed(struct device *dev,
1125 struct device_attribute *attr,
1129 return max1363_store_thresh_signed(dev, attr, buf, len, false);
1132 static IIO_DEVICE_ATTR(in0_thresh_high_value, S_IRUGO | S_IWUSR,
1133 max1363_show_thresh_high,
1134 max1363_store_thresh_high_unsigned, 0);
1135 static IIO_DEVICE_ATTR(in0_thresh_low_value, S_IRUGO | S_IWUSR,
1136 max1363_show_thresh_low,
1137 max1363_store_thresh_low_unsigned, 0);
1138 static IIO_DEVICE_ATTR(in1_thresh_high_value, S_IRUGO | S_IWUSR,
1139 max1363_show_thresh_high,
1140 max1363_store_thresh_high_unsigned, 1);
1141 static IIO_DEVICE_ATTR(in1_thresh_low_value, S_IRUGO | S_IWUSR,
1142 max1363_show_thresh_low,
1143 max1363_store_thresh_low_unsigned, 1);
1144 static IIO_DEVICE_ATTR(in2_thresh_high_value, S_IRUGO | S_IWUSR,
1145 max1363_show_thresh_high,
1146 max1363_store_thresh_high_unsigned, 2);
1147 static IIO_DEVICE_ATTR(in2_thresh_low_value, S_IRUGO | S_IWUSR,
1148 max1363_show_thresh_low,
1149 max1363_store_thresh_low_unsigned, 2);
1150 static IIO_DEVICE_ATTR(in3_thresh_high_value, S_IRUGO | S_IWUSR,
1151 max1363_show_thresh_high,
1152 max1363_store_thresh_high_unsigned, 3);
1153 static IIO_DEVICE_ATTR(in3_thresh_low_value, S_IRUGO | S_IWUSR,
1154 max1363_show_thresh_low,
1155 max1363_store_thresh_low_unsigned, 3);
1157 static IIO_DEVICE_ATTR_NAMED(in0min1_thresh_high_value,
1158 in0-in1_thresh_high_value,
1159 S_IRUGO | S_IWUSR, max1363_show_thresh_high,
1160 max1363_store_thresh_high_signed, 4);
1161 static IIO_DEVICE_ATTR_NAMED(in0min1_thresh_low_value,
1162 in0-in1_thresh_low_value,
1163 S_IRUGO | S_IWUSR, max1363_show_thresh_low,
1164 max1363_store_thresh_low_signed, 4);
1165 static IIO_DEVICE_ATTR_NAMED(in2min3_thresh_high_value,
1166 in2-in3_thresh_high_value,
1167 S_IRUGO | S_IWUSR, max1363_show_thresh_high,
1168 max1363_store_thresh_high_signed, 5);
1169 static IIO_DEVICE_ATTR_NAMED(in2min3_thresh_low_value,
1170 in2-in3_thresh_low_value,
1171 S_IRUGO | S_IWUSR, max1363_show_thresh_low,
1172 max1363_store_thresh_low_signed, 5);
1173 static IIO_DEVICE_ATTR_NAMED(in1min0_thresh_high_value,
1174 in1-in0_thresh_high_value,
1175 S_IRUGO | S_IWUSR, max1363_show_thresh_high,
1176 max1363_store_thresh_high_signed, 6);
1177 static IIO_DEVICE_ATTR_NAMED(in1min0_thresh_low_value,
1178 in1-in0_thresh_low_value,
1179 S_IRUGO | S_IWUSR, max1363_show_thresh_low,
1180 max1363_store_thresh_low_signed, 6);
1181 static IIO_DEVICE_ATTR_NAMED(in3min2_thresh_high_value,
1182 in3-in2_thresh_high_value,
1183 S_IRUGO | S_IWUSR, max1363_show_thresh_high,
1184 max1363_store_thresh_high_signed, 7);
1185 static IIO_DEVICE_ATTR_NAMED(in3min2_thresh_low_value,
1186 in3-in2_thresh_low_value,
1187 S_IRUGO | S_IWUSR, max1363_show_thresh_low,
1188 max1363_store_thresh_low_signed, 7);
1190 static int max1363_int_th(struct iio_dev *dev_info,
1195 struct max1363_state *st = dev_info->dev_data;
1197 st->last_timestamp = timestamp;
1198 schedule_work(&st->thresh_work);
1202 static void max1363_thresh_handler_bh(struct work_struct *work_s)
1204 struct max1363_state *st = container_of(work_s, struct max1363_state,
1207 u8 tx[2] = { st->setupbyte,
1208 MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
1210 i2c_master_recv(st->client, &rx, 1);
1212 iio_push_event(st->indio_dev, 0,
1213 IIO_EVENT_CODE_IN_LOW_THRESH(3),
1214 st->last_timestamp);
1216 iio_push_event(st->indio_dev, 0,
1217 IIO_EVENT_CODE_IN_HIGH_THRESH(3),
1218 st->last_timestamp);
1220 iio_push_event(st->indio_dev, 0,
1221 IIO_EVENT_CODE_IN_LOW_THRESH(2),
1222 st->last_timestamp);
1224 iio_push_event(st->indio_dev, 0,
1225 IIO_EVENT_CODE_IN_HIGH_THRESH(2),
1226 st->last_timestamp);
1228 iio_push_event(st->indio_dev, 0,
1229 IIO_EVENT_CODE_IN_LOW_THRESH(1),
1230 st->last_timestamp);
1232 iio_push_event(st->indio_dev, 0,
1233 IIO_EVENT_CODE_IN_HIGH_THRESH(1),
1234 st->last_timestamp);
1236 iio_push_event(st->indio_dev, 0,
1237 IIO_EVENT_CODE_IN_LOW_THRESH(0),
1238 st->last_timestamp);
1240 iio_push_event(st->indio_dev, 0,
1241 IIO_EVENT_CODE_IN_HIGH_THRESH(0),
1242 st->last_timestamp);
1243 enable_irq(st->client->irq);
1244 i2c_master_send(st->client, tx, 2);
1247 static ssize_t max1363_read_interrupt_config(struct device *dev,
1248 struct device_attribute *attr,
1251 struct iio_dev *dev_info = dev_get_drvdata(dev);
1252 struct max1363_state *st = iio_dev_get_devdata(dev_info);
1253 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1256 mutex_lock(&dev_info->mlock);
1257 if (this_attr->mask & 0x8)
1258 val = (1 << (this_attr->mask & 0x7)) & st->mask_low;
1260 val = (1 << this_attr->mask) & st->mask_high;
1261 mutex_unlock(&dev_info->mlock);
1263 return sprintf(buf, "%d\n", !!val);
1266 static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
1270 unsigned long numelements;
1275 /* transition to ring capture is not currently supported */
1276 st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
1277 st->configbyte &= ~MAX1363_SCAN_MASK;
1278 st->monitor_on = false;
1279 return max1363_write_basic_config(st->client,
1284 /* Ensure we are in the relevant mode */
1285 st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
1286 st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
1288 | MAX1363_SE_DE_MASK);
1289 st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
1290 if ((st->mask_low | st->mask_high) & 0x0F) {
1291 st->configbyte |= max1363_mode_table[s0to3].conf;
1292 modemask = max1363_mode_table[s0to3].modemask;
1293 } else if ((st->mask_low | st->mask_high) & 0x30) {
1294 st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
1295 modemask = max1363_mode_table[d0m1to2m3].modemask;
1297 st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
1298 modemask = max1363_mode_table[d1m0to3m2].modemask;
1300 numelements = hweight_long(modemask);
1301 len = 3 * numelements + 3;
1302 tx_buf = kmalloc(len, GFP_KERNEL);
1307 tx_buf[0] = st->configbyte;
1308 tx_buf[1] = st->setupbyte;
1309 tx_buf[2] = (st->monitor_speed << 1);
1312 * So we need to do yet another bit of nefarious scan mode
1313 * setup to match what we need.
1315 for (j = 0; j < 8; j++)
1316 if (modemask & (1 << j)) {
1317 /* Establish the mode is in the scan */
1318 if (st->mask_low & (1 << j)) {
1319 tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
1320 tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
1328 if (st->mask_high & (1 << j)) {
1330 (st->thresh_high[j] >> 8) & 0x0F;
1331 tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
1333 tx_buf[i + 1] |= 0x0F;
1334 tx_buf[i + 2] = 0xFF;
1336 tx_buf[i + 1] |= 0x07;
1337 tx_buf[i + 2] = 0xFF;
1343 ret = i2c_master_send(st->client, tx_buf, len);
1352 * Now that we hopefully have sensible thresholds in place it is
1353 * time to turn the interrupts on.
1354 * It is unclear from the data sheet if this should be necessary
1355 * (i.e. whether monitor mode setup is atomic) but it appears to
1358 tx_buf[0] = st->setupbyte;
1359 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
1360 ret = i2c_master_send(st->client, tx_buf, 2);
1368 st->monitor_on = true;
1377 * To keep this managable we always use one of 3 scan modes.
1378 * Scan 0...3, 0-1,2-3 and 1-0,3-2
1380 static inline int __max1363_check_event_mask(int thismask, int checkmask)
1383 /* Is it unipolar */
1385 if (checkmask & ~0x0F) {
1389 } else if (thismask < 6) {
1390 if (checkmask & ~0x30) {
1394 } else if (checkmask & ~0xC0)
1400 static ssize_t max1363_write_interrupt_config(struct device *dev,
1401 struct device_attribute *attr,
1405 struct iio_dev *dev_info = dev_get_drvdata(dev);
1406 struct max1363_state *st = iio_dev_get_devdata(dev_info);
1407 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
1411 ret = strict_strtoul(buf, 10, &val);
1414 mutex_lock(&st->indio_dev->mlock);
1415 unifiedmask = st->mask_low | st->mask_high;
1416 if (this_attr->mask & 0x08) {
1417 /* If we are disabling no need to test */
1419 st->mask_low &= ~(1 << (this_attr->mask & 0x7));
1421 ret = __max1363_check_event_mask(this_attr->mask & 0x7,
1425 st->mask_low |= (1 << (this_attr->mask & 0x7));
1429 st->mask_high &= ~(1 << (this_attr->mask));
1431 ret = __max1363_check_event_mask(this_attr->mask,
1435 st->mask_high |= (1 << this_attr->mask);
1438 if (st->monitor_on && !st->mask_high && !st->mask_low)
1439 iio_remove_event_from_list(this_attr->listel,
1440 &dev_info->interrupts[0]->ev_list);
1441 if (!st->monitor_on && val)
1442 iio_add_event_to_list(this_attr->listel,
1443 &dev_info->interrupts[0]->ev_list);
1445 max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
1447 mutex_unlock(&st->indio_dev->mlock);
1452 IIO_EVENT_SH(max1363_thresh, max1363_int_th);
1454 #define MAX1363_HIGH_THRESH(a) a
1455 #define MAX1363_LOW_THRESH(a) (a | 0x8)
1457 IIO_EVENT_ATTR_SH(in0_thresh_high_en,
1458 iio_event_max1363_thresh,
1459 max1363_read_interrupt_config,
1460 max1363_write_interrupt_config,
1461 MAX1363_HIGH_THRESH(0));
1463 IIO_EVENT_ATTR_SH(in0_thresh_low_en,
1464 iio_event_max1363_thresh,
1465 max1363_read_interrupt_config,
1466 max1363_write_interrupt_config,
1467 MAX1363_LOW_THRESH(0));
1469 IIO_EVENT_ATTR_SH(in1_thresh_high_en,
1470 iio_event_max1363_thresh,
1471 max1363_read_interrupt_config,
1472 max1363_write_interrupt_config,
1473 MAX1363_HIGH_THRESH(1));
1475 IIO_EVENT_ATTR_SH(in1_thresh_low_en,
1476 iio_event_max1363_thresh,
1477 max1363_read_interrupt_config,
1478 max1363_write_interrupt_config,
1479 MAX1363_LOW_THRESH(1));
1481 IIO_EVENT_ATTR_SH(in2_thresh_high_en,
1482 iio_event_max1363_thresh,
1483 max1363_read_interrupt_config,
1484 max1363_write_interrupt_config,
1485 MAX1363_HIGH_THRESH(2));
1487 IIO_EVENT_ATTR_SH(in2_thresh_low_en,
1488 iio_event_max1363_thresh,
1489 max1363_read_interrupt_config,
1490 max1363_write_interrupt_config,
1491 MAX1363_LOW_THRESH(2));
1493 IIO_EVENT_ATTR_SH(in3_thresh_high_en,
1494 iio_event_max1363_thresh,
1495 max1363_read_interrupt_config,
1496 max1363_write_interrupt_config,
1497 MAX1363_HIGH_THRESH(3));
1499 IIO_EVENT_ATTR_SH(in3_thresh_low_en,
1500 iio_event_max1363_thresh,
1501 max1363_read_interrupt_config,
1502 max1363_write_interrupt_config,
1503 MAX1363_LOW_THRESH(3));
1505 IIO_EVENT_ATTR_NAMED_SH(in0min1_thresh_high_en,
1506 in0-in1_thresh_high_en,
1507 iio_event_max1363_thresh,
1508 max1363_read_interrupt_config,
1509 max1363_write_interrupt_config,
1510 MAX1363_HIGH_THRESH(4));
1512 IIO_EVENT_ATTR_NAMED_SH(in0min1_thresh_low_en,
1513 in0-in1_thresh_low_en,
1514 iio_event_max1363_thresh,
1515 max1363_read_interrupt_config,
1516 max1363_write_interrupt_config,
1517 MAX1363_LOW_THRESH(4));
1519 IIO_EVENT_ATTR_NAMED_SH(in3min2_thresh_high_en,
1520 in3-in2_thresh_high_en,
1521 iio_event_max1363_thresh,
1522 max1363_read_interrupt_config,
1523 max1363_write_interrupt_config,
1524 MAX1363_HIGH_THRESH(5));
1526 IIO_EVENT_ATTR_NAMED_SH(in3min2_thresh_low_en,
1527 in3-in2_thresh_low_en,
1528 iio_event_max1363_thresh,
1529 max1363_read_interrupt_config,
1530 max1363_write_interrupt_config,
1531 MAX1363_LOW_THRESH(5));
1533 IIO_EVENT_ATTR_NAMED_SH(in1min0_thresh_high_en,
1534 in1-in0_thresh_high_en,
1535 iio_event_max1363_thresh,
1536 max1363_read_interrupt_config,
1537 max1363_write_interrupt_config,
1538 MAX1363_HIGH_THRESH(6));
1540 IIO_EVENT_ATTR_NAMED_SH(in1min0_thresh_low_en,
1541 in1-in0_thresh_low_en,
1542 iio_event_max1363_thresh,
1543 max1363_read_interrupt_config,
1544 max1363_write_interrupt_config,
1545 MAX1363_LOW_THRESH(6));
1547 IIO_EVENT_ATTR_NAMED_SH(in2min3_thresh_high_en,
1548 in2-in3_thresh_high_en,
1549 iio_event_max1363_thresh,
1550 max1363_read_interrupt_config,
1551 max1363_write_interrupt_config,
1552 MAX1363_HIGH_THRESH(7));
1554 IIO_EVENT_ATTR_NAMED_SH(in2min3_thresh_low_en,
1555 in2-in3_thresh_low_en,
1556 iio_event_max1363_thresh,
1557 max1363_read_interrupt_config,
1558 max1363_write_interrupt_config,
1559 MAX1363_LOW_THRESH(7));
1562 * As with scan_elements, only certain sets of these can
1565 static struct attribute *max1363_event_attributes[] = {
1566 &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
1567 &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
1568 &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
1569 &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
1570 &iio_dev_attr_in2_thresh_high_value.dev_attr.attr,
1571 &iio_dev_attr_in2_thresh_low_value.dev_attr.attr,
1572 &iio_dev_attr_in3_thresh_high_value.dev_attr.attr,
1573 &iio_dev_attr_in3_thresh_low_value.dev_attr.attr,
1574 &iio_dev_attr_in0min1_thresh_high_value.dev_attr.attr,
1575 &iio_dev_attr_in0min1_thresh_low_value.dev_attr.attr,
1576 &iio_dev_attr_in2min3_thresh_high_value.dev_attr.attr,
1577 &iio_dev_attr_in2min3_thresh_low_value.dev_attr.attr,
1578 &iio_dev_attr_in1min0_thresh_high_value.dev_attr.attr,
1579 &iio_dev_attr_in1min0_thresh_low_value.dev_attr.attr,
1580 &iio_dev_attr_in3min2_thresh_high_value.dev_attr.attr,
1581 &iio_dev_attr_in3min2_thresh_low_value.dev_attr.attr,
1582 &iio_dev_attr_sampling_frequency.dev_attr.attr,
1583 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1584 &iio_event_attr_in0_thresh_high_en.dev_attr.attr,
1585 &iio_event_attr_in0_thresh_low_en.dev_attr.attr,
1586 &iio_event_attr_in1_thresh_high_en.dev_attr.attr,
1587 &iio_event_attr_in1_thresh_low_en.dev_attr.attr,
1588 &iio_event_attr_in2_thresh_high_en.dev_attr.attr,
1589 &iio_event_attr_in2_thresh_low_en.dev_attr.attr,
1590 &iio_event_attr_in3_thresh_high_en.dev_attr.attr,
1591 &iio_event_attr_in3_thresh_low_en.dev_attr.attr,
1592 &iio_event_attr_in0min1_thresh_high_en.dev_attr.attr,
1593 &iio_event_attr_in0min1_thresh_low_en.dev_attr.attr,
1594 &iio_event_attr_in3min2_thresh_high_en.dev_attr.attr,
1595 &iio_event_attr_in3min2_thresh_low_en.dev_attr.attr,
1596 &iio_event_attr_in1min0_thresh_high_en.dev_attr.attr,
1597 &iio_event_attr_in1min0_thresh_low_en.dev_attr.attr,
1598 &iio_event_attr_in2min3_thresh_high_en.dev_attr.attr,
1599 &iio_event_attr_in2min3_thresh_low_en.dev_attr.attr,
1603 static struct attribute_group max1363_event_attribute_group = {
1604 .attrs = max1363_event_attributes,
1607 static int max1363_initial_setup(struct max1363_state *st)
1609 st->setupbyte = MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD
1610 | MAX1363_SETUP_POWER_UP_INT_REF
1611 | MAX1363_SETUP_INT_CLOCK
1612 | MAX1363_SETUP_UNIPOLAR
1613 | MAX1363_SETUP_NORESET;
1615 /* Set scan mode writes the config anyway so wait until then*/
1616 st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
1617 st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
1618 st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
1620 return max1363_set_scan_mode(st);
1623 static int __devinit max1363_probe(struct i2c_client *client,
1624 const struct i2c_device_id *id)
1626 int ret, i, regdone = 0;
1627 struct max1363_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
1633 /* this is only used for device removal purposes */
1634 i2c_set_clientdata(client, st);
1636 atomic_set(&st->protect_ring, 0);
1638 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
1639 st->reg = regulator_get(&client->dev, "vcc");
1640 if (!IS_ERR(st->reg)) {
1641 ret = regulator_enable(st->reg);
1645 st->client = client;
1647 st->indio_dev = iio_allocate_device();
1648 if (st->indio_dev == NULL) {
1650 goto error_disable_reg;
1653 st->indio_dev->available_scan_masks
1654 = kzalloc(sizeof(*st->indio_dev->available_scan_masks)*
1655 (st->chip_info->num_modes + 1), GFP_KERNEL);
1656 if (!st->indio_dev->available_scan_masks) {
1658 goto error_free_device;
1661 for (i = 0; i < st->chip_info->num_modes; i++)
1662 st->indio_dev->available_scan_masks[i] =
1663 max1363_mode_table[st->chip_info->mode_list[i]]
1665 /* Estabilish that the iio_dev is a child of the i2c device */
1666 st->indio_dev->dev.parent = &client->dev;
1667 st->indio_dev->attrs = st->chip_info->dev_attrs;
1669 /* Todo: this shouldn't be here. */
1670 st->indio_dev->dev_data = (void *)(st);
1671 st->indio_dev->driver_module = THIS_MODULE;
1672 st->indio_dev->modes = INDIO_DIRECT_MODE;
1673 if (st->chip_info->monitor_mode && client->irq) {
1674 st->indio_dev->num_interrupt_lines = 1;
1675 st->indio_dev->event_attrs
1676 = &max1363_event_attribute_group;
1679 ret = max1363_initial_setup(st);
1681 goto error_free_available_scan_masks;
1683 ret = max1363_register_ring_funcs_and_init(st->indio_dev);
1685 goto error_free_available_scan_masks;
1687 ret = iio_device_register(st->indio_dev);
1689 goto error_cleanup_ring;
1691 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
1693 goto error_cleanup_ring;
1695 if (st->chip_info->monitor_mode && client->irq) {
1696 ret = iio_register_interrupt_line(client->irq,
1699 IRQF_TRIGGER_RISING,
1702 goto error_uninit_ring;
1704 INIT_WORK(&st->thresh_work, max1363_thresh_handler_bh);
1709 iio_ring_buffer_unregister(st->indio_dev->ring);
1711 max1363_ring_cleanup(st->indio_dev);
1712 error_free_available_scan_masks:
1713 kfree(st->indio_dev->available_scan_masks);
1716 iio_free_device(st->indio_dev);
1718 iio_device_unregister(st->indio_dev);
1720 if (!IS_ERR(st->reg))
1721 regulator_disable(st->reg);
1723 if (!IS_ERR(st->reg))
1724 regulator_put(st->reg);
1731 static int max1363_remove(struct i2c_client *client)
1733 struct max1363_state *st = i2c_get_clientdata(client);
1734 struct iio_dev *indio_dev = st->indio_dev;
1736 if (st->chip_info->monitor_mode && client->irq)
1737 iio_unregister_interrupt_line(st->indio_dev, 0);
1738 iio_ring_buffer_unregister(indio_dev->ring);
1739 max1363_ring_cleanup(indio_dev);
1740 kfree(st->indio_dev->available_scan_masks);
1741 iio_device_unregister(indio_dev);
1742 if (!IS_ERR(st->reg)) {
1743 regulator_disable(st->reg);
1744 regulator_put(st->reg);
1751 static const struct i2c_device_id max1363_id[] = {
1752 { "max1361", max1361 },
1753 { "max1362", max1362 },
1754 { "max1363", max1363 },
1755 { "max1364", max1364 },
1756 { "max1036", max1036 },
1757 { "max1037", max1037 },
1758 { "max1038", max1038 },
1759 { "max1039", max1039 },
1760 { "max1136", max1136 },
1761 { "max1137", max1137 },
1762 { "max1138", max1138 },
1763 { "max1139", max1139 },
1764 { "max1236", max1236 },
1765 { "max1237", max1237 },
1766 { "max1238", max1238 },
1767 { "max1239", max1239 },
1768 { "max11600", max11600 },
1769 { "max11601", max11601 },
1770 { "max11602", max11602 },
1771 { "max11603", max11603 },
1772 { "max11604", max11604 },
1773 { "max11605", max11605 },
1774 { "max11606", max11606 },
1775 { "max11607", max11607 },
1776 { "max11608", max11608 },
1777 { "max11609", max11609 },
1778 { "max11610", max11610 },
1779 { "max11611", max11611 },
1780 { "max11612", max11612 },
1781 { "max11613", max11613 },
1782 { "max11614", max11614 },
1783 { "max11615", max11615 },
1784 { "max11616", max11616 },
1785 { "max11617", max11617 },
1789 MODULE_DEVICE_TABLE(i2c, max1363_id);
1791 static struct i2c_driver max1363_driver = {
1795 .probe = max1363_probe,
1796 .remove = max1363_remove,
1797 .id_table = max1363_id,
1800 static __init int max1363_init(void)
1802 return i2c_add_driver(&max1363_driver);
1805 static __exit void max1363_exit(void)
1807 i2c_del_driver(&max1363_driver);
1810 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1811 MODULE_DESCRIPTION("Maxim 1363 ADC");
1812 MODULE_LICENSE("GPL v2");
1814 module_init(max1363_init);
1815 module_exit(max1363_exit);