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/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14 #include <linux/unaligned.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) may require the
36 * transfer buffers to live in their own cache lines.
37 */
38 u8 buffer[3] __aligned(IIO_DMA_MINALIGN);
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
max11100_read_single(struct iio_dev * indio_dev,int * val)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 = get_unaligned_be16(&state->buffer[1]);
67
68 return 0;
69 }
70
max11100_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)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
max11100_regulator_disable(void * reg)104 static void max11100_regulator_disable(void *reg)
105 {
106 regulator_disable(reg);
107 }
108
max11100_probe(struct spi_device * spi)109 static int max11100_probe(struct spi_device *spi)
110 {
111 int ret;
112 struct iio_dev *indio_dev;
113 struct max11100_state *state;
114
115 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
116 if (!indio_dev)
117 return -ENOMEM;
118
119 state = iio_priv(indio_dev);
120 state->spi = spi;
121
122 indio_dev->name = "max11100";
123 indio_dev->info = &max11100_info;
124 indio_dev->modes = INDIO_DIRECT_MODE;
125 indio_dev->channels = max11100_channels;
126 indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
127
128 state->vref_reg = devm_regulator_get(&spi->dev, "vref");
129 if (IS_ERR(state->vref_reg))
130 return PTR_ERR(state->vref_reg);
131
132 ret = regulator_enable(state->vref_reg);
133 if (ret)
134 return ret;
135
136 ret = devm_add_action_or_reset(&spi->dev, max11100_regulator_disable,
137 state->vref_reg);
138 if (ret)
139 return ret;
140
141 return devm_iio_device_register(&spi->dev, indio_dev);
142 }
143
144 static const struct of_device_id max11100_ids[] = {
145 { .compatible = "maxim,max11100" },
146 { }
147 };
148 MODULE_DEVICE_TABLE(of, max11100_ids);
149
150 static struct spi_driver max11100_driver = {
151 .driver = {
152 .name = "max11100",
153 .of_match_table = max11100_ids,
154 },
155 .probe = max11100_probe,
156 };
157
158 module_spi_driver(max11100_driver);
159
160 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
161 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
162 MODULE_LICENSE("GPL v2");
163