25 #include <linux/device.h>
26 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/i2c.h>
31 #include <linux/slab.h>
33 #include <linux/module.h>
43 #define MAX1363_MODE_SINGLE(_num, _mask) { \
44 .conf = MAX1363_CHANNEL_SEL(_num) \
45 | MAX1363_CONFIG_SCAN_SINGLE_1 \
46 | MAX1363_CONFIG_SE, \
47 .modemask[0] = _mask, \
50 #define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
51 .conf = MAX1363_CHANNEL_SEL(_num) \
52 | MAX1363_CONFIG_SCAN_TO_CS \
53 | MAX1363_CONFIG_SE, \
54 .modemask[0] = _mask, \
58 #define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
59 .conf = MAX1363_CHANNEL_SEL(_num) \
60 | MAX1236_SCAN_MID_TO_CHANNEL \
61 | MAX1363_CONFIG_SE, \
62 .modemask[0] = _mask \
65 #define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
66 .conf = MAX1363_CHANNEL_SEL(_nump) \
67 | MAX1363_CONFIG_SCAN_SINGLE_1 \
68 | MAX1363_CONFIG_DE, \
69 .modemask[0] = _mask \
73 #define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
74 .conf = MAX1363_CHANNEL_SEL(_num) \
75 | MAX1363_CONFIG_SCAN_TO_CS \
76 | MAX1363_CONFIG_DE, \
77 .modemask[0] = _mask \
81 #define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
82 .conf = MAX1363_CHANNEL_SEL(_num) \
83 | MAX1236_SCAN_MID_TO_CHANNEL \
84 | MAX1363_CONFIG_SE, \
85 .modemask[0] = _mask \
88 static const struct max1363_mode max1363_mode_table[] = {
158 if (bitmap_subset(mask,
182 return max1363_write_basic_config(st->
client,
187 static int max1363_read_single_chan(
struct iio_dev *indio_dev,
206 if (st->
monitor_on || iio_buffer_enabled(indio_dev)) {
226 data = (
s32)(rxbuf[1]) | ((
s32)(rxbuf[0] & 0x0F)) << 8;
243 static int max1363_read_raw(
struct iio_dev *indio_dev,
253 ret = max1363_read_single_chan(indio_dev, chan, val, m);
282 #define MAX1363_EV_M \
283 (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) \
284 | IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
285 #define MAX1363_INFO_MASK (IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
286 IIO_CHAN_INFO_SCALE_SHARED_BIT)
287 #define MAX1363_CHAN_U(num, addr, si, bits, evmask) \
289 .type = IIO_VOLTAGE, \
293 .info_mask = MAX1363_INFO_MASK, \
294 .datasheet_name = "AIN"#num, \
298 .storagebits = (bits > 8) ? 16 : 8, \
299 .endianness = IIO_BE, \
302 .event_mask = evmask, \
306 #define MAX1363_CHAN_B(num, num2, addr, si, bits, evmask) \
308 .type = IIO_VOLTAGE, \
314 .info_mask = MAX1363_INFO_MASK, \
315 .datasheet_name = "AIN"#num"-AIN"#num2, \
319 .storagebits = (bits > 8) ? 16 : 8, \
320 .endianness = IIO_BE, \
323 .event_mask = evmask, \
326 #define MAX1363_4X_CHANS(bits, em) { \
327 MAX1363_CHAN_U(0, _s0, 0, bits, em), \
328 MAX1363_CHAN_U(1, _s1, 1, bits, em), \
329 MAX1363_CHAN_U(2, _s2, 2, bits, em), \
330 MAX1363_CHAN_U(3, _s3, 3, bits, em), \
331 MAX1363_CHAN_B(0, 1, d0m1, 4, bits, em), \
332 MAX1363_CHAN_B(2, 3, d2m3, 5, bits, em), \
333 MAX1363_CHAN_B(1, 0, d1m0, 6, bits, em), \
334 MAX1363_CHAN_B(3, 2, d3m2, 7, bits, em), \
335 IIO_CHAN_SOFT_TIMESTAMP(8) \
357 _s0,
_s1,
_s2,
_s3,
_s4,
_s5,
_s6,
_s7,
_s8,
_s9,
_s10,
_s11,
368 #define MAX1363_12X_CHANS(bits) { \
369 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
370 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
371 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
372 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
373 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
374 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
375 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
376 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
377 MAX1363_CHAN_U(8, _s8, 8, bits, 0), \
378 MAX1363_CHAN_U(9, _s9, 9, bits, 0), \
379 MAX1363_CHAN_U(10, _s10, 10, bits, 0), \
380 MAX1363_CHAN_U(11, _s11, 11, bits, 0), \
381 MAX1363_CHAN_B(0, 1, d0m1, 12, bits, 0), \
382 MAX1363_CHAN_B(2, 3, d2m3, 13, bits, 0), \
383 MAX1363_CHAN_B(4, 5, d4m5, 14, bits, 0), \
384 MAX1363_CHAN_B(6, 7, d6m7, 15, bits, 0), \
385 MAX1363_CHAN_B(8, 9, d8m9, 16, bits, 0), \
386 MAX1363_CHAN_B(10, 11, d10m11, 17, bits, 0), \
387 MAX1363_CHAN_B(1, 0, d1m0, 18, bits, 0), \
388 MAX1363_CHAN_B(3, 2, d3m2, 19, bits, 0), \
389 MAX1363_CHAN_B(5, 4, d5m4, 20, bits, 0), \
390 MAX1363_CHAN_B(7, 6, d7m6, 21, bits, 0), \
391 MAX1363_CHAN_B(9, 8, d9m8, 22, bits, 0), \
392 MAX1363_CHAN_B(11, 10, d11m10, 23, bits, 0), \
393 IIO_CHAN_SOFT_TIMESTAMP(24) \
417 #define MAX1363_8X_CHANS(bits) { \
418 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
419 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
420 MAX1363_CHAN_U(2, _s2, 2, bits, 0), \
421 MAX1363_CHAN_U(3, _s3, 3, bits, 0), \
422 MAX1363_CHAN_U(4, _s4, 4, bits, 0), \
423 MAX1363_CHAN_U(5, _s5, 5, bits, 0), \
424 MAX1363_CHAN_U(6, _s6, 6, bits, 0), \
425 MAX1363_CHAN_U(7, _s7, 7, bits, 0), \
426 MAX1363_CHAN_B(0, 1, d0m1, 8, bits, 0), \
427 MAX1363_CHAN_B(2, 3, d2m3, 9, bits, 0), \
428 MAX1363_CHAN_B(4, 5, d4m5, 10, bits, 0), \
429 MAX1363_CHAN_B(6, 7, d6m7, 11, bits, 0), \
430 MAX1363_CHAN_B(1, 0, d1m0, 12, bits, 0), \
431 MAX1363_CHAN_B(3, 2, d3m2, 13, bits, 0), \
432 MAX1363_CHAN_B(5, 4, d5m4, 14, bits, 0), \
433 MAX1363_CHAN_B(7, 6, d7m6, 15, bits, 0), \
434 IIO_CHAN_SOFT_TIMESTAMP(16) \
444 #define MAX1363_2X_CHANS(bits) { \
445 MAX1363_CHAN_U(0, _s0, 0, bits, 0), \
446 MAX1363_CHAN_U(1, _s1, 1, bits, 0), \
447 MAX1363_CHAN_B(0, 1, d0m1, 2, bits, 0), \
448 MAX1363_CHAN_B(1, 0, d1m0, 3, bits, 0), \
449 IIO_CHAN_SOFT_TIMESTAMP(4) \
495 static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
496 8300, 4200, 2000, 1000 };
506 static ssize_t max1363_monitor_store_freq(
struct device *dev,
511 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
520 for (i = 0; i <
ARRAY_SIZE(max1363_monitor_speeds); i++)
521 if (val == max1363_monitor_speeds[i]) {
536 max1363_monitor_show_freq,
537 max1363_monitor_store_freq);
540 "133000 665000 33300 16600 8300 4200 2000 1000");
542 static int max1363_read_thresh(
struct iio_dev *indio_dev,
554 static int max1363_write_thresh(
struct iio_dev *indio_dev,
583 static const u64 max1363_event_codes[] = {
602 static irqreturn_t max1363_event_handler(
int irq,
void *
private)
604 struct iio_dev *indio_dev =
private;
621 static
int max1363_read_event_config(
struct iio_dev *indio_dev,
642 unsigned long numelements;
644 const long *modemask;
651 return max1363_write_basic_config(st->
client,
664 modemask = max1363_mode_table[
s0to3].modemask;
667 modemask = max1363_mode_table[
d0m1to2m3].modemask;
670 modemask = max1363_mode_table[
d1m0to3m2].modemask;
673 len = 3 * numelements + 3;
687 for (
j = 0;
j < 8;
j++)
705 tx_buf[i + 1] |= 0x0F;
706 tx_buf[i + 2] = 0xFF;
708 tx_buf[i + 1] |= 0x07;
709 tx_buf[i + 2] = 0xFF;
753 static inline int __max1363_check_event_mask(
int thismask,
int checkmask)
758 if (checkmask & ~0x0F) {
762 }
else if (thismask < 6) {
763 if (checkmask & ~0x30) {
767 }
else if (checkmask & ~0xC0)
773 static int max1363_write_event_config(
struct iio_dev *indio_dev,
789 ret = __max1363_check_event_mask((1 << number),
799 ret = __max1363_check_event_mask((1 << number),
818 static struct attribute *max1363_event_attributes[] = {
819 &iio_dev_attr_sampling_frequency.dev_attr.attr,
820 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
825 .attrs = max1363_event_attributes,
829 #define MAX1363_EVENT_FUNCS \
832 static const struct iio_info max1238_info = {
833 .read_raw = &max1363_read_raw,
838 static const struct iio_info max1363_info = {
839 .read_event_value = &max1363_read_thresh,
840 .write_event_value = &max1363_write_thresh,
841 .read_event_config = &max1363_read_event_config,
842 .write_event_config = &max1363_write_event_config,
843 .read_raw = &max1363_read_raw,
846 .event_attrs = &max1363_event_attribute_group,
854 .mode_list = max1363_mode_list,
856 .default_mode =
s0to3,
857 .channels = max1361_channels,
859 .info = &max1363_info,
864 .mode_list = max1363_mode_list,
866 .default_mode =
s0to3,
867 .channels = max1361_channels,
869 .info = &max1363_info,
874 .mode_list = max1363_mode_list,
876 .default_mode =
s0to3,
877 .channels = max1363_channels,
879 .info = &max1363_info,
884 .mode_list = max1363_mode_list,
886 .default_mode =
s0to3,
887 .channels = max1363_channels,
889 .info = &max1363_info,
894 .mode_list = max1236_mode_list,
896 .default_mode =
s0to3,
897 .info = &max1238_info,
898 .channels = max1036_channels,
904 .mode_list = max1236_mode_list,
906 .default_mode =
s0to3,
907 .info = &max1238_info,
908 .channels = max1036_channels,
914 .mode_list = max1238_mode_list,
917 .info = &max1238_info,
918 .channels = max1038_channels,
924 .mode_list = max1238_mode_list,
927 .info = &max1238_info,
928 .channels = max1038_channels,
934 .mode_list = max1236_mode_list,
936 .default_mode =
s0to3,
937 .info = &max1238_info,
938 .channels = max1136_channels,
944 .mode_list = max1236_mode_list,
946 .default_mode =
s0to3,
947 .info = &max1238_info,
948 .channels = max1136_channels,
954 .mode_list = max1238_mode_list,
957 .info = &max1238_info,
958 .channels = max1138_channels,
964 .mode_list = max1238_mode_list,
967 .info = &max1238_info,
968 .channels = max1138_channels,
974 .mode_list = max1236_mode_list,
976 .default_mode =
s0to3,
977 .info = &max1238_info,
978 .channels = max1236_channels,
984 .mode_list = max1236_mode_list,
986 .default_mode =
s0to3,
987 .info = &max1238_info,
988 .channels = max1236_channels,
994 .mode_list = max1238_mode_list,
997 .info = &max1238_info,
998 .channels = max1238_channels,
1003 .int_vref_mv = 2048,
1004 .mode_list = max1238_mode_list,
1007 .info = &max1238_info,
1008 .channels = max1238_channels,
1009 .num_channels =
ARRAY_SIZE(max1238_channels),
1013 .int_vref_mv = 4096,
1014 .mode_list = max11607_mode_list,
1016 .default_mode =
s0to3,
1017 .info = &max1238_info,
1018 .channels = max1036_channels,
1019 .num_channels =
ARRAY_SIZE(max1036_channels),
1023 .int_vref_mv = 2048,
1024 .mode_list = max11607_mode_list,
1026 .default_mode =
s0to3,
1027 .info = &max1238_info,
1028 .channels = max1036_channels,
1029 .num_channels =
ARRAY_SIZE(max1036_channels),
1033 .int_vref_mv = 4096,
1034 .mode_list = max11608_mode_list,
1036 .default_mode =
s0to7,
1037 .info = &max1238_info,
1038 .channels = max11602_channels,
1039 .num_channels =
ARRAY_SIZE(max11602_channels),
1043 .int_vref_mv = 2048,
1044 .mode_list = max11608_mode_list,
1046 .default_mode =
s0to7,
1047 .info = &max1238_info,
1048 .channels = max11602_channels,
1049 .num_channels =
ARRAY_SIZE(max11602_channels),
1053 .int_vref_mv = 4098,
1054 .mode_list = max1238_mode_list,
1057 .info = &max1238_info,
1058 .channels = max1238_channels,
1059 .num_channels =
ARRAY_SIZE(max1238_channels),
1063 .int_vref_mv = 2048,
1064 .mode_list = max1238_mode_list,
1067 .info = &max1238_info,
1068 .channels = max1238_channels,
1069 .num_channels =
ARRAY_SIZE(max1238_channels),
1073 .int_vref_mv = 4096,
1074 .mode_list = max11607_mode_list,
1076 .default_mode =
s0to3,
1077 .info = &max1238_info,
1078 .channels = max1136_channels,
1079 .num_channels =
ARRAY_SIZE(max1136_channels),
1083 .int_vref_mv = 2048,
1084 .mode_list = max11607_mode_list,
1086 .default_mode =
s0to3,
1087 .info = &max1238_info,
1088 .channels = max1136_channels,
1089 .num_channels =
ARRAY_SIZE(max1136_channels),
1093 .int_vref_mv = 4096,
1094 .mode_list = max11608_mode_list,
1096 .default_mode =
s0to7,
1097 .info = &max1238_info,
1098 .channels = max11608_channels,
1099 .num_channels =
ARRAY_SIZE(max11608_channels),
1103 .int_vref_mv = 2048,
1104 .mode_list = max11608_mode_list,
1106 .default_mode =
s0to7,
1107 .info = &max1238_info,
1108 .channels = max11608_channels,
1109 .num_channels =
ARRAY_SIZE(max11608_channels),
1113 .int_vref_mv = 4098,
1114 .mode_list = max1238_mode_list,
1117 .info = &max1238_info,
1118 .channels = max1238_channels,
1119 .num_channels =
ARRAY_SIZE(max1238_channels),
1123 .int_vref_mv = 2048,
1124 .mode_list = max1238_mode_list,
1127 .info = &max1238_info,
1128 .channels = max1238_channels,
1129 .num_channels =
ARRAY_SIZE(max1238_channels),
1133 .int_vref_mv = 4096,
1134 .mode_list = max11607_mode_list,
1136 .default_mode =
s0to3,
1137 .info = &max1238_info,
1138 .channels = max1363_channels,
1139 .num_channels =
ARRAY_SIZE(max1363_channels),
1143 .int_vref_mv = 2048,
1144 .mode_list = max11607_mode_list,
1146 .default_mode =
s0to3,
1147 .info = &max1238_info,
1148 .channels = max1363_channels,
1149 .num_channels =
ARRAY_SIZE(max1363_channels),
1153 .int_vref_mv = 4096,
1154 .mode_list = max11608_mode_list,
1156 .default_mode =
s0to7,
1157 .info = &max1238_info,
1158 .channels = max11614_channels,
1159 .num_channels =
ARRAY_SIZE(max11614_channels),
1163 .int_vref_mv = 2048,
1164 .mode_list = max11608_mode_list,
1166 .default_mode =
s0to7,
1167 .info = &max1238_info,
1168 .channels = max11614_channels,
1169 .num_channels =
ARRAY_SIZE(max11614_channels),
1173 .int_vref_mv = 4098,
1174 .mode_list = max1238_mode_list,
1177 .info = &max1238_info,
1178 .channels = max1238_channels,
1179 .num_channels =
ARRAY_SIZE(max1238_channels),
1183 .int_vref_mv = 2048,
1184 .mode_list = max1238_mode_list,
1187 .info = &max1238_info,
1188 .channels = max1238_channels,
1189 .num_channels =
ARRAY_SIZE(max1238_channels),
1193 .int_vref_mv = 2048,
1194 .mode_list = max11644_mode_list,
1196 .default_mode =
s0to1,
1197 .info = &max1238_info,
1198 .channels = max11644_channels,
1199 .num_channels =
ARRAY_SIZE(max11644_channels),
1203 .int_vref_mv = 4096,
1204 .mode_list = max11644_mode_list,
1206 .default_mode =
s0to1,
1207 .info = &max1238_info,
1208 .channels = max11644_channels,
1209 .num_channels =
ARRAY_SIZE(max11644_channels),
1213 .int_vref_mv = 2048,
1214 .mode_list = max11644_mode_list,
1216 .default_mode =
s0to1,
1217 .info = &max1238_info,
1218 .channels = max11646_channels,
1219 .num_channels =
ARRAY_SIZE(max11646_channels),
1223 .int_vref_mv = 4096,
1224 .mode_list = max11644_mode_list,
1226 .default_mode =
s0to1,
1227 .info = &max1238_info,
1228 .channels = max11646_channels,
1229 .num_channels =
ARRAY_SIZE(max11646_channels),
1254 unsigned long *masks;
1262 for (i = 0; i < st->
chip_info->num_modes; i++)
1264 max1363_mode_table[st->
chip_info->mode_list[i]]
1291 if (indio_dev ==
NULL) {
1293 goto error_disable_reg;
1297 goto error_free_device;
1298 st = iio_priv(indio_dev);
1301 i2c_set_clientdata(client, indio_dev);
1303 st->
chip_info = &max1363_chip_info_tbl[
id->driver_data];
1306 ret = max1363_alloc_scan_masks(indio_dev);
1308 goto error_unregister_map;
1311 indio_dev->
dev.parent = &client->
dev;
1312 indio_dev->
name =
id->name;
1319 ret = max1363_initial_setup(st);
1321 goto error_free_available_scan_masks;
1325 goto error_free_available_scan_masks;
1331 goto error_cleanup_ring;
1336 &max1363_event_handler,
1342 goto error_uninit_ring;
1347 goto error_free_irq;
1356 error_free_available_scan_masks:
1358 error_unregister_map:
1372 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1436 .probe = max1363_probe,
1438 .id_table = max1363_id,