1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Maxim MAX11205 16-Bit Delta-Sigma ADC
4 *
5 * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-max11205.pdf
6 * Copyright (C) 2022 Analog Devices, Inc.
7 * Author: Ramona Bolboaca <ramona.bolboaca@analog.com>
8 */
9
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14
15 #include <linux/iio/iio.h>
16 #include <linux/iio/adc/ad_sigma_delta.h>
17
18 #define MAX11205_BIT_SCALE 15
19 #define MAX11205A_OUT_DATA_RATE 116
20 #define MAX11205B_OUT_DATA_RATE 13
21
22 enum max11205_chip_type {
23 TYPE_MAX11205A,
24 TYPE_MAX11205B,
25 };
26
27 struct max11205_chip_info {
28 unsigned int out_data_rate;
29 const char *name;
30 };
31
32 struct max11205_state {
33 const struct max11205_chip_info *chip_info;
34 struct regulator *vref;
35 struct ad_sigma_delta sd;
36 };
37
38 static const struct ad_sigma_delta_info max11205_sigma_delta_info = {
39 .has_registers = false,
40 };
41
max11205_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)42 static int max11205_read_raw(struct iio_dev *indio_dev,
43 struct iio_chan_spec const *chan,
44 int *val, int *val2, long mask)
45 {
46 struct max11205_state *st = iio_priv(indio_dev);
47 int reg_mv;
48
49 switch (mask) {
50 case IIO_CHAN_INFO_RAW:
51 return ad_sigma_delta_single_conversion(indio_dev, chan, val);
52 case IIO_CHAN_INFO_SCALE:
53 reg_mv = regulator_get_voltage(st->vref);
54 if (reg_mv < 0)
55 return reg_mv;
56 reg_mv /= 1000;
57 *val = reg_mv;
58 *val2 = MAX11205_BIT_SCALE;
59 return IIO_VAL_FRACTIONAL_LOG2;
60 case IIO_CHAN_INFO_SAMP_FREQ:
61 *val = st->chip_info->out_data_rate;
62 return IIO_VAL_INT;
63 default:
64 return -EINVAL;
65 }
66 }
67
68 static const struct iio_info max11205_iio_info = {
69 .read_raw = max11205_read_raw,
70 .validate_trigger = ad_sd_validate_trigger,
71 };
72
73 static const struct iio_chan_spec max11205_channels[] = {
74 {
75 .type = IIO_VOLTAGE,
76 .indexed = 1,
77 .scan_type = {
78 .sign = 's',
79 .realbits = 16,
80 .storagebits = 16,
81 .endianness = IIO_BE,
82 },
83 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
84 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
85 BIT(IIO_CHAN_INFO_SCALE),
86 },
87 };
88
89 static const struct max11205_chip_info max11205_chip_info[] = {
90 [TYPE_MAX11205A] = {
91 .out_data_rate = MAX11205A_OUT_DATA_RATE,
92 .name = "max11205a",
93 },
94 [TYPE_MAX11205B] = {
95 .out_data_rate = MAX11205B_OUT_DATA_RATE,
96 .name = "max11205b",
97 },
98 };
99
max11205_reg_disable(void * reg)100 static void max11205_reg_disable(void *reg)
101 {
102 regulator_disable(reg);
103 }
104
max11205_probe(struct spi_device * spi)105 static int max11205_probe(struct spi_device *spi)
106 {
107 struct max11205_state *st;
108 struct iio_dev *indio_dev;
109 int ret;
110
111 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
112 if (!indio_dev)
113 return -ENOMEM;
114
115 st = iio_priv(indio_dev);
116
117 ad_sd_init(&st->sd, indio_dev, spi, &max11205_sigma_delta_info);
118
119 st->chip_info = spi_get_device_match_data(spi);
120
121 indio_dev->name = st->chip_info->name;
122 indio_dev->modes = INDIO_DIRECT_MODE;
123 indio_dev->channels = max11205_channels;
124 indio_dev->num_channels = 1;
125 indio_dev->info = &max11205_iio_info;
126
127 st->vref = devm_regulator_get(&spi->dev, "vref");
128 if (IS_ERR(st->vref))
129 return dev_err_probe(&spi->dev, PTR_ERR(st->vref),
130 "Failed to get vref regulator\n");
131
132 ret = regulator_enable(st->vref);
133 if (ret)
134 return ret;
135
136 ret = devm_add_action_or_reset(&spi->dev, max11205_reg_disable, st->vref);
137 if (ret)
138 return ret;
139
140 ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
141 if (ret)
142 return ret;
143
144 return devm_iio_device_register(&spi->dev, indio_dev);
145 }
146
147 static const struct spi_device_id max11205_spi_ids[] = {
148 { "max11205a", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205A] },
149 { "max11205b", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205B] },
150 { }
151 };
152 MODULE_DEVICE_TABLE(spi, max11205_spi_ids);
153
154 static const struct of_device_id max11205_dt_ids[] = {
155 {
156 .compatible = "maxim,max11205a",
157 .data = &max11205_chip_info[TYPE_MAX11205A],
158 },
159 {
160 .compatible = "maxim,max11205b",
161 .data = &max11205_chip_info[TYPE_MAX11205B],
162 },
163 { }
164 };
165 MODULE_DEVICE_TABLE(of, max11205_dt_ids);
166
167 static struct spi_driver max11205_spi_driver = {
168 .driver = {
169 .name = "max11205",
170 .of_match_table = max11205_dt_ids,
171 },
172 .probe = max11205_probe,
173 .id_table = max11205_spi_ids,
174 };
175 module_spi_driver(max11205_spi_driver);
176
177 MODULE_AUTHOR("Ramona Bolboaca <ramona.bolboaca@analog.com>");
178 MODULE_DESCRIPTION("MAX11205 ADC driver");
179 MODULE_LICENSE("GPL v2");
180 MODULE_IMPORT_NS("IIO_AD_SIGMA_DELTA");
181