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