1 // SPDX-License-Identifier: GPL-2.0
3 * Maxim MAX11205 16-Bit Delta-Sigma ADC
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>
10 #include <linux/device.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/adc/ad_sigma_delta.h>
18 #define MAX11205_BIT_SCALE 15
19 #define MAX11205A_OUT_DATA_RATE 116
20 #define MAX11205B_OUT_DATA_RATE 13
22 enum max11205_chip_type
{
27 struct max11205_chip_info
{
28 unsigned int out_data_rate
;
32 struct max11205_state
{
33 const struct max11205_chip_info
*chip_info
;
34 struct regulator
*vref
;
35 struct ad_sigma_delta sd
;
38 static const struct ad_sigma_delta_info max11205_sigma_delta_info
= {
39 .has_registers
= false,
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
)
46 struct max11205_state
*st
= iio_priv(indio_dev
);
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
);
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
;
68 static const struct iio_info max11205_iio_info
= {
69 .read_raw
= max11205_read_raw
,
70 .validate_trigger
= ad_sd_validate_trigger
,
73 static const struct iio_chan_spec max11205_channels
[] = {
83 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
84 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
85 BIT(IIO_CHAN_INFO_SCALE
),
89 static const struct max11205_chip_info max11205_chip_info
[] = {
91 .out_data_rate
= MAX11205A_OUT_DATA_RATE
,
95 .out_data_rate
= MAX11205B_OUT_DATA_RATE
,
100 static void max11205_reg_disable(void *reg
)
102 regulator_disable(reg
);
105 static int max11205_probe(struct spi_device
*spi
)
107 struct max11205_state
*st
;
108 struct iio_dev
*indio_dev
;
111 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
115 st
= iio_priv(indio_dev
);
117 ad_sd_init(&st
->sd
, indio_dev
, spi
, &max11205_sigma_delta_info
);
119 st
->chip_info
= spi_get_device_match_data(spi
);
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
;
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");
132 ret
= regulator_enable(st
->vref
);
136 ret
= devm_add_action_or_reset(&spi
->dev
, max11205_reg_disable
, st
->vref
);
140 ret
= devm_ad_sd_setup_buffer_and_trigger(&spi
->dev
, indio_dev
);
144 return devm_iio_device_register(&spi
->dev
, indio_dev
);
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
] },
152 MODULE_DEVICE_TABLE(spi
, max11205_spi_ids
);
154 static const struct of_device_id max11205_dt_ids
[] = {
156 .compatible
= "maxim,max11205a",
157 .data
= &max11205_chip_info
[TYPE_MAX11205A
],
160 .compatible
= "maxim,max11205b",
161 .data
= &max11205_chip_info
[TYPE_MAX11205B
],
165 MODULE_DEVICE_TABLE(of
, max11205_dt_ids
);
167 static struct spi_driver max11205_spi_driver
= {
170 .of_match_table
= max11205_dt_ids
,
172 .probe
= max11205_probe
,
173 .id_table
= max11205_spi_ids
,
175 module_spi_driver(max11205_spi_driver
);
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");