drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / iio / adc / max11100.c
blob520e37f75aac4cd4e75c0d1001ad0f2cfb866b1b
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/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/spi/spi.h>
15 #include <linux/unaligned.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/driver.h>
21 * LSB is the ADC single digital step
22 * 1 LSB = (vref_mv / 2 ^ 16)
24 * LSB is used to calculate analog voltage value
25 * from the number of ADC steps count
27 * Ain = (count * LSB)
29 #define MAX11100_LSB_DIV (1 << 16)
31 struct max11100_state {
32 struct regulator *vref_reg;
33 struct spi_device *spi;
36 * DMA (thus cache coherency maintenance) may require the
37 * transfer buffers to live in their own cache lines.
39 u8 buffer[3] __aligned(IIO_DMA_MINALIGN);
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),
50 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
52 int ret;
53 struct max11100_state *state = iio_priv(indio_dev);
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;
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;
67 *val = get_unaligned_be16(&state->buffer[1]);
69 return 0;
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)
76 int ret, vref_uv;
77 struct max11100_state *state = iio_priv(indio_dev);
79 switch (info) {
80 case IIO_CHAN_INFO_RAW:
81 ret = max11100_read_single(indio_dev, val);
82 if (ret)
83 return ret;
85 return IIO_VAL_INT;
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;
93 *val = vref_uv / 1000;
94 *val2 = MAX11100_LSB_DIV;
95 return IIO_VAL_FRACTIONAL;
98 return -EINVAL;
101 static const struct iio_info max11100_info = {
102 .read_raw = max11100_read_raw,
105 static void max11100_regulator_disable(void *reg)
107 regulator_disable(reg);
110 static int max11100_probe(struct spi_device *spi)
112 int ret;
113 struct iio_dev *indio_dev;
114 struct max11100_state *state;
116 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
117 if (!indio_dev)
118 return -ENOMEM;
120 state = iio_priv(indio_dev);
121 state->spi = spi;
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);
129 state->vref_reg = devm_regulator_get(&spi->dev, "vref");
130 if (IS_ERR(state->vref_reg))
131 return PTR_ERR(state->vref_reg);
133 ret = regulator_enable(state->vref_reg);
134 if (ret)
135 return ret;
137 ret = devm_add_action_or_reset(&spi->dev, max11100_regulator_disable,
138 state->vref_reg);
139 if (ret)
140 return ret;
142 return devm_iio_device_register(&spi->dev, indio_dev);
145 static const struct of_device_id max11100_ids[] = {
146 { .compatible = "maxim,max11100" },
149 MODULE_DEVICE_TABLE(of, max11100_ids);
151 static struct spi_driver max11100_driver = {
152 .driver = {
153 .name = "max11100",
154 .of_match_table = max11100_ids,
156 .probe = max11100_probe,
159 module_spi_driver(max11100_driver);
161 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
162 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
163 MODULE_LICENSE("GPL v2");