1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Analog Devices AD7405 driver
4 *
5 * Copyright 2025 Analog Devices Inc.
6 */
7
8 #include <linux/clk.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/math64.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/util_macros.h>
17
18 #include <linux/iio/backend.h>
19 #include <linux/iio/iio.h>
20
21 static const unsigned int ad7405_dec_rates_range[] = {
22 32, 1, 4096,
23 };
24
25 struct ad7405_chip_info {
26 const char *name;
27 const unsigned int full_scale_mv;
28 };
29
30 struct ad7405_state {
31 struct iio_backend *back;
32 const struct ad7405_chip_info *info;
33 unsigned int ref_frequency;
34 unsigned int dec_rate;
35 };
36
ad7405_set_dec_rate(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int dec_rate)37 static int ad7405_set_dec_rate(struct iio_dev *indio_dev,
38 const struct iio_chan_spec *chan,
39 unsigned int dec_rate)
40 {
41 struct ad7405_state *st = iio_priv(indio_dev);
42 int ret;
43
44 if (dec_rate > 4096 || dec_rate < 32)
45 return -EINVAL;
46
47 if (!iio_device_claim_direct(indio_dev))
48 return -EBUSY;
49
50 ret = iio_backend_oversampling_ratio_set(st->back, chan->scan_index, dec_rate);
51 iio_device_release_direct(indio_dev);
52
53 if (ret < 0)
54 return ret;
55
56 st->dec_rate = dec_rate;
57
58 return 0;
59 }
60
ad7405_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long info)61 static int ad7405_read_raw(struct iio_dev *indio_dev,
62 const struct iio_chan_spec *chan, int *val,
63 int *val2, long info)
64 {
65 struct ad7405_state *st = iio_priv(indio_dev);
66
67 switch (info) {
68 case IIO_CHAN_INFO_SCALE:
69 *val = st->info->full_scale_mv;
70 *val2 = indio_dev->channels[0].scan_type.realbits - 1;
71 return IIO_VAL_FRACTIONAL_LOG2;
72 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
73 *val = st->dec_rate;
74 return IIO_VAL_INT;
75 case IIO_CHAN_INFO_SAMP_FREQ:
76 *val = DIV_ROUND_CLOSEST_ULL(st->ref_frequency, st->dec_rate);
77 return IIO_VAL_INT;
78 case IIO_CHAN_INFO_OFFSET:
79 *val = -(1 << (indio_dev->channels[0].scan_type.realbits - 1));
80 return IIO_VAL_INT;
81 default:
82 return -EINVAL;
83 }
84 }
85
ad7405_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)86 static int ad7405_write_raw(struct iio_dev *indio_dev,
87 struct iio_chan_spec const *chan, int val,
88 int val2, long info)
89 {
90 switch (info) {
91 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
92 if (val < 0)
93 return -EINVAL;
94 return ad7405_set_dec_rate(indio_dev, chan, val);
95 default:
96 return -EINVAL;
97 }
98 }
99
ad7405_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long info)100 static int ad7405_read_avail(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan,
102 const int **vals, int *type, int *length,
103 long info)
104 {
105 switch (info) {
106 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
107 *vals = ad7405_dec_rates_range;
108 *type = IIO_VAL_INT;
109 return IIO_AVAIL_RANGE;
110 default:
111 return -EINVAL;
112 }
113 }
114
115 static const struct iio_info ad7405_iio_info = {
116 .read_raw = &ad7405_read_raw,
117 .write_raw = &ad7405_write_raw,
118 .read_avail = &ad7405_read_avail,
119 };
120
121 static const struct iio_chan_spec ad7405_channel = {
122 .type = IIO_VOLTAGE,
123 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
124 BIT(IIO_CHAN_INFO_OFFSET),
125 .info_mask_shared_by_all = IIO_CHAN_INFO_SAMP_FREQ |
126 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
127 .info_mask_shared_by_all_available =
128 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
129 .indexed = 1,
130 .channel = 0,
131 .channel2 = 1,
132 .differential = 1,
133 .scan_index = 0,
134 .scan_type = {
135 .sign = 'u',
136 .realbits = 16,
137 .storagebits = 16,
138 },
139 };
140
141 static const struct ad7405_chip_info ad7405_chip_info = {
142 .name = "ad7405",
143 .full_scale_mv = 320,
144 };
145
146 static const struct ad7405_chip_info adum7701_chip_info = {
147 .name = "adum7701",
148 .full_scale_mv = 320,
149 };
150
151 static const struct ad7405_chip_info adum7702_chip_info = {
152 .name = "adum7702",
153 .full_scale_mv = 64,
154 };
155
156 static const struct ad7405_chip_info adum7703_chip_info = {
157 .name = "adum7703",
158 .full_scale_mv = 320,
159 };
160
161 static const char * const ad7405_power_supplies[] = {
162 "vdd1", "vdd2",
163 };
164
ad7405_probe(struct platform_device * pdev)165 static int ad7405_probe(struct platform_device *pdev)
166 {
167 struct device *dev = &pdev->dev;
168 struct iio_dev *indio_dev;
169 struct ad7405_state *st;
170 struct clk *clk;
171 int ret;
172
173 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
174 if (!indio_dev)
175 return -ENOMEM;
176
177 st = iio_priv(indio_dev);
178
179 st->info = device_get_match_data(dev);
180 if (!st->info)
181 return dev_err_probe(dev, -EINVAL, "no chip info\n");
182
183 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad7405_power_supplies),
184 ad7405_power_supplies);
185 if (ret)
186 return dev_err_probe(dev, ret, "failed to get and enable supplies");
187
188 clk = devm_clk_get_enabled(dev, NULL);
189 if (IS_ERR(clk))
190 return PTR_ERR(clk);
191
192 st->ref_frequency = clk_get_rate(clk);
193 if (!st->ref_frequency)
194 return -EINVAL;
195
196 indio_dev->name = st->info->name;
197 indio_dev->channels = &ad7405_channel;
198 indio_dev->num_channels = 1;
199 indio_dev->info = &ad7405_iio_info;
200
201 st->back = devm_iio_backend_get(dev, NULL);
202 if (IS_ERR(st->back))
203 return dev_err_probe(dev, PTR_ERR(st->back),
204 "failed to get IIO backend");
205
206 ret = iio_backend_chan_enable(st->back, 0);
207 if (ret)
208 return ret;
209
210 ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
211 if (ret)
212 return ret;
213
214 ret = devm_iio_backend_enable(dev, st->back);
215 if (ret)
216 return ret;
217
218 /*
219 * Set 256 decimation rate. The default value in the AXI_ADC register
220 * is 0, so we set the register with a decimation rate value that is
221 * functional for all parts.
222 */
223 ret = ad7405_set_dec_rate(indio_dev, &indio_dev->channels[0], 256);
224 if (ret)
225 return ret;
226
227 return devm_iio_device_register(dev, indio_dev);
228 }
229
230 static const struct of_device_id ad7405_of_match[] = {
231 { .compatible = "adi,ad7405", .data = &ad7405_chip_info, },
232 { .compatible = "adi,adum7701", .data = &adum7701_chip_info, },
233 { .compatible = "adi,adum7702", .data = &adum7702_chip_info, },
234 { .compatible = "adi,adum7703", .data = &adum7703_chip_info, },
235 { }
236 };
237 MODULE_DEVICE_TABLE(of, ad7405_of_match);
238
239 static struct platform_driver ad7405_driver = {
240 .driver = {
241 .name = "ad7405",
242 .of_match_table = ad7405_of_match,
243 },
244 .probe = ad7405_probe,
245 };
246 module_platform_driver(ad7405_driver);
247
248 MODULE_AUTHOR("Dragos Bogdan <dragos.bogdan@analog.com>");
249 MODULE_AUTHOR("Pop Ioan Daniel <pop.ioan-daniel@analog.com>");
250 MODULE_DESCRIPTION("Analog Devices AD7405 driver");
251 MODULE_LICENSE("GPL");
252 MODULE_IMPORT_NS("IIO_BACKEND");
253