Skip to content

Commit 8a80a71

Browse files
x64kjic23
authored andcommitted
iio: adc: Add MAX1241 driver
Add driver for the Maxim MAX1241 12-bit, single-channel ADC. Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-MAX1241.pdf Reviewed-by: Alexandru Ardelean <[email protected]> Signed-off-by: Alexandru Lazar <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent fc20a26 commit 8a80a71

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed

drivers/iio/adc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,16 @@ config MAX1118
630630
To compile this driver as a module, choose M here: the module will be
631631
called max1118.
632632

633+
config MAX1241
634+
tristate "Maxim max1241 ADC driver"
635+
depends on SPI_MASTER
636+
help
637+
Say yes here to build support for Maxim max1241 12-bit, single-channel
638+
ADC.
639+
640+
To compile this driver as a module, choose M here: the module will be
641+
called max1241.
642+
633643
config MAX1363
634644
tristate "Maxim max1363 ADC driver"
635645
depends on I2C

drivers/iio/adc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ obj-$(CONFIG_LTC2497) += ltc2497.o ltc2497-core.o
5959
obj-$(CONFIG_MAX1027) += max1027.o
6060
obj-$(CONFIG_MAX11100) += max11100.o
6161
obj-$(CONFIG_MAX1118) += max1118.o
62+
obj-$(CONFIG_MAX1241) += max1241.o
6263
obj-$(CONFIG_MAX1363) += max1363.o
6364
obj-$(CONFIG_MAX9611) += max9611.o
6465
obj-$(CONFIG_MCP320X) += mcp320x.o

