1 // SPDX-License-Identifier: GPL-2.0
3 * HMC425A and similar Gain Amplifiers
5 * Copyright 2020 Analog Devices Inc.
8 #include <linux/bitops.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/sysfs.h>
29 struct hmc425a_chip_info {
31 const struct iio_chan_spec *channels;
32 unsigned int num_channels;
33 unsigned int num_gpios;
39 struct hmc425a_state {
40 struct mutex lock; /* protect sensor state */
41 struct hmc425a_chip_info *chip_info;
42 struct gpio_descs *gpios;
43 enum hmc425a_type type;
47 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
49 struct hmc425a_state *st = iio_priv(indio_dev);
50 DECLARE_BITMAP(values, BITS_PER_TYPE(value));
54 gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
59 static int hmc425a_read_raw(struct iio_dev *indio_dev,
60 struct iio_chan_spec const *chan, int *val,
63 struct hmc425a_state *st = iio_priv(indio_dev);
67 mutex_lock(&st->lock);
69 case IIO_CHAN_INFO_HARDWAREGAIN:
80 code = code & BIT(3) ? code & ~BIT(2) : code;
86 *val2 = (gain % 1000) * 1000;
88 ret = IIO_VAL_INT_PLUS_MICRO_DB;
93 mutex_unlock(&st->lock);
98 static int hmc425a_write_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan, int val,
102 struct hmc425a_state *st = iio_priv(indio_dev);
103 struct hmc425a_chip_info *inf = st->chip_info;
108 gain = (val * 1000) - (val2 / 1000);
110 gain = (val * 1000) + (val2 / 1000);
112 if (gain > inf->gain_max || gain < inf->gain_min)
117 code = ~((abs(gain) / 500) & 0x3F);
120 code = ~((abs(gain) / 1000) & 0xF);
123 code = (abs(gain) / 2000) & 0xF;
124 code = code & BIT(3) ? code | BIT(2) : code;
128 mutex_lock(&st->lock);
130 case IIO_CHAN_INFO_HARDWAREGAIN:
133 ret = hmc425a_write(indio_dev, st->gain);
138 mutex_unlock(&st->lock);
143 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
144 struct iio_chan_spec const *chan,
148 case IIO_CHAN_INFO_HARDWAREGAIN:
149 return IIO_VAL_INT_PLUS_MICRO_DB;
155 static const struct iio_info hmc425a_info = {
156 .read_raw = &hmc425a_read_raw,
157 .write_raw = &hmc425a_write_raw,
158 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
161 #define HMC425A_CHAN(_channel) \
163 .type = IIO_VOLTAGE, \
166 .channel = _channel, \
167 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
170 static const struct iio_chan_spec hmc425a_channels[] = {
174 /* Match table for of_platform binding */
175 static const struct of_device_id hmc425a_of_match[] = {
176 { .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
177 { .compatible = "adi,hmc540s", .data = (void *)ID_HMC540S },
178 { .compatible = "adi,adrf5740", .data = (void *)ID_ADRF5740 },
181 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
183 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
186 .channels = hmc425a_channels,
187 .num_channels = ARRAY_SIZE(hmc425a_channels),
191 .default_gain = -0x40, /* set default gain -31.5db*/
195 .channels = hmc425a_channels,
196 .num_channels = ARRAY_SIZE(hmc425a_channels),
200 .default_gain = -0x10, /* set default gain -15.0db*/
204 .channels = hmc425a_channels,
205 .num_channels = ARRAY_SIZE(hmc425a_channels),
209 .default_gain = 0xF, /* set default gain -22.0db*/
213 static int hmc425a_probe(struct platform_device *pdev)
215 struct iio_dev *indio_dev;
216 struct hmc425a_state *st;
219 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
223 st = iio_priv(indio_dev);
224 st->type = (uintptr_t)device_get_match_data(&pdev->dev);
226 st->chip_info = &hmc425a_chip_info_tbl[st->type];
227 indio_dev->num_channels = st->chip_info->num_channels;
228 indio_dev->channels = st->chip_info->channels;
229 indio_dev->name = st->chip_info->name;
230 st->gain = st->chip_info->default_gain;
232 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
233 if (IS_ERR(st->gpios))
234 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
235 "failed to get gpios\n");
237 if (st->gpios->ndescs != st->chip_info->num_gpios) {
238 dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
239 st->chip_info->num_gpios);
243 ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
247 mutex_init(&st->lock);
249 indio_dev->info = &hmc425a_info;
250 indio_dev->modes = INDIO_DIRECT_MODE;
252 /* Set default gain */
253 hmc425a_write(indio_dev, st->gain);
255 return devm_iio_device_register(&pdev->dev, indio_dev);
258 static struct platform_driver hmc425a_driver = {
260 .name = KBUILD_MODNAME,
261 .of_match_table = hmc425a_of_match,
263 .probe = hmc425a_probe,
265 module_platform_driver(hmc425a_driver);
267 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
268 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
269 MODULE_LICENSE("GPL v2");