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