dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / adc / max11100.c
blob3440539cfdba7323952bed1ba89dace2ff7b2d18
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * iio/adc/max11100.c
4 * Maxim max11100 ADC Driver with IIO interface
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>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/driver.h>
19 * LSB is the ADC single digital step
20 * 1 LSB = (vref_mv / 2 ^ 16)
22 * LSB is used to calculate analog voltage value
23 * from the number of ADC steps count
25 * Ain = (count * LSB)
27 #define MAX11100_LSB_DIV (1 << 16)
29 struct max11100_state {
30 struct regulator *vref_reg;
31 struct spi_device *spi;
34 * DMA (thus cache coherency maintenance) requires the
35 * transfer buffers to live in their own cache lines.
37 u8 buffer[3] ____cacheline_aligned;
40 static struct iio_chan_spec max11100_channels[] = {
41 { /* [0] */
42 .type = IIO_VOLTAGE,
43 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
44 BIT(IIO_CHAN_INFO_SCALE),
48 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
50 int ret;
51 struct max11100_state *state = iio_priv(indio_dev);
53 ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
54 if (ret) {
55 dev_err(&indio_dev->dev, "SPI transfer failed\n");
56 return ret;
59 /* the first 8 bits sent out from ADC must be 0s */
60 if (state->buffer[0]) {
61 dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
62 return -EINVAL;
65 *val = (state->buffer[1] << 8) | state->buffer[2];
67 return 0;
70 static int max11100_read_raw(struct iio_dev *indio_dev,
71 struct iio_chan_spec const *chan,
72 int *val, int *val2, long info)
74 int ret, vref_uv;
75 struct max11100_state *state = iio_priv(indio_dev);
77 switch (info) {
78 case IIO_CHAN_INFO_RAW:
79 ret = max11100_read_single(indio_dev, val);
80 if (ret)
81 return ret;
83 return IIO_VAL_INT;
85 case IIO_CHAN_INFO_SCALE:
86 vref_uv = regulator_get_voltage(state->vref_reg);
87 if (vref_uv < 0)
88 /* dummy regulator "get_voltage" returns -EINVAL */
89 return -EINVAL;
91 *val = vref_uv / 1000;
92 *val2 = MAX11100_LSB_DIV;
93 return IIO_VAL_FRACTIONAL;
96 return -EINVAL;
99 static const struct iio_info max11100_info = {
100 .read_raw = max11100_read_raw,
103 static int max11100_probe(struct spi_device *spi)
105 int ret;
106 struct iio_dev *indio_dev;
107 struct max11100_state *state;
109 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
110 if (!indio_dev)
111 return -ENOMEM;
113 spi_set_drvdata(spi, indio_dev);
115 state = iio_priv(indio_dev);
116 state->spi = spi;
118 indio_dev->dev.parent = &spi->dev;
119 indio_dev->dev.of_node = spi->dev.of_node;
120 indio_dev->name = "max11100";
121 indio_dev->info = &max11100_info;
122 indio_dev->modes = INDIO_DIRECT_MODE;
123 indio_dev->channels = max11100_channels;
124 indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
126 state->vref_reg = devm_regulator_get(&spi->dev, "vref");
127 if (IS_ERR(state->vref_reg))
128 return PTR_ERR(state->vref_reg);
130 ret = regulator_enable(state->vref_reg);
131 if (ret)
132 return ret;
134 ret = iio_device_register(indio_dev);
135 if (ret)
136 goto disable_regulator;
138 return 0;
140 disable_regulator:
141 regulator_disable(state->vref_reg);
143 return ret;
146 static int max11100_remove(struct spi_device *spi)
148 struct iio_dev *indio_dev = spi_get_drvdata(spi);
149 struct max11100_state *state = iio_priv(indio_dev);
151 iio_device_unregister(indio_dev);
152 regulator_disable(state->vref_reg);
154 return 0;
157 static const struct of_device_id max11100_ids[] = {
158 {.compatible = "maxim,max11100"},
159 { },
161 MODULE_DEVICE_TABLE(of, max11100_ids);
163 static struct spi_driver max11100_driver = {
164 .driver = {
165 .name = "max11100",
166 .of_match_table = of_match_ptr(max11100_ids),
168 .probe = max11100_probe,
169 .remove = max11100_remove,
172 module_spi_driver(max11100_driver);
174 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
175 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
176 MODULE_LICENSE("GPL v2");