Skip to content

Commit ccc2372

Browse files
committed
iio: adc: Add initial support for MAX22531 ADC
Add device support for MAX22530-MAX22531. Implement scale and read functionality for raw/filtered ADC readings. Signed-off-by: Abhinav Jain <jain.abhinav177@gmail.com>
1 parent 0091ba7 commit ccc2372

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13058,6 +13058,7 @@ L: linux-iio@vger.kernel.org
1305813058
S: Maintained
1305913059
W: https://ez.analog.com/linux-software-drivers
1306013060
F: Documentation/devicetree/bindings/iio/adc/adi,max22531.yaml
13061+
F: drivers/iio/adc/max22531.c
1306113062

1306213063
MAX31827 TEMPERATURE SWITCH DRIVER
1306313064
M: Daniel Matyas <daniel.matyas@analog.com>

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 MAX22531
1130+
tristate "Analog Devices MAX22531 ADC Driver"
1131+
depends on SPI
1132+
help
1133+
Say yes here to build support for field-side self-powered 12-bit
1134+
isolated Maxim ADCs. (max22530, max22531, max22532).
1135+
1136+
To compile this driver as a module, choose M here: the module will be
1137+
called max22531.
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_MAX22531) += max22531.o
159160
obj-$(CONFIG_MAX77541_ADC) += max77541-adc.o
160161
obj-$(CONFIG_MAX9611) += max9611.o
161162
obj-$(CONFIG_MCP320X) += mcp320x.o

