Skip to content

Commit 18a129e

Browse files
iio: adc: Add basic support for MAX14001
The MAX14001/MAX14002 are configurable, isolated 10-bit ADCs for multi-range binary inputs. Besides the ADC readings, the MAX14001/MAX14002 offers more features, like a binary comparator, a filtered reading that can provide the average of the last 2, 4, or 8 ADC readings, and an inrush comparator that triggers the inrush current. There is also a fault feature that can diagnose seven possible fault conditions. And an option to select an external or internal ADC voltage reference. Add basic support for MAX14001/MAX14002 with the following features: - Raw ADC reading. - Filtered ADC average reading with the default configuration. Signed-off-by: Marilene Andrade Garcia <[email protected]>
1 parent 494c456 commit 18a129e

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,7 @@ L: [email protected]
15071507
S: Maintained
15081508
W: https://ez.analog.com/linux-software-drivers
15091509
F: Documentation/devicetree/bindings/iio/adc/adi,max14001.yaml
1510+
F: drivers/iio/adc/max14001.c
15101511

15111512
ANALOG DEVICES INC MAX31760 DRIVER
15121513
M: Ibrahim Tilki <[email protected]>

drivers/iio/adc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,16 @@ config MAX1363
11261126
To compile this driver as a module, choose M here: the module will be
11271127
called max1363.
11281128

1129+
config MAX14001
1130+
tristate "Analog Devices MAX14001/MAX14002 ADCs driver"
1131+
depends on SPI
1132+
help
1133+
Say yes here to build support for Analog Devices MAX14001/MAX14002
1134+
Configurable, Isolated 10-bit ADCs for Multi-Range Binary Inputs.
1135+
1136+
To compile this driver as a module, choose M here: the module will be
1137+
called max14001.
1138+
11291139
config MAX77541_ADC
11301140
tristate "Analog Devices MAX77541 ADC driver"
11311141
depends on MFD_MAX77541

drivers/iio/adc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ obj-$(CONFIG_MAX11205) += max11205.o
156156
obj-$(CONFIG_MAX11410) += max11410.o
157157
obj-$(CONFIG_MAX1241) += max1241.o
158158
obj-$(CONFIG_MAX1363) += max1363.o
159+
obj-$(CONFIG_MAX14001) += max14001.o
159160
obj-$(CONFIG_MAX77541_ADC) += max77541-adc.o
160161
obj-$(CONFIG_MAX9611) += max9611.o
161162
obj-$(CONFIG_MCP320X) += mcp320x.o

