drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / iio / adc / max11205.c
blob6c803df220b6e98b0f8e0d5b527a98d7adf6c517
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
8 */
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 {
23 TYPE_MAX11205A,
24 TYPE_MAX11205B,
27 struct max11205_chip_info {
28 unsigned int out_data_rate;
29 const char *name;
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);
47 int reg_mv;
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;
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[] = {
75 .type = IIO_VOLTAGE,
76 .indexed = 1,
77 .scan_type = {
78 .sign = 's',
79 .realbits = 16,
80 .storagebits = 16,
81 .endianness = IIO_BE,
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[] = {
90 [TYPE_MAX11205A] = {
91 .out_data_rate = MAX11205A_OUT_DATA_RATE,
92 .name = "max11205a",
94 [TYPE_MAX11205B] = {
95 .out_data_rate = MAX11205B_OUT_DATA_RATE,
96 .name = "max11205b",
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;
109 int ret;
111 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
112 if (!indio_dev)
113 return -ENOMEM;
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);
133 if (ret)
134 return ret;
136 ret = devm_add_action_or_reset(&spi->dev, max11205_reg_disable, st->vref);
137 if (ret)
138 return ret;
140 ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
141 if (ret)
142 return ret;
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 = {
168 .driver = {
169 .name = "max11205",
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");