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