3 * Maxim max11100 ADC Driver with IIO interface
5 * Copyright (C) 2016-17 Renesas Electronics Corporation
6 * Copyright (C) 2016-17 Jacopo Mondi
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.
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>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/driver.h>
22 * LSB is the ADC single digital step
23 * 1 LSB = (vref_mv / 2 ^ 16)
25 * LSB is used to calculate analog voltage value
26 * from the number of ADC steps count
30 #define MAX11100_LSB_DIV (1 << 16)
32 struct max11100_state {
33 struct regulator *vref_reg;
34 struct spi_device *spi;
37 * DMA (thus cache coherency maintenance) requires the
38 * transfer buffers to live in their own cache lines.
40 u8 buffer[3] ____cacheline_aligned;
43 static struct iio_chan_spec max11100_channels[] = {
46 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
47 BIT(IIO_CHAN_INFO_SCALE),
51 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
54 struct max11100_state *state = iio_priv(indio_dev);
56 ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
58 dev_err(&indio_dev->dev, "SPI transfer failed\n");
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");
68 *val = (state->buffer[1] << 8) | state->buffer[2];
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)
78 struct max11100_state *state = iio_priv(indio_dev);
81 case IIO_CHAN_INFO_RAW:
82 ret = max11100_read_single(indio_dev, val);
88 case IIO_CHAN_INFO_SCALE:
89 vref_uv = regulator_get_voltage(state->vref_reg);
91 /* dummy regulator "get_voltage" returns -EINVAL */
94 *val = vref_uv / 1000;
95 *val2 = MAX11100_LSB_DIV;
96 return IIO_VAL_FRACTIONAL;
102 static const struct iio_info max11100_info = {
103 .read_raw = max11100_read_raw,
106 static int max11100_probe(struct spi_device *spi)
109 struct iio_dev *indio_dev;
110 struct max11100_state *state;
112 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
116 spi_set_drvdata(spi, indio_dev);
118 state = iio_priv(indio_dev);
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);
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);
137 ret = iio_device_register(indio_dev);
139 goto disable_regulator;
144 regulator_disable(state->vref_reg);
149 static int max11100_remove(struct spi_device *spi)
151 struct iio_dev *indio_dev = spi_get_drvdata(spi);
152 struct max11100_state *state = iio_priv(indio_dev);
154 iio_device_unregister(indio_dev);
155 regulator_disable(state->vref_reg);
160 static const struct of_device_id max11100_ids[] = {
161 {.compatible = "maxim,max11100"},
164 MODULE_DEVICE_TABLE(of, max11100_ids);
166 static struct spi_driver max11100_driver = {
169 .of_match_table = of_match_ptr(max11100_ids),
171 .probe = max11100_probe,
172 .remove = max11100_remove,
175 module_spi_driver(max11100_driver);
178 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
179 MODULE_LICENSE("GPL v2");