xref: /openbmc/linux/drivers/iio/adc/max11205.c (revision 7559e757)
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 
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 
100 static void max11205_reg_disable(void *reg)
101 {
102 	regulator_disable(reg);
103 }
104 
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 = device_get_match_data(&spi->dev);
120 	if (!st->chip_info)
121 		st->chip_info =
122 			(const struct max11205_chip_info *)spi_get_device_id(spi)->driver_data;
123 
124 	indio_dev->name = st->chip_info->name;
125 	indio_dev->modes = INDIO_DIRECT_MODE;
126 	indio_dev->channels = max11205_channels;
127 	indio_dev->num_channels = 1;
128 	indio_dev->info = &max11205_iio_info;
129 
130 	st->vref = devm_regulator_get(&spi->dev, "vref");
131 	if (IS_ERR(st->vref))
132 		return dev_err_probe(&spi->dev, PTR_ERR(st->vref),
133 				     "Failed to get vref regulator\n");
134 
135 	ret = regulator_enable(st->vref);
136 	if (ret)
137 		return ret;
138 
139 	ret = devm_add_action_or_reset(&spi->dev, max11205_reg_disable, st->vref);
140 	if (ret)
141 		return ret;
142 
143 	ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev);
144 	if (ret)
145 		return ret;
146 
147 	return devm_iio_device_register(&spi->dev, indio_dev);
148 }
149 
150 static const struct spi_device_id max11205_spi_ids[] = {
151 	{ "max11205a", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205A] },
152 	{ "max11205b", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205B] },
153 	{ }
154 };
155 MODULE_DEVICE_TABLE(spi, max11205_spi_ids);
156 
157 static const struct of_device_id max11205_dt_ids[] = {
158 	{
159 		.compatible = "maxim,max11205a",
160 		.data = &max11205_chip_info[TYPE_MAX11205A],
161 	},
162 	{
163 		.compatible = "maxim,max11205b",
164 		.data = &max11205_chip_info[TYPE_MAX11205B],
165 	},
166 	{ }
167 };
168 MODULE_DEVICE_TABLE(of, max11205_dt_ids);
169 
170 static struct spi_driver max11205_spi_driver = {
171 	.driver = {
172 		.name = "max11205",
173 		.of_match_table = max11205_dt_ids,
174 	},
175 	.probe = max11205_probe,
176 	.id_table = max11205_spi_ids,
177 };
178 module_spi_driver(max11205_spi_driver);
179 
180 MODULE_AUTHOR("Ramona Bolboaca <ramona.bolboaca@analog.com>");
181 MODULE_DESCRIPTION("MAX11205 ADC driver");
182 MODULE_LICENSE("GPL v2");
183 MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);
184