blob: 520e37f75aac4cd4e75c0d1001ad0f2cfb866b1b [file] [log] [blame]
Kuninori Morimotoc924eee2018-11-08 06:41:10 +00001// SPDX-License-Identifier: GPL-2.0
Jacopo Mondia8e7e882017-01-18 17:30:52 +01002/*
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
Jacopo Mondia8e7e882017-01-18 17:30:52 +01008 */
9#include <linux/delay.h>
10#include <linux/kernel.h>
Jonathan Cameron7a3e1902020-06-28 13:36:41 +010011#include <linux/mod_devicetable.h>
Jacopo Mondia8e7e882017-01-18 17:30:52 +010012#include <linux/module.h>
13#include <linux/regulator/consumer.h>
14#include <linux/spi/spi.h>
Al Viro5f60d5f2024-10-01 15:35:57 -040015#include <linux/unaligned.h>
Jacopo Mondia8e7e882017-01-18 17:30:52 +010016
17#include <linux/iio/iio.h>
18#include <linux/iio/driver.h>
19
20/*
21 * LSB is the ADC single digital step
22 * 1 LSB = (vref_mv / 2 ^ 16)
23 *
24 * LSB is used to calculate analog voltage value
25 * from the number of ADC steps count
26 *
27 * Ain = (count * LSB)
28 */
29#define MAX11100_LSB_DIV (1 << 16)
30
31struct max11100_state {
Jacopo Mondia8e7e882017-01-18 17:30:52 +010032 struct regulator *vref_reg;
33 struct spi_device *spi;
34
35 /*
Jonathan Cameron51f30d62022-05-08 18:56:04 +010036 * DMA (thus cache coherency maintenance) may require the
Jacopo Mondia8e7e882017-01-18 17:30:52 +010037 * transfer buffers to live in their own cache lines.
38 */
Jonathan Cameron51f30d62022-05-08 18:56:04 +010039 u8 buffer[3] __aligned(IIO_DMA_MINALIGN);
Jacopo Mondia8e7e882017-01-18 17:30:52 +010040};
41
Rikard Falkebornce6c3732020-05-26 23:02:18 +020042static const struct iio_chan_spec max11100_channels[] = {
Jacopo Mondia8e7e882017-01-18 17:30:52 +010043 { /* [0] */
44 .type = IIO_VOLTAGE,
45 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
46 BIT(IIO_CHAN_INFO_SCALE),
47 },
48};
49
Jacopo Mondia8e7e882017-01-18 17:30:52 +010050static int max11100_read_single(struct iio_dev *indio_dev, int *val)
51{
52 int ret;
53 struct max11100_state *state = iio_priv(indio_dev);
54
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;
59 }
60
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;
65 }
66
Jonathan Cameronff9111a2021-05-16 18:25:13 +010067 *val = get_unaligned_be16(&state->buffer[1]);
Jacopo Mondia8e7e882017-01-18 17:30:52 +010068
69 return 0;
70}
71
72static int max11100_read_raw(struct iio_dev *indio_dev,
73 struct iio_chan_spec const *chan,
74 int *val, int *val2, long info)
75{
76 int ret, vref_uv;
77 struct max11100_state *state = iio_priv(indio_dev);
78
79 switch (info) {
80 case IIO_CHAN_INFO_RAW:
81 ret = max11100_read_single(indio_dev, val);
82 if (ret)
83 return ret;
84
85 return IIO_VAL_INT;
86
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;
92
93 *val = vref_uv / 1000;
94 *val2 = MAX11100_LSB_DIV;
95 return IIO_VAL_FRACTIONAL;
96 }
97
98 return -EINVAL;
99}
100
101static const struct iio_info max11100_info = {
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100102 .read_raw = max11100_read_raw,
103};
104
Jonathan Cameron7169a782021-05-16 18:25:14 +0100105static void max11100_regulator_disable(void *reg)
106{
107 regulator_disable(reg);
108}
109
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100110static int max11100_probe(struct spi_device *spi)
111{
112 int ret;
113 struct iio_dev *indio_dev;
114 struct max11100_state *state;
115
116 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
117 if (!indio_dev)
118 return -ENOMEM;
119
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100120 state = iio_priv(indio_dev);
121 state->spi = spi;
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100122
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100123 indio_dev->name = "max11100";
124 indio_dev->info = &max11100_info;
125 indio_dev->modes = INDIO_DIRECT_MODE;
Arushi Singhalbb4787f2017-03-30 18:16:03 +0530126 indio_dev->channels = max11100_channels;
127 indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100128
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
Jonathan Cameron7169a782021-05-16 18:25:14 +0100137 ret = devm_add_action_or_reset(&spi->dev, max11100_regulator_disable,
138 state->vref_reg);
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100139 if (ret)
Jonathan Cameron7169a782021-05-16 18:25:14 +0100140 return ret;
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100141
Jonathan Cameron7169a782021-05-16 18:25:14 +0100142 return devm_iio_device_register(&spi->dev, indio_dev);
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100143}
144
145static const struct of_device_id max11100_ids[] = {
Jonathan Cameron09e3bdf2024-08-18 19:09:12 +0100146 { .compatible = "maxim,max11100" },
147 { }
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100148};
149MODULE_DEVICE_TABLE(of, max11100_ids);
150
151static struct spi_driver max11100_driver = {
152 .driver = {
153 .name = "max11100",
Jonathan Cameron7a3e1902020-06-28 13:36:41 +0100154 .of_match_table = max11100_ids,
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100155 },
156 .probe = max11100_probe,
Jacopo Mondia8e7e882017-01-18 17:30:52 +0100157};
158
159module_spi_driver(max11100_driver);
160
161MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
162MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
163MODULE_LICENSE("GPL v2");