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