xref: /openbmc/linux/drivers/iio/adc/max11100.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * iio/adc/max11100.c
4  * Maxim max11100 ADC Driver with IIO interface
5  *
6  * Copyright (C) 2016-17 Renesas Electronics Corporation
7  * Copyright (C) 2016-17 Jacopo Mondi
8  */
9 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/spi/spi.h>
15 
16 #include <linux/iio/iio.h>
17 #include <linux/iio/driver.h>
18 
19 /*
20  * LSB is the ADC single digital step
21  * 1 LSB = (vref_mv / 2 ^ 16)
22  *
23  * LSB is used to calculate analog voltage value
24  * from the number of ADC steps count
25  *
26  * Ain = (count * LSB)
27  */
28 #define MAX11100_LSB_DIV		(1 << 16)
29 
30 struct max11100_state {
31 	struct regulator *vref_reg;
32 	struct spi_device *spi;
33 
34 	/*
35 	 * DMA (thus cache coherency maintenance) requires the
36 	 * transfer buffers to live in their own cache lines.
37 	 */
38 	u8 buffer[3] ____cacheline_aligned;
39 };
40 
41 static const struct iio_chan_spec max11100_channels[] = {
42 	{ /* [0] */
43 		.type = IIO_VOLTAGE,
44 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
45 				      BIT(IIO_CHAN_INFO_SCALE),
46 	},
47 };
48 
49 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
50 {
51 	int ret;
52 	struct max11100_state *state = iio_priv(indio_dev);
53 
54 	ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
55 	if (ret) {
56 		dev_err(&indio_dev->dev, "SPI transfer failed\n");
57 		return ret;
58 	}
59 
60 	/* the first 8 bits sent out from ADC must be 0s */
61 	if (state->buffer[0]) {
62 		dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
63 		return -EINVAL;
64 	}
65 
66 	*val = (state->buffer[1] << 8) | state->buffer[2];
67 
68 	return 0;
69 }
70 
71 static int max11100_read_raw(struct iio_dev *indio_dev,
72 			     struct iio_chan_spec const *chan,
73 			     int *val, int *val2, long info)
74 {
75 	int ret, vref_uv;
76 	struct max11100_state *state = iio_priv(indio_dev);
77 
78 	switch (info) {
79 	case IIO_CHAN_INFO_RAW:
80 		ret = max11100_read_single(indio_dev, val);
81 		if (ret)
82 			return ret;
83 
84 		return IIO_VAL_INT;
85 
86 	case IIO_CHAN_INFO_SCALE:
87 		vref_uv = regulator_get_voltage(state->vref_reg);
88 		if (vref_uv < 0)
89 			/* dummy regulator "get_voltage" returns -EINVAL */
90 			return -EINVAL;
91 
92 		*val =  vref_uv / 1000;
93 		*val2 = MAX11100_LSB_DIV;
94 		return IIO_VAL_FRACTIONAL;
95 	}
96 
97 	return -EINVAL;
98 }
99 
100 static const struct iio_info max11100_info = {
101 	.read_raw = max11100_read_raw,
102 };
103 
104 static int max11100_probe(struct spi_device *spi)
105 {
106 	int ret;
107 	struct iio_dev *indio_dev;
108 	struct max11100_state *state;
109 
110 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
111 	if (!indio_dev)
112 		return -ENOMEM;
113 
114 	spi_set_drvdata(spi, indio_dev);
115 
116 	state = iio_priv(indio_dev);
117 	state->spi = spi;
118 
119 	indio_dev->name = "max11100";
120 	indio_dev->info = &max11100_info;
121 	indio_dev->modes = INDIO_DIRECT_MODE;
122 	indio_dev->channels = max11100_channels;
123 	indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
124 
125 	state->vref_reg = devm_regulator_get(&spi->dev, "vref");
126 	if (IS_ERR(state->vref_reg))
127 		return PTR_ERR(state->vref_reg);
128 
129 	ret = regulator_enable(state->vref_reg);
130 	if (ret)
131 		return ret;
132 
133 	ret = iio_device_register(indio_dev);
134 	if (ret)
135 		goto disable_regulator;
136 
137 	return 0;
138 
139 disable_regulator:
140 	regulator_disable(state->vref_reg);
141 
142 	return ret;
143 }
144 
145 static int max11100_remove(struct spi_device *spi)
146 {
147 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
148 	struct max11100_state *state = iio_priv(indio_dev);
149 
150 	iio_device_unregister(indio_dev);
151 	regulator_disable(state->vref_reg);
152 
153 	return 0;
154 }
155 
156 static const struct of_device_id max11100_ids[] = {
157 	{.compatible = "maxim,max11100"},
158 	{ },
159 };
160 MODULE_DEVICE_TABLE(of, max11100_ids);
161 
162 static struct spi_driver max11100_driver = {
163 	.driver = {
164 		.name	= "max11100",
165 		.of_match_table = max11100_ids,
166 	},
167 	.probe		= max11100_probe,
168 	.remove		= max11100_remove,
169 };
170 
171 module_spi_driver(max11100_driver);
172 
173 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
174 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
175 MODULE_LICENSE("GPL v2");
176