1f665d7d3SAngelo Dureghello // SPDX-License-Identifier: GPL-2.0+
2f665d7d3SAngelo Dureghello //
3f665d7d3SAngelo Dureghello // Copyright (c) 2010-2024 Analog Devices Inc.
4f665d7d3SAngelo Dureghello // Copyright (c) 2024 Baylibre, SAS
5f665d7d3SAngelo Dureghello
6f665d7d3SAngelo Dureghello #include <linux/bitfield.h>
7f665d7d3SAngelo Dureghello #include <linux/device.h>
8f665d7d3SAngelo Dureghello #include <linux/module.h>
9f665d7d3SAngelo Dureghello #include <linux/property.h>
10f665d7d3SAngelo Dureghello #include <linux/regulator/consumer.h>
11f665d7d3SAngelo Dureghello
12f665d7d3SAngelo Dureghello #include "ad3552r.h"
13f665d7d3SAngelo Dureghello
14f665d7d3SAngelo Dureghello const s32 ad3552r_ch_ranges[AD3552R_MAX_RANGES][2] = {
15f665d7d3SAngelo Dureghello [AD3552R_CH_OUTPUT_RANGE_0__2P5V] = { 0, 2500 },
16f665d7d3SAngelo Dureghello [AD3552R_CH_OUTPUT_RANGE_0__5V] = { 0, 5000 },
17f665d7d3SAngelo Dureghello [AD3552R_CH_OUTPUT_RANGE_0__10V] = { 0, 10000 },
18f665d7d3SAngelo Dureghello [AD3552R_CH_OUTPUT_RANGE_NEG_5__5V] = { -5000, 5000 },
19f665d7d3SAngelo Dureghello [AD3552R_CH_OUTPUT_RANGE_NEG_10__10V] = { -10000, 10000 }
20f665d7d3SAngelo Dureghello };
21*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_ch_ranges, "IIO_AD3552R");
22f665d7d3SAngelo Dureghello
23f665d7d3SAngelo Dureghello const s32 ad3542r_ch_ranges[AD3542R_MAX_RANGES][2] = {
24f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_0__2P5V] = { 0, 2500 },
25f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_0__3V] = { 0, 3000 },
26f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_0__5V] = { 0, 5000 },
27f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_0__10V] = { 0, 10000 },
28f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_NEG_2P5__7P5V] = { -2500, 7500 },
29f665d7d3SAngelo Dureghello [AD3542R_CH_OUTPUT_RANGE_NEG_5__5V] = { -5000, 5000 }
30f665d7d3SAngelo Dureghello };
31*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3542r_ch_ranges, "IIO_AD3552R");
32f665d7d3SAngelo Dureghello
33f665d7d3SAngelo Dureghello /* Gain * AD3552R_GAIN_SCALE */
34f665d7d3SAngelo Dureghello static const s32 gains_scaling_table[] = {
35f665d7d3SAngelo Dureghello [AD3552R_CH_GAIN_SCALING_1] = 1000,
36f665d7d3SAngelo Dureghello [AD3552R_CH_GAIN_SCALING_0_5] = 500,
37f665d7d3SAngelo Dureghello [AD3552R_CH_GAIN_SCALING_0_25] = 250,
38f665d7d3SAngelo Dureghello [AD3552R_CH_GAIN_SCALING_0_125] = 125
39f665d7d3SAngelo Dureghello };
40f665d7d3SAngelo Dureghello
ad3552r_calc_custom_gain(u8 p,u8 n,s16 goffs)41f665d7d3SAngelo Dureghello u16 ad3552r_calc_custom_gain(u8 p, u8 n, s16 goffs)
42f665d7d3SAngelo Dureghello {
43f665d7d3SAngelo Dureghello return FIELD_PREP(AD3552R_MASK_CH_RANGE_OVERRIDE, 1) |
44f665d7d3SAngelo Dureghello FIELD_PREP(AD3552R_MASK_CH_GAIN_SCALING_P, p) |
45f665d7d3SAngelo Dureghello FIELD_PREP(AD3552R_MASK_CH_GAIN_SCALING_N, n) |
46f665d7d3SAngelo Dureghello FIELD_PREP(AD3552R_MASK_CH_OFFSET_BIT_8, abs(goffs)) |
47f665d7d3SAngelo Dureghello FIELD_PREP(AD3552R_MASK_CH_OFFSET_POLARITY, goffs < 0);
48f665d7d3SAngelo Dureghello }
49*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_calc_custom_gain, "IIO_AD3552R");
50f665d7d3SAngelo Dureghello
ad3552r_get_custom_range(struct ad3552r_ch_data * ch_data,s32 * v_min,s32 * v_max)51f665d7d3SAngelo Dureghello static void ad3552r_get_custom_range(struct ad3552r_ch_data *ch_data,
52f665d7d3SAngelo Dureghello s32 *v_min, s32 *v_max)
53f665d7d3SAngelo Dureghello {
54f665d7d3SAngelo Dureghello s64 vref, tmp, common, offset, gn, gp;
55f665d7d3SAngelo Dureghello /*
56f665d7d3SAngelo Dureghello * From datasheet formula (In Volts):
57f665d7d3SAngelo Dureghello * Vmin = 2.5 + [(GainN + Offset / 1024) * 2.5 * Rfb * 1.03]
58f665d7d3SAngelo Dureghello * Vmax = 2.5 - [(GainP + Offset / 1024) * 2.5 * Rfb * 1.03]
59f665d7d3SAngelo Dureghello * Calculus are converted to milivolts
60f665d7d3SAngelo Dureghello */
61f665d7d3SAngelo Dureghello vref = 2500;
62f665d7d3SAngelo Dureghello /* 2.5 * 1.03 * 1000 (To mV) */
63f665d7d3SAngelo Dureghello common = 2575 * ch_data->rfb;
64f665d7d3SAngelo Dureghello offset = ch_data->gain_offset;
65f665d7d3SAngelo Dureghello
66f665d7d3SAngelo Dureghello gn = gains_scaling_table[ch_data->n];
67f665d7d3SAngelo Dureghello tmp = (1024 * gn + AD3552R_GAIN_SCALE * offset) * common;
68f665d7d3SAngelo Dureghello tmp = div_s64(tmp, 1024 * AD3552R_GAIN_SCALE);
69f665d7d3SAngelo Dureghello *v_max = vref + tmp;
70f665d7d3SAngelo Dureghello
71f665d7d3SAngelo Dureghello gp = gains_scaling_table[ch_data->p];
72f665d7d3SAngelo Dureghello tmp = (1024 * gp - AD3552R_GAIN_SCALE * offset) * common;
73f665d7d3SAngelo Dureghello tmp = div_s64(tmp, 1024 * AD3552R_GAIN_SCALE);
74f665d7d3SAngelo Dureghello *v_min = vref - tmp;
75f665d7d3SAngelo Dureghello }
76f665d7d3SAngelo Dureghello
ad3552r_calc_gain_and_offset(struct ad3552r_ch_data * ch_data,const struct ad3552r_model_data * model_data)77f665d7d3SAngelo Dureghello void ad3552r_calc_gain_and_offset(struct ad3552r_ch_data *ch_data,
78f665d7d3SAngelo Dureghello const struct ad3552r_model_data *model_data)
79f665d7d3SAngelo Dureghello {
80f665d7d3SAngelo Dureghello s32 idx, v_max, v_min, span, rem;
81f665d7d3SAngelo Dureghello s64 tmp;
82f665d7d3SAngelo Dureghello
83f665d7d3SAngelo Dureghello if (ch_data->range_override) {
84f665d7d3SAngelo Dureghello ad3552r_get_custom_range(ch_data, &v_min, &v_max);
85f665d7d3SAngelo Dureghello } else {
86f665d7d3SAngelo Dureghello /* Normal range */
87f665d7d3SAngelo Dureghello idx = ch_data->range;
88f665d7d3SAngelo Dureghello v_min = model_data->ranges_table[idx][0];
89f665d7d3SAngelo Dureghello v_max = model_data->ranges_table[idx][1];
90f665d7d3SAngelo Dureghello }
91f665d7d3SAngelo Dureghello
92f665d7d3SAngelo Dureghello /*
93f665d7d3SAngelo Dureghello * From datasheet formula:
94f665d7d3SAngelo Dureghello * Vout = Span * (D / 65536) + Vmin
95f665d7d3SAngelo Dureghello * Converted to scale and offset:
96f665d7d3SAngelo Dureghello * Scale = Span / 65536
97f665d7d3SAngelo Dureghello * Offset = 65536 * Vmin / Span
98f665d7d3SAngelo Dureghello *
99f665d7d3SAngelo Dureghello * Reminders are in micros in order to be printed as
100f665d7d3SAngelo Dureghello * IIO_VAL_INT_PLUS_MICRO
101f665d7d3SAngelo Dureghello */
102f665d7d3SAngelo Dureghello span = v_max - v_min;
103f665d7d3SAngelo Dureghello ch_data->scale_int = div_s64_rem(span, 65536, &rem);
104f665d7d3SAngelo Dureghello /* Do operations in microvolts */
105f665d7d3SAngelo Dureghello ch_data->scale_dec = DIV_ROUND_CLOSEST((s64)rem * 1000000, 65536);
106f665d7d3SAngelo Dureghello
107f665d7d3SAngelo Dureghello ch_data->offset_int = div_s64_rem(v_min * 65536, span, &rem);
108f665d7d3SAngelo Dureghello tmp = (s64)rem * 1000000;
109f665d7d3SAngelo Dureghello ch_data->offset_dec = div_s64(tmp, span);
110f665d7d3SAngelo Dureghello }
111*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_calc_gain_and_offset, "IIO_AD3552R");
112f665d7d3SAngelo Dureghello
ad3552r_get_ref_voltage(struct device * dev,u32 * val)113f665d7d3SAngelo Dureghello int ad3552r_get_ref_voltage(struct device *dev, u32 *val)
114f665d7d3SAngelo Dureghello {
115f665d7d3SAngelo Dureghello int voltage;
116f665d7d3SAngelo Dureghello int delta = 100000;
117f665d7d3SAngelo Dureghello
118f665d7d3SAngelo Dureghello voltage = devm_regulator_get_enable_read_voltage(dev, "vref");
119f665d7d3SAngelo Dureghello if (voltage < 0 && voltage != -ENODEV)
120f665d7d3SAngelo Dureghello return dev_err_probe(dev, voltage,
121f665d7d3SAngelo Dureghello "Error getting vref voltage\n");
122f665d7d3SAngelo Dureghello
123f665d7d3SAngelo Dureghello if (voltage == -ENODEV) {
124f665d7d3SAngelo Dureghello if (device_property_read_bool(dev, "adi,vref-out-en"))
125f665d7d3SAngelo Dureghello *val = AD3552R_INTERNAL_VREF_PIN_2P5V;
126f665d7d3SAngelo Dureghello else
127f665d7d3SAngelo Dureghello *val = AD3552R_INTERNAL_VREF_PIN_FLOATING;
128f665d7d3SAngelo Dureghello
129f665d7d3SAngelo Dureghello return 0;
130f665d7d3SAngelo Dureghello }
131f665d7d3SAngelo Dureghello
132f665d7d3SAngelo Dureghello if (voltage > 2500000 + delta || voltage < 2500000 - delta) {
133f665d7d3SAngelo Dureghello dev_warn(dev, "vref-supply must be 2.5V");
134f665d7d3SAngelo Dureghello return -EINVAL;
135f665d7d3SAngelo Dureghello }
136f665d7d3SAngelo Dureghello
137f665d7d3SAngelo Dureghello *val = AD3552R_EXTERNAL_VREF_PIN_INPUT;
138f665d7d3SAngelo Dureghello
139f665d7d3SAngelo Dureghello return 0;
140f665d7d3SAngelo Dureghello }
141*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_get_ref_voltage, "IIO_AD3552R");
142f665d7d3SAngelo Dureghello
ad3552r_get_drive_strength(struct device * dev,u32 * val)143f665d7d3SAngelo Dureghello int ad3552r_get_drive_strength(struct device *dev, u32 *val)
144f665d7d3SAngelo Dureghello {
145f665d7d3SAngelo Dureghello int err;
146f665d7d3SAngelo Dureghello u32 drive_strength;
147f665d7d3SAngelo Dureghello
148f665d7d3SAngelo Dureghello err = device_property_read_u32(dev, "adi,sdo-drive-strength",
149f665d7d3SAngelo Dureghello &drive_strength);
150f665d7d3SAngelo Dureghello if (err)
151f665d7d3SAngelo Dureghello return err;
152f665d7d3SAngelo Dureghello
153f665d7d3SAngelo Dureghello if (drive_strength > 3) {
154f665d7d3SAngelo Dureghello dev_err_probe(dev, -EINVAL,
155f665d7d3SAngelo Dureghello "adi,sdo-drive-strength must be less than 4\n");
156f665d7d3SAngelo Dureghello return -EINVAL;
157f665d7d3SAngelo Dureghello }
158f665d7d3SAngelo Dureghello
159f665d7d3SAngelo Dureghello *val = drive_strength;
160f665d7d3SAngelo Dureghello
161f665d7d3SAngelo Dureghello return 0;
162f665d7d3SAngelo Dureghello }
163*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_get_drive_strength, "IIO_AD3552R");
164f665d7d3SAngelo Dureghello
ad3552r_get_custom_gain(struct device * dev,struct fwnode_handle * child,u8 * gs_p,u8 * gs_n,u16 * rfb,s16 * goffs)165f665d7d3SAngelo Dureghello int ad3552r_get_custom_gain(struct device *dev, struct fwnode_handle *child,
166f665d7d3SAngelo Dureghello u8 *gs_p, u8 *gs_n, u16 *rfb, s16 *goffs)
167f665d7d3SAngelo Dureghello {
168f665d7d3SAngelo Dureghello int err;
169f665d7d3SAngelo Dureghello u32 val;
170f665d7d3SAngelo Dureghello struct fwnode_handle *gain_child __free(fwnode_handle) =
171f665d7d3SAngelo Dureghello fwnode_get_named_child_node(child,
172f665d7d3SAngelo Dureghello "custom-output-range-config");
173f665d7d3SAngelo Dureghello
174f665d7d3SAngelo Dureghello if (!gain_child)
175f665d7d3SAngelo Dureghello return dev_err_probe(dev, -EINVAL,
176f665d7d3SAngelo Dureghello "custom-output-range-config mandatory\n");
177f665d7d3SAngelo Dureghello
178f665d7d3SAngelo Dureghello err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-p", &val);
179f665d7d3SAngelo Dureghello if (err)
180f665d7d3SAngelo Dureghello return dev_err_probe(dev, err,
181f665d7d3SAngelo Dureghello "adi,gain-scaling-p mandatory\n");
182f665d7d3SAngelo Dureghello *gs_p = val;
183f665d7d3SAngelo Dureghello
184f665d7d3SAngelo Dureghello err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-n", &val);
185f665d7d3SAngelo Dureghello if (err)
186f665d7d3SAngelo Dureghello return dev_err_probe(dev, err,
187f665d7d3SAngelo Dureghello "adi,gain-scaling-n property mandatory\n");
188f665d7d3SAngelo Dureghello *gs_n = val;
189f665d7d3SAngelo Dureghello
190f665d7d3SAngelo Dureghello err = fwnode_property_read_u32(gain_child, "adi,rfb-ohms", &val);
191f665d7d3SAngelo Dureghello if (err)
192f665d7d3SAngelo Dureghello return dev_err_probe(dev, err,
193f665d7d3SAngelo Dureghello "adi,rfb-ohms mandatory\n");
194f665d7d3SAngelo Dureghello *rfb = val;
195f665d7d3SAngelo Dureghello
196f665d7d3SAngelo Dureghello err = fwnode_property_read_u32(gain_child, "adi,gain-offset", &val);
197f665d7d3SAngelo Dureghello if (err)
198f665d7d3SAngelo Dureghello return dev_err_probe(dev, err,
199f665d7d3SAngelo Dureghello "adi,gain-offset mandatory\n");
200f665d7d3SAngelo Dureghello *goffs = val;
201f665d7d3SAngelo Dureghello
202f665d7d3SAngelo Dureghello return 0;
203f665d7d3SAngelo Dureghello }
204*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_get_custom_gain, "IIO_AD3552R");
205f665d7d3SAngelo Dureghello
ad3552r_find_range(const struct ad3552r_model_data * model_info,s32 * vals)206f665d7d3SAngelo Dureghello static int ad3552r_find_range(const struct ad3552r_model_data *model_info,
207f665d7d3SAngelo Dureghello s32 *vals)
208f665d7d3SAngelo Dureghello {
209f665d7d3SAngelo Dureghello int i;
210f665d7d3SAngelo Dureghello
211f665d7d3SAngelo Dureghello for (i = 0; i < model_info->num_ranges; i++)
212f665d7d3SAngelo Dureghello if (vals[0] == model_info->ranges_table[i][0] * 1000 &&
213f665d7d3SAngelo Dureghello vals[1] == model_info->ranges_table[i][1] * 1000)
214f665d7d3SAngelo Dureghello return i;
215f665d7d3SAngelo Dureghello
216f665d7d3SAngelo Dureghello return -EINVAL;
217f665d7d3SAngelo Dureghello }
218f665d7d3SAngelo Dureghello
ad3552r_get_output_range(struct device * dev,const struct ad3552r_model_data * model_info,struct fwnode_handle * child,u32 * val)219f665d7d3SAngelo Dureghello int ad3552r_get_output_range(struct device *dev,
220f665d7d3SAngelo Dureghello const struct ad3552r_model_data *model_info,
221f665d7d3SAngelo Dureghello struct fwnode_handle *child, u32 *val)
222f665d7d3SAngelo Dureghello {
223f665d7d3SAngelo Dureghello int ret;
224f665d7d3SAngelo Dureghello s32 vals[2];
225f665d7d3SAngelo Dureghello
226f665d7d3SAngelo Dureghello /* This property is optional, so returning -ENOENT if missing */
227f665d7d3SAngelo Dureghello if (!fwnode_property_present(child, "adi,output-range-microvolt"))
228f665d7d3SAngelo Dureghello return -ENOENT;
229f665d7d3SAngelo Dureghello
230f665d7d3SAngelo Dureghello ret = fwnode_property_read_u32_array(child,
231f665d7d3SAngelo Dureghello "adi,output-range-microvolt",
232f665d7d3SAngelo Dureghello vals, 2);
233f665d7d3SAngelo Dureghello if (ret)
234f665d7d3SAngelo Dureghello return dev_err_probe(dev, ret,
235f665d7d3SAngelo Dureghello "invalid adi,output-range-microvolt\n");
236f665d7d3SAngelo Dureghello
237f665d7d3SAngelo Dureghello ret = ad3552r_find_range(model_info, vals);
238f665d7d3SAngelo Dureghello if (ret < 0)
239f665d7d3SAngelo Dureghello return dev_err_probe(dev, ret,
240f665d7d3SAngelo Dureghello "invalid adi,output-range-microvolt value\n");
241f665d7d3SAngelo Dureghello
242f665d7d3SAngelo Dureghello *val = ret;
243f665d7d3SAngelo Dureghello
244f665d7d3SAngelo Dureghello return 0;
245f665d7d3SAngelo Dureghello }
246*cdd30ebbSPeter Zijlstra EXPORT_SYMBOL_NS_GPL(ad3552r_get_output_range, "IIO_AD3552R");
247f665d7d3SAngelo Dureghello
248f665d7d3SAngelo Dureghello MODULE_DESCRIPTION("ad3552r common functions");
249f665d7d3SAngelo Dureghello MODULE_LICENSE("GPL");
250