drivers/iio/adc/max14001.c

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* MAX14001/MAX14002 SPI ADC driver
4+
*
5+
* Copyright (c) 2025 Marilene Andrade Garcia <[email protected]>
6+
*
7+
* Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX14001-MAX14002.pdf
8+
*/
9+
10+
#include <asm/unaligned.h>
11+
#include <linux/bitfield.h>
12+
#include <linux/bitrev.h>
13+
#include <linux/module.h>
14+
#include <linux/spi/spi.h>
15+
#include <linux/iio/iio.h>
16+
#include <linux/regulator/consumer.h>
17+
18+
/* MAX14001 registers definition */
19+
#define MAX14001_REG_ADC 0x00
20+
#define MAX14001_REG_FADC 0x01
21+
#define MAX14001_REG_FLAGS 0x02
22+
#define MAX14001_REG_FLTEN 0x03
23+
#define MAX14001_REG_THL 0x04
24+
#define MAX14001_REG_THU 0x05
25+
#define MAX14001_REG_INRR 0x06
26+
#define MAX14001_REG_INRT 0x07
27+
#define MAX14001_REG_INRP 0x08
28+
#define MAX14001_REG_CFG 0x09
29+
#define MAX14001_REG_ENBL 0x0A
30+
#define MAX14001_REG_ACT 0x0B
31+
#define MAX14001_REG_WEN 0x0C
32+
33+
/* MAX14001 CONTROL values*/
34+
#define MAX14001_REG_WRITE 0x1
35+
#define MAX14001_REG_READ 0x0
36+
37+
/* MAX14001 MASKS */
38+
#define MAX14001_MASK_ADDR GENMASK(15, 11)
39+
#define MAX14001_MASK_WR BIT(10)
40+
#define MAX14001_MASK_DATA GENMASK(9, 0)
41+
42+
enum max14001_chip_model {
43+
max14001,
44+
max14002,
45+
};
46+
47+
struct max14001_chip_info {
48+
const char *name;
49+
};
50+
51+
struct max14001_state {
52+
const struct max14001_chip_info *chip_info;
53+
struct spi_device *spi;
54+
int vref_mv;
55+
56+
__be16 rx_buffer __aligned(IIO_DMA_MINALIGN);
57+
__be16 tx_buffer;
58+
};
59+
60+
static struct max14001_chip_info max14001_chip_info_tbl[] = {
61+
[max14001] = {
62+
.name = "max14001",
63+
},
64+
[max14002] = {
65+
.name = "max14002",
66+
},
67+
};
68+
69+
static int max14001_spi_read(struct max14001_state *st, u16 reg, int *val)
70+
{
71+
struct spi_transfer xfer[] = {
72+
{
73+
.tx_buf = &st->tx_buffer,
74+
.len = sizeof(st->tx_buffer),
75+
.bits_per_word = 16,
76+
.cs_change = 1,
77+
},
78+
{
79+
.rx_buf = &st->rx_buffer,
80+
.len = sizeof(st->rx_buffer),
81+
.bits_per_word = 16,
82+
},
83+
};
84+
int ret;
85+
86+
st->tx_buffer = FIELD_PREP(MAX14001_MASK_ADDR, reg) |
87+
FIELD_PREP(MAX14001_MASK_WR, MAX14001_REG_READ);
88+
st->tx_buffer = bitrev16(st->tx_buffer);
89+
90+
ret = spi_sync_transfer(st->spi, xfer, ARRAY_SIZE(xfer));
91+
if (ret < 0)
92+
return ret;
93+
94+
st->rx_buffer = bitrev16(st->rx_buffer);
95+
*val = FIELD_GET(MAX14001_MASK_DATA, st->rx_buffer);
96+
97+
return 0;
98+
}
99+
100+
static int max14001_read_raw(struct iio_dev *indio_dev,
101+
struct iio_chan_spec const *chan,
102+
int *val, int *val2, long mask)
103+
{
104+
struct max14001_state *st = iio_priv(indio_dev);
105+
int ret;
106+
107+
switch (mask) {
108+
case IIO_CHAN_INFO_RAW:
109+
ret = max14001_spi_read(st, MAX14001_REG_ADC, val);
110+
if (ret < 0)
111+
return ret;
112+
113+
return IIO_VAL_INT;
114+
case IIO_CHAN_INFO_AVERAGE_RAW:
115+
ret = max14001_spi_read(st, MAX14001_REG_FADC, val);
116+
if (ret < 0)
117+
return ret;
118+
119+
return IIO_VAL_INT;
120+
case IIO_CHAN_INFO_SCALE:
121+
*val = st->vref_mv;
122+
*val2 = 10;
123+
124+
return IIO_VAL_FRACTIONAL_LOG2;
125+
}
126+
127+
return -EINVAL;
128+
}
129+
130+
static const struct iio_info max14001_info = {
131+
.read_raw = max14001_read_raw,
132+
};
133+
134+
static const struct iio_chan_spec max14001_channel[] = {
135+
{
136+
.type = IIO_VOLTAGE,
137+
.indexed = 1,
138+
.channel = 0,
139+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
140+
BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
141+
BIT(IIO_CHAN_INFO_SCALE),
142+
}
143+
};
144+
145+
static int max14001_probe(struct spi_device *spi)
146+
{
147+
struct device *dev = &spi->dev;
148+
struct max14001_state *st;
149+
struct iio_dev *indio_dev;
150+
int ret;
151+
152+
indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
153+
if (!indio_dev)
154+
return -ENOMEM;
155+
156+
st = iio_priv(indio_dev);
157+
st->spi = spi;
158+
st->chip_info = spi_get_device_match_data(spi);
159+
if (!st->chip_info)
160+
return dev_err_probe(dev, -ENODEV, "Failed to get match data\n");
161+
162+
indio_dev->name = st->chip_info->name;
163+
indio_dev->modes = INDIO_DIRECT_MODE;
164+
indio_dev->info = &max14001_info;
165+
indio_dev->channels = max14001_channel;
166+
indio_dev->num_channels = ARRAY_SIZE(max14001_channel);
167+
168+
ret = devm_regulator_get_enable(dev, "vdd");
169+
if (ret)
170+
return dev_err_probe(dev, ret,
171+
"Failed to enable specified Vdd supply\n");
172+
173+
ret = devm_regulator_get_enable(dev, "vddl");
174+
if (ret)
175+
return dev_err_probe(dev, ret,
176+
"Failed to enable specified Vddl supply\n");
177+
178+
ret = devm_regulator_get_enable_read_voltage(dev, "vrefin");
179+
if (ret < 0)
180+
st->vref_mv = 1250000 / 1000;
181+
else
182+
st->vref_mv = ret / 1000;
183+
184+
return devm_iio_device_register(dev, indio_dev);
185+
}
186+
187+
static const struct spi_device_id max14001_id_table[] = {
188+
{ "max14001", (kernel_ulong_t)&max14001_chip_info_tbl[max14001] },
189+
{ "max14002", (kernel_ulong_t)&max14001_chip_info_tbl[max14002] },
190+
{}
191+
};
192+
MODULE_DEVICE_TABLE(spi, max14001_id_table);
193+
194+
static const struct of_device_id max14001_of_match[] = {
195+
{ .compatible = "adi,max14001",
196+
.data = &max14001_chip_info_tbl[max14001], },
197+
{ .compatible = "adi,max14002",
198+
.data = &max14001_chip_info_tbl[max14002], },
199+
{ }
200+
};
201+
MODULE_DEVICE_TABLE(of, max14001_of_match);
202+
203+
static struct spi_driver max14001_driver = {
204+
.driver = {
205+
.name = "max14001",
206+
.of_match_table = max14001_of_match,
207+
},
208+
.probe = max14001_probe,
209+
.id_table = max14001_id_table,
210+
};
211+
module_spi_driver(max14001_driver);
212+
213+
MODULE_AUTHOR("Marilene Andrade Garcia <[email protected]>");
214+
MODULE_DESCRIPTION("Analog Devices MAX14001/MAX14002 ADCs driver");
215+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)