drivers/iio/adc/max22531.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* MAX22531 SPI ADC Driver
4+
*
5+
* Copyright (C) 2025 Abhinav Jain
6+
*
7+
* Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max22530-max22532.pdf
8+
*/
9+
10+
#include <linux/module.h>
11+
#include <asm/unaligned.h>
12+
#include <linux/spi/spi.h>
13+
#include <linux/iio/iio.h>
14+
#include <linux/regulator/consumer.h>
15+
16+
#define MAX22531_REG_PROD_ID 0x00
17+
#define MAX22531_REG_ADC_CHAN(x) ((x) + 1)
18+
#define MAX22531_REG_FADC_CHAN(x) ((x) + 1)
19+
20+
#define MAX22531_VREF_MV 1800
21+
#define MAX22531_DEVICE_REV_MSK GENMASK(6, 0)
22+
#define MAX22531_DEVICE_REV 0x01
23+
24+
#define MAX22531_REG_ADDR_MASK GENMASK(7, 2)
25+
#define MAX22531_REG_WRITE_MASK BIT(1)
26+
27+
enum max22531_id {
28+
max22530,
29+
max22531,
30+
max22532,
31+
};
32+
33+
struct max22531_chip_info {
34+
const char *name;
35+
};
36+
37+
static struct max22531_chip_info max22531_chip_info_tbl[] = {
38+
[max22530] = {
39+
.name = "max22530",
40+
},
41+
[max22531] = {
42+
.name = "max22531",
43+
},
44+
[max22532] = {
45+
.name = "max22532",
46+
},
47+
};
48+
49+
struct max22531 {
50+
struct spi_device *spi_dev;
51+
const struct max22531_chip_info *chip_info;
52+
};
53+
54+
#define MAX22531_CHANNEL(ch) \
55+
{ \
56+
.type = IIO_VOLTAGE, \
57+
.indexed = 1, \
58+
.channel = (ch), \
59+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
60+
BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
61+
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
62+
}
63+
64+
static const struct iio_chan_spec max22531_channels[] = {
65+
MAX22531_CHANNEL(0),
66+
MAX22531_CHANNEL(1),
67+
MAX22531_CHANNEL(2),
68+
MAX22531_CHANNEL(3),
69+
};
70+
71+
static int max22531_reg_read(struct max22531 *adc, unsigned int reg,
72+
unsigned int *readval)
73+
{
74+
u8 cmd;
75+
76+
cmd = FIELD_PREP(MAX22531_REG_ADDR_MASK, reg);
77+
*readval = spi_w8r16be(adc->spi_dev, cmd);
78+
if (*readval < 0)
79+
return *readval;
80+
81+
return 0;
82+
}
83+
84+
static int max22531_read_raw(struct iio_dev *indio_dev,
85+
struct iio_chan_spec const *chan,
86+
int *val, int *val2, long mask)
87+
{
88+
struct max22531 *adc = iio_priv(indio_dev);
89+
int ret;
90+
91+
switch (mask) {
92+
case IIO_CHAN_INFO_RAW:
93+
ret = max22531_reg_read(adc, MAX22531_REG_ADC_CHAN(chan->channel), val);
94+
if (ret)
95+
return ret;
96+
return IIO_VAL_INT;
97+
98+
case IIO_CHAN_INFO_AVERAGE_RAW:
99+
ret = max22531_reg_read(adc, MAX22531_REG_FADC_CHAN(chan->channel), val);
100+
if (ret)
101+
return ret;
102+
return IIO_VAL_INT;
103+
104+
case IIO_CHAN_INFO_SCALE:
105+
*val = MAX22531_VREF_MV;
106+
*val2 = 12;
107+
108+
return IIO_VAL_FRACTIONAL_LOG2;
109+
110+
default:
111+
return -EINVAL;
112+
}
113+
}
114+
115+
static const struct iio_info max22531_info = {
116+
.read_raw = max22531_read_raw,
117+
};
118+
119+
static int max22531_probe(struct spi_device *spi)
120+
{
121+
struct iio_dev *indio_dev;
122+
struct max22531 *adc;
123+
unsigned int prod_id;
124+
int ret;
125+
126+
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
127+
if (!indio_dev)
128+
return -ENOMEM;
129+
130+
adc = iio_priv(indio_dev);
131+
adc->spi_dev = spi;
132+
adc->chip_info = spi_get_device_match_data(spi);
133+
if (!adc->chip_info)
134+
return dev_err_probe(&spi->dev, -EINVAL, "no chip info\n");
135+
136+
indio_dev->name = adc->chip_info->name;
137+
indio_dev->info = &max22531_info;
138+
indio_dev->channels = max22531_channels;
139+
indio_dev->num_channels = ARRAY_SIZE(max22531_channels);
140+
141+
ret = devm_regulator_get_enable(&spi->dev, "vddl");
142+
if (ret)
143+
return dev_err_probe(&spi->dev, ret,
144+
"Failed to retrieve power logic supply.\n");
145+
146+
ret = devm_regulator_get_enable(&spi->dev, "vddpl");
147+
if (ret)
148+
return dev_err_probe(&spi->dev, ret,
149+
"Failed to retrieve isolated DC-DC supply.\n");
150+
151+
ret = max22531_reg_read(adc, MAX22531_REG_PROD_ID, &prod_id);
152+
if (ret ||
153+
FIELD_GET(MAX22531_DEVICE_REV_MSK, prod_id) != MAX22531_DEVICE_REV)
154+
dev_warn(&spi->dev, "PROD_ID verification failed\n");
155+
156+
return devm_iio_device_register(&spi->dev, indio_dev);
157+
}
158+
159+
static const struct spi_device_id max22531_id[] = {
160+
{ "max22530", (kernel_ulong_t)&max22531_chip_info_tbl[max22530] },
161+
{ "max22531", (kernel_ulong_t)&max22531_chip_info_tbl[max22531] },
162+
{ "max22532", (kernel_ulong_t)&max22531_chip_info_tbl[max22532] },
163+
{ }
164+
};
165+
MODULE_DEVICE_TABLE(spi, max22531_id);
166+
167+
static const struct of_device_id max22531_spi_of_id[] = {
168+
{ .compatible = "adi,max22530",
169+
.data = &max22531_chip_info_tbl[max22530], },
170+
{ .compatible = "adi,max22531",
171+
.data = &max22531_chip_info_tbl[max22531], },
172+
{ .compatible = "adi,max22532",
173+
.data = &max22531_chip_info_tbl[max22532], },
174+
{ }
175+
};
176+
MODULE_DEVICE_TABLE(of, max22531_spi_of_id);
177+
178+
static struct spi_driver max22531_driver = {
179+
.driver = {
180+
.name = "max22531",
181+
.of_match_table = max22531_spi_of_id,
182+
},
183+
.probe = max22531_probe,
184+
.id_table = max22531_id,
185+
};
186+
module_spi_driver(max22531_driver);
187+
188+
MODULE_AUTHOR("Abhinav Jain <jain.abhinav177@gmail.com>");
189+
MODULE_DESCRIPTION("MAX22531 ADC");
190+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)