1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAX1241 low-power, 12-bit serial ADC
5 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-MAX1241.pdf
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>
15 #define MAX1241_VAL_MASK GENMASK(11, 0)
16 #define MAX1241_SHUTDOWN_DELAY_USEC 4
23 struct spi_device
*spi
;
25 struct regulator
*vdd
;
26 struct regulator
*vref
;
27 struct gpio_desc
*shutdown
;
29 __be16 data ____cacheline_aligned
;
32 static const struct iio_chan_spec max1241_channels
[] = {
37 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
38 BIT(IIO_CHAN_INFO_SCALE
),
42 static int max1241_read(struct max1241
*adc
)
44 struct spi_transfer xfers
[] = {
46 * Begin conversion by bringing /CS low for at least
52 .delay
.unit
= SPI_DELAY_UNIT_USECS
,
55 * Then read two bytes of data in our RX buffer.
63 return spi_sync_transfer(adc
->spi
, xfers
, ARRAY_SIZE(xfers
));
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
)
71 struct max1241
*adc
= iio_priv(indio_dev
);
74 case IIO_CHAN_INFO_RAW
:
75 mutex_lock(&adc
->lock
);
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);
83 ret
= max1241_read(adc
);
86 mutex_unlock(&adc
->lock
);
90 *val
= (be16_to_cpu(adc
->data
) >> 3) & MAX1241_VAL_MASK
;
92 mutex_unlock(&adc
->lock
);
94 case IIO_CHAN_INFO_SCALE
:
95 vref_uV
= regulator_get_voltage(adc
->vref
);
100 *val
= vref_uV
/ 1000;
103 return IIO_VAL_FRACTIONAL_LOG2
;
109 static const struct iio_info max1241_info
= {
110 .read_raw
= max1241_read_raw
,
113 static void max1241_disable_vdd_action(void *data
)
115 struct max1241
*adc
= data
;
116 struct device
*dev
= &adc
->spi
->dev
;
119 err
= regulator_disable(adc
->vdd
);
121 dev_err(dev
, "could not disable vdd regulator.\n");
124 static void max1241_disable_vref_action(void *data
)
126 struct max1241
*adc
= data
;
127 struct device
*dev
= &adc
->spi
->dev
;
130 err
= regulator_disable(adc
->vref
);
132 dev_err(dev
, "could not disable vref regulator.\n");
135 static int max1241_probe(struct spi_device
*spi
)
137 struct device
*dev
= &spi
->dev
;
138 struct iio_dev
*indio_dev
;
142 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*adc
));
146 adc
= iio_priv(indio_dev
);
148 mutex_init(&adc
->lock
);
150 spi_set_drvdata(spi
, indio_dev
);
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
);
158 ret
= regulator_enable(adc
->vdd
);
162 ret
= devm_add_action_or_reset(dev
, max1241_disable_vdd_action
, adc
);
164 dev_err(dev
, "could not set up vdd regulator cleanup action\n");
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
);
174 ret
= regulator_enable(adc
->vref
);
178 ret
= devm_add_action_or_reset(dev
, max1241_disable_vref_action
, adc
);
180 dev_err(dev
, "could not set up vref regulator cleanup action\n");
184 adc
->shutdown
= devm_gpiod_get_optional(dev
, "shutdown",
186 if (IS_ERR(adc
->shutdown
))
187 return PTR_ERR(adc
->shutdown
);
190 dev_dbg(dev
, "shutdown pin passed, low-power mode enabled");
192 dev_dbg(dev
, "no shutdown pin passed, low-power mode disabled");
194 indio_dev
->name
= spi_get_device_id(spi
)->name
;
195 indio_dev
->info
= &max1241_info
;
196 indio_dev
->modes
= INDIO_DIRECT_MODE
;
197 indio_dev
->channels
= max1241_channels
;
198 indio_dev
->num_channels
= ARRAY_SIZE(max1241_channels
);
200 return devm_iio_device_register(dev
, indio_dev
);
203 static const struct spi_device_id max1241_id
[] = {
204 { "max1241", max1241
},
208 static const struct of_device_id max1241_dt_ids
[] = {
209 { .compatible
= "maxim,max1241" },
212 MODULE_DEVICE_TABLE(of
, max1241_dt_ids
);
214 static struct spi_driver max1241_spi_driver
= {
217 .of_match_table
= max1241_dt_ids
,
219 .probe
= max1241_probe
,
220 .id_table
= max1241_id
,
222 module_spi_driver(max1241_spi_driver
);
224 MODULE_AUTHOR("Alexandru Lazar <alazar@startmail.com>");
225 MODULE_DESCRIPTION("MAX1241 ADC driver");
226 MODULE_LICENSE("GPL v2");