drivers/iio/adc/max1241.c

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* MAX1241 low-power, 12-bit serial ADC
4+
*
5+
* Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-MAX1241.pdf
6+
*/
7+
8+
#include <linux/delay.h>
9+
#include <linux/gpio/consumer.h>
10+
#include <linux/iio/iio.h>
11+
#include <linux/module.h>
12+
#include <linux/regulator/consumer.h>
13+
#include <linux/spi/spi.h>
14+
15+
#define MAX1241_VAL_MASK GENMASK(11, 0)
16+
#define MAX1241_SHUTDOWN_DELAY_USEC 4
17+
18+
enum max1241_id {
19+
max1241,
20+
};
21+
22+
struct max1241 {
23+
struct spi_device *spi;
24+
struct mutex lock;
25+
struct regulator *vdd;
26+
struct regulator *vref;
27+
struct gpio_desc *shutdown;
28+
29+
__be16 data ____cacheline_aligned;
30+
};
31+
32+
static const struct iio_chan_spec max1241_channels[] = {
33+
{
34+
.type = IIO_VOLTAGE,
35+
.indexed = 1,
36+
.channel = 0,
37+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
38+
BIT(IIO_CHAN_INFO_SCALE),
39+
},
40+
};
41+
42+
static int max1241_read(struct max1241 *adc)
43+
{
44+
struct spi_transfer xfers[] = {
45+
/*
46+
* Begin conversion by bringing /CS low for at least
47+
* tconv us.
48+
*/
49+
{
50+
.len = 0,
51+
.delay.value = 8,
52+
.delay.unit = SPI_DELAY_UNIT_USECS,
53+
},
54+
/*
55+
* Then read two bytes of data in our RX buffer.
56+
*/
57+
{
58+
.rx_buf = &adc->data,
59+
.len = 2,
60+
},
61+
};
62+
63+
return spi_sync_transfer(adc->spi, xfers, ARRAY_SIZE(xfers));
64+
}
65+
66+
static int max1241_read_raw(struct iio_dev *indio_dev,
67+
struct iio_chan_spec const *chan,
68+
int *val, int *val2, long mask)
69+
{
70+
int ret, vref_uV;
71+
struct max1241 *adc = iio_priv(indio_dev);
72+
73+
switch (mask) {
74+
case IIO_CHAN_INFO_RAW:
75+
mutex_lock(&adc->lock);
76+
77+
if (adc->shutdown) {
78+
gpiod_set_value(adc->shutdown, 0);
79+
udelay(MAX1241_SHUTDOWN_DELAY_USEC);
80+
ret = max1241_read(adc);
81+
gpiod_set_value(adc->shutdown, 1);
82+
} else
83+
ret = max1241_read(adc);
84+
85+
if (ret) {
86+
mutex_unlock(&adc->lock);
87+
return ret;
88+
}
89+
90+
*val = (be16_to_cpu(adc->data) >> 3) & MAX1241_VAL_MASK;
91+
92+
mutex_unlock(&adc->lock);
93+
return IIO_VAL_INT;
94+
case IIO_CHAN_INFO_SCALE:
95+
vref_uV = regulator_get_voltage(adc->vref);
96+
97+
if (vref_uV < 0)
98+
return vref_uV;
99+
100+
*val = vref_uV / 1000;
101+
*val2 = 12;
102+
103+
return IIO_VAL_FRACTIONAL_LOG2;
104+
default:
105+
return -EINVAL;
106+
}
107+
}
108+
109+
static const struct iio_info max1241_info = {
110+
.read_raw = max1241_read_raw,
111+
};
112+
113+
static void max1241_disable_vdd_action(void *data)
114+
{
115+
struct max1241 *adc = data;
116+
struct device *dev = &adc->spi->dev;
117+
int err;
118+
119+
err = regulator_disable(adc->vdd);
120+
if (err)
121+
dev_err(dev, "could not disable vdd regulator.\n");
122+
}
123+
124+
static void max1241_disable_vref_action(void *data)
125+
{
126+
struct max1241 *adc = data;
127+
struct device *dev = &adc->spi->dev;
128+
int err;
129+
130+
err = regulator_disable(adc->vref);
131+
if (err)
132+
dev_err(dev, "could not disable vref regulator.\n");
133+
}
134+
135+
static int max1241_probe(struct spi_device *spi)
136+
{
137+
struct device *dev = &spi->dev;
138+
struct iio_dev *indio_dev;
139+
struct max1241 *adc;
140+
int ret;
141+
142+
indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
143+
if (!indio_dev)
144+
return -ENOMEM;
145+
146+
adc = iio_priv(indio_dev);
147+
adc->spi = spi;
148+
mutex_init(&adc->lock);
149+
150+
spi_set_drvdata(spi, indio_dev);
151+
152+
adc->vdd = devm_regulator_get(dev, "vdd");
153+
if (IS_ERR(adc->vdd)) {
154+
dev_err(dev, "failed to get vdd regulator\n");
155+
return PTR_ERR(adc->vdd);
156+
}
157+
158+
ret = regulator_enable(adc->vdd);
159+
if (ret)
160+
return ret;
161+
162+
ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
163+
if (ret) {
164+
dev_err(dev, "could not set up vdd regulator cleanup action\n");
165+
return ret;
166+
}
167+
168+
adc->vref = devm_regulator_get(dev, "vref");
169+
if (IS_ERR(adc->vref)) {
170+
dev_err(dev, "failed to get vref regulator\n");
171+
return PTR_ERR(adc->vref);
172+
}
173+
174+
ret = regulator_enable(adc->vref);
175+
if (ret)
176+
return ret;
177+
178+
ret = devm_add_action_or_reset(dev, max1241_disable_vref_action, adc);
179+
if (ret) {
180+
dev_err(dev, "could not set up vref regulator cleanup action\n");
181+
return ret;
182+
}
183+
184+
adc->shutdown = devm_gpiod_get_optional(dev, "shutdown",
185+
GPIOD_OUT_HIGH);
186+
if (IS_ERR(adc->shutdown))
187+
return PTR_ERR(adc->shutdown);
188+
189+
if (adc->shutdown)
190+
dev_dbg(dev, "shutdown pin passed, low-power mode enabled");
191+
else
192+
dev_dbg(dev, "no shutdown pin passed, low-power mode disabled");
193+
194+
indio_dev->name = spi_get_device_id(spi)->name;
195+
indio_dev->dev.parent = dev;
196+
indio_dev->info = &max1241_info;
197+
indio_dev->modes = INDIO_DIRECT_MODE;
198+
indio_dev->channels = max1241_channels;
199+
indio_dev->num_channels = ARRAY_SIZE(max1241_channels);
200+
201+
return devm_iio_device_register(dev, indio_dev);
202+
}
203+
204+
static const struct spi_device_id max1241_id[] = {
205+
{ "max1241", max1241 },
206+
{}
207+
};
208+
209+
static const struct of_device_id max1241_dt_ids[] = {
210+
{ .compatible = "maxim,max1241" },
211+
{}
212+
};
213+
MODULE_DEVICE_TABLE(of, max1241_dt_ids);
214+
215+
static struct spi_driver max1241_spi_driver = {
216+
.driver = {
217+
.name = "max1241",
218+
.of_match_table = max1241_dt_ids,
219+
},
220+
.probe = max1241_probe,
221+
.id_table = max1241_id,
222+
};
223+
module_spi_driver(max1241_spi_driver);
224+
225+
MODULE_AUTHOR("Alexandru Lazar <[email protected]>");
226+
MODULE_DESCRIPTION("MAX1241 ADC driver");
227+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)