1 // SPDX-License-Identifier: GPL-2.0
3 * HMC425A and similar Gain Amplifiers
5 * Copyright 2020, 2024 Analog Devices Inc.
8 #include <linux/bits.h>
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/kernel.h>
16 #include <linux/math.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/sysfs.h>
26 * The LTC6373 amplifier supports configuring gain using GPIO's with the following
27 * values (OUTPUT_V / INPUT_V): 0(shutdown), 0.25, 0.5, 1, 2, 4, 8, 16
29 * Except for the shutdown value, all can be converted to dB using 20 * log10(x)
30 * From here, it is observed that all values are multiples of the '2' gain setting,
31 * with the correspondent of 6.020dB.
33 #define LTC6373_CONVERSION_CONSTANT 6020
34 #define LTC6373_MIN_GAIN_CODE 0x6
35 #define LTC6373_CONVERSION_MASK GENMASK(2, 0)
36 #define LTC6373_SHUTDOWN GENMASK(2, 0)
45 struct hmc425a_chip_info {
47 const struct iio_chan_spec *channels;
48 unsigned int num_channels;
49 unsigned int num_gpios;
56 int (*gain_dB_to_code)(int gain, int *code);
57 int (*code_to_gain_dB)(int code, int *val, int *val2);
60 struct hmc425a_state {
61 struct mutex lock; /* protect sensor state */
62 const struct hmc425a_chip_info *chip_info;
63 struct gpio_descs *gpios;
68 static int gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code)
70 const struct hmc425a_chip_info *inf = st->chip_info;
74 gain = (val * 1000) - (val2 / 1000);
76 gain = (val * 1000) + (val2 / 1000);
78 if (gain > inf->gain_max || gain < inf->gain_min)
83 return st->chip_info->gain_dB_to_code(gain, code);
86 static int hmc425a_gain_dB_to_code(int gain, int *code)
88 *code = ~((abs(gain) / 500) & 0x3F);
92 static int hmc540s_gain_dB_to_code(int gain, int *code)
94 *code = ~((abs(gain) / 1000) & 0xF);
98 static int adrf5740_gain_dB_to_code(int gain, int *code)
100 int temp = (abs(gain) / 2000) & 0xF;
102 /* Bit [0-3]: 2dB 4dB 8dB 8dB */
103 *code = temp & BIT(3) ? temp | BIT(2) : temp;
107 static int ltc6373_gain_dB_to_code(int gain, int *code)
109 *code = ~(DIV_ROUND_CLOSEST(gain, LTC6373_CONVERSION_CONSTANT) + 3)
110 & LTC6373_CONVERSION_MASK;
114 static int code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2)
118 return st->chip_info->code_to_gain_dB(st->gain, val, val2);
121 static int hmc425a_code_to_gain_dB(int code, int *val, int *val2)
123 *val = (~code * -500) / 1000;
124 *val2 = ((~code * -500) % 1000) * 1000;
128 static int hmc540s_code_to_gain_dB(int code, int *val, int *val2)
130 *val = (~code * -1000) / 1000;
131 *val2 = ((~code * -1000) % 1000) * 1000;
135 static int adrf5740_code_to_gain_dB(int code, int *val, int *val2)
138 * Bit [0-3]: 2dB 4dB 8dB 8dB
139 * When BIT(3) is set, unset BIT(2) and use 3 as double the place value
141 code = code & BIT(3) ? code & ~BIT(2) : code;
142 *val = (code * -2000) / 1000;
143 *val2 = ((code * -2000) % 1000) * 1000;
147 static int ltc6373_code_to_gain_dB(int code, int *val, int *val2)
149 int gain = ((~code & LTC6373_CONVERSION_MASK) - 3) *
150 LTC6373_CONVERSION_CONSTANT;
153 *val2 = (gain % 1000) * 1000;
157 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
159 struct hmc425a_state *st = iio_priv(indio_dev);
160 DECLARE_BITMAP(values, BITS_PER_TYPE(value));
164 gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
169 static int hmc425a_read_raw(struct iio_dev *indio_dev,
170 struct iio_chan_spec const *chan, int *val,
173 struct hmc425a_state *st = iio_priv(indio_dev);
176 mutex_lock(&st->lock);
178 case IIO_CHAN_INFO_HARDWAREGAIN:
179 ret = code_to_gain_dB(st, val, val2);
182 ret = IIO_VAL_INT_PLUS_MICRO_DB;
187 mutex_unlock(&st->lock);
192 static int hmc425a_write_raw(struct iio_dev *indio_dev,
193 struct iio_chan_spec const *chan, int val,
196 struct hmc425a_state *st = iio_priv(indio_dev);
199 mutex_lock(&st->lock);
201 case IIO_CHAN_INFO_HARDWAREGAIN:
202 ret = gain_dB_to_code(st, val, val2, &code);
207 ret = hmc425a_write(indio_dev, st->gain);
212 mutex_unlock(&st->lock);
217 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
218 struct iio_chan_spec const *chan,
222 case IIO_CHAN_INFO_HARDWAREGAIN:
223 return IIO_VAL_INT_PLUS_MICRO_DB;
229 static const struct iio_info hmc425a_info = {
230 .read_raw = &hmc425a_read_raw,
231 .write_raw = &hmc425a_write_raw,
232 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
235 static ssize_t ltc6373_read_powerdown(struct iio_dev *indio_dev,
237 const struct iio_chan_spec *chan,
240 struct hmc425a_state *st = iio_priv(indio_dev);
242 return sysfs_emit(buf, "%d\n", st->powerdown);
245 static ssize_t ltc6373_write_powerdown(struct iio_dev *indio_dev,
247 const struct iio_chan_spec *chan,
251 struct hmc425a_state *st = iio_priv(indio_dev);
255 ret = kstrtobool(buf, &powerdown);
259 mutex_lock(&st->lock);
260 st->powerdown = powerdown;
261 code = (powerdown) ? LTC6373_SHUTDOWN : st->gain;
262 hmc425a_write(indio_dev, code);
263 mutex_unlock(&st->lock);
267 static const struct iio_chan_spec_ext_info ltc6373_ext_info[] = {
270 .read = ltc6373_read_powerdown,
271 .write = ltc6373_write_powerdown,
272 .shared = IIO_SEPARATE,
277 #define HMC425A_CHAN(_channel) \
279 .type = IIO_VOLTAGE, \
282 .channel = _channel, \
283 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
286 #define LTC6373_CHAN(_channel) \
288 .type = IIO_VOLTAGE, \
291 .channel = _channel, \
292 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
293 .ext_info = ltc6373_ext_info, \
296 static const struct iio_chan_spec hmc425a_channels[] = {
300 static const struct iio_chan_spec ltc6373_channels[] = {
304 static const struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
307 .channels = hmc425a_channels,
308 .num_channels = ARRAY_SIZE(hmc425a_channels),
312 .default_gain = -0x40, /* set default gain -31.5db*/
313 .gain_dB_to_code = hmc425a_gain_dB_to_code,
314 .code_to_gain_dB = hmc425a_code_to_gain_dB,
318 .channels = hmc425a_channels,
319 .num_channels = ARRAY_SIZE(hmc425a_channels),
323 .default_gain = -0x10, /* set default gain -15.0db*/
324 .gain_dB_to_code = hmc540s_gain_dB_to_code,
325 .code_to_gain_dB = hmc540s_code_to_gain_dB,
329 .channels = hmc425a_channels,
330 .num_channels = ARRAY_SIZE(hmc425a_channels),
334 .default_gain = 0xF, /* set default gain -22.0db*/
335 .gain_dB_to_code = adrf5740_gain_dB_to_code,
336 .code_to_gain_dB = adrf5740_code_to_gain_dB,
340 .channels = ltc6373_channels,
341 .num_channels = ARRAY_SIZE(ltc6373_channels),
343 .gain_min = -12041, /* gain setting x0.25*/
344 .gain_max = 24082, /* gain setting x16 */
345 .default_gain = LTC6373_MIN_GAIN_CODE,
346 .powerdown_val = LTC6373_SHUTDOWN,
347 .has_powerdown = true,
348 .gain_dB_to_code = ltc6373_gain_dB_to_code,
349 .code_to_gain_dB = ltc6373_code_to_gain_dB,
353 static int hmc425a_probe(struct platform_device *pdev)
355 struct iio_dev *indio_dev;
356 struct hmc425a_state *st;
359 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
363 st = iio_priv(indio_dev);
365 st->chip_info = device_get_match_data(&pdev->dev);
366 indio_dev->num_channels = st->chip_info->num_channels;
367 indio_dev->channels = st->chip_info->channels;
368 indio_dev->name = st->chip_info->name;
369 st->gain = st->chip_info->default_gain;
371 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
372 if (IS_ERR(st->gpios))
373 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
374 "failed to get gpios\n");
376 if (st->gpios->ndescs != st->chip_info->num_gpios) {
377 dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
378 st->chip_info->num_gpios);
382 ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
386 mutex_init(&st->lock);
388 indio_dev->info = &hmc425a_info;
389 indio_dev->modes = INDIO_DIRECT_MODE;
391 if (st->chip_info->has_powerdown) {
392 st->powerdown = true;
393 hmc425a_write(indio_dev, st->chip_info->powerdown_val);
395 /* Set default gain */
396 hmc425a_write(indio_dev, st->gain);
399 return devm_iio_device_register(&pdev->dev, indio_dev);
402 /* Match table for of_platform binding */
403 static const struct of_device_id hmc425a_of_match[] = {
404 { .compatible = "adi,hmc425a",
405 .data = &hmc425a_chip_info_tbl[ID_HMC425A]},
406 { .compatible = "adi,hmc540s",
407 .data = &hmc425a_chip_info_tbl[ID_HMC540S]},
408 { .compatible = "adi,adrf5740",
409 .data = &hmc425a_chip_info_tbl[ID_ADRF5740]},
410 { .compatible = "adi,ltc6373",
411 .data = &hmc425a_chip_info_tbl[ID_LTC6373]},
414 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
416 static struct platform_driver hmc425a_driver = {
418 .name = KBUILD_MODNAME,
419 .of_match_table = hmc425a_of_match,
421 .probe = hmc425a_probe,
423 module_platform_driver(hmc425a_driver);
425 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
426 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
427 MODULE_LICENSE("GPL v2");