xref: /linux/drivers/iio/adc/max1241.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 struct max1241 {
19 	struct spi_device *spi;
20 	struct mutex lock;
21 	struct regulator *vref;
22 	struct gpio_desc *shutdown;
23 
24 	__be16 data __aligned(IIO_DMA_MINALIGN);
25 };
26 
27 static const struct iio_chan_spec max1241_channels[] = {
28 	{
29 		.type = IIO_VOLTAGE,
30 		.indexed = 1,
31 		.channel = 0,
32 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
33 				BIT(IIO_CHAN_INFO_SCALE),
34 	},
35 };
36 
37 static int max1241_read(struct max1241 *adc)
38 {
39 	struct spi_transfer xfers[] = {
40 		/*
41 		 * Begin conversion by bringing /CS low for at least
42 		 * tconv us.
43 		 */
44 		{
45 			.len = 0,
46 			.delay.value = 8,
47 			.delay.unit = SPI_DELAY_UNIT_USECS,
48 		},
49 		/*
50 		 * Then read two bytes of data in our RX buffer.
51 		 */
52 		{
53 			.rx_buf = &adc->data,
54 			.len = 2,
55 		},
56 	};
57 
58 	return spi_sync_transfer(adc->spi, xfers, ARRAY_SIZE(xfers));
59 }
60 
61 static int max1241_read_raw(struct iio_dev *indio_dev,
62 			struct iio_chan_spec const *chan,
63 			int *val, int *val2, long mask)
64 {
65 	int ret, vref_uV;
66 	struct max1241 *adc = iio_priv(indio_dev);
67 
68 	switch (mask) {
69 	case IIO_CHAN_INFO_RAW:
70 		mutex_lock(&adc->lock);
71 
72 		if (adc->shutdown) {
73 			gpiod_set_value(adc->shutdown, 0);
74 			udelay(MAX1241_SHUTDOWN_DELAY_USEC);
75 			ret = max1241_read(adc);
76 			gpiod_set_value(adc->shutdown, 1);
77 		} else
78 			ret = max1241_read(adc);
79 
80 		if (ret) {
81 			mutex_unlock(&adc->lock);
82 			return ret;
83 		}
84 
85 		*val = (be16_to_cpu(adc->data) >> 3) & MAX1241_VAL_MASK;
86 
87 		mutex_unlock(&adc->lock);
88 		return IIO_VAL_INT;
89 	case IIO_CHAN_INFO_SCALE:
90 		vref_uV = regulator_get_voltage(adc->vref);
91 
92 		if (vref_uV < 0)
93 			return vref_uV;
94 
95 		*val = vref_uV / 1000;
96 		*val2 = 12;
97 
98 		return IIO_VAL_FRACTIONAL_LOG2;
99 	default:
100 		return -EINVAL;
101 	}
102 }
103 
104 static const struct iio_info max1241_info = {
105 	.read_raw = max1241_read_raw,
106 };
107 
108 static void max1241_disable_vref_action(void *data)
109 {
110 	struct max1241 *adc = data;
111 	struct device *dev = &adc->spi->dev;
112 	int err;
113 
114 	err = regulator_disable(adc->vref);
115 	if (err)
116 		dev_err(dev, "could not disable vref regulator.\n");
117 }
118 
119 static int max1241_probe(struct spi_device *spi)
120 {
121 	struct device *dev = &spi->dev;
122 	struct iio_dev *indio_dev;
123 	struct max1241 *adc;
124 	int ret;
125 
126 	indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
127 	if (!indio_dev)
128 		return -ENOMEM;
129 
130 	adc = iio_priv(indio_dev);
131 	adc->spi = spi;
132 	mutex_init(&adc->lock);
133 
134 	ret = devm_regulator_get_enable(dev, "vdd");
135 	if (ret)
136 		return dev_err_probe(dev, ret,
137 				     "failed to get/enable vdd regulator\n");
138 
139 	adc->vref = devm_regulator_get(dev, "vref");
140 	if (IS_ERR(adc->vref))
141 		return dev_err_probe(dev, PTR_ERR(adc->vref),
142 				     "failed to get vref regulator\n");
143 
144 	ret = regulator_enable(adc->vref);
145 	if (ret)
146 		return ret;
147 
148 	ret = devm_add_action_or_reset(dev, max1241_disable_vref_action, adc);
149 	if (ret) {
150 		dev_err(dev, "could not set up vref regulator cleanup action\n");
151 		return ret;
152 	}
153 
154 	adc->shutdown = devm_gpiod_get_optional(dev, "shutdown",
155 						GPIOD_OUT_HIGH);
156 	if (IS_ERR(adc->shutdown))
157 		return dev_err_probe(dev, PTR_ERR(adc->shutdown),
158 				     "cannot get shutdown gpio\n");
159 
160 	if (adc->shutdown)
161 		dev_dbg(dev, "shutdown pin passed, low-power mode enabled");
162 	else
163 		dev_dbg(dev, "no shutdown pin passed, low-power mode disabled");
164 
165 	indio_dev->name = "max1241";
166 	indio_dev->info = &max1241_info;
167 	indio_dev->modes = INDIO_DIRECT_MODE;
168 	indio_dev->channels = max1241_channels;
169 	indio_dev->num_channels = ARRAY_SIZE(max1241_channels);
170 
171 	return devm_iio_device_register(dev, indio_dev);
172 }
173 
174 static const struct spi_device_id max1241_id[] = {
175 	{ .name = "max1241" },
176 	{ }
177 };
178 MODULE_DEVICE_TABLE(spi, max1241_id);
179 
180 static const struct of_device_id max1241_dt_ids[] = {
181 	{ .compatible = "maxim,max1241" },
182 	{ }
183 };
184 MODULE_DEVICE_TABLE(of, max1241_dt_ids);
185 
186 static struct spi_driver max1241_spi_driver = {
187 	.driver = {
188 		.name = "max1241",
189 		.of_match_table = max1241_dt_ids,
190 	},
191 	.probe = max1241_probe,
192 	.id_table = max1241_id,
193 };
194 module_spi_driver(max1241_spi_driver);
195 
196 MODULE_AUTHOR("Alexandru Lazar <alazar@startmail.com>");
197 MODULE_DESCRIPTION("MAX1241 ADC driver");
198 MODULE_LICENSE("GPL v2");
199