1 // SPDX-License-Identifier: GPL-2.0
3 * HMC425A and similar Gain Amplifiers
5 * Copyright 2020 Analog Devices Inc.
8 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/sysfs.h>
13 #include <linux/kernel.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/sysfs.h>
27 struct hmc425a_chip_info {
29 const struct iio_chan_spec *channels;
30 unsigned int num_channels;
31 unsigned int num_gpios;
37 struct hmc425a_state {
38 struct mutex lock; /* protect sensor state */
39 struct hmc425a_chip_info *chip_info;
40 struct gpio_descs *gpios;
41 enum hmc425a_type type;
45 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
47 struct hmc425a_state *st = iio_priv(indio_dev);
48 DECLARE_BITMAP(values, BITS_PER_TYPE(value));
52 gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
57 static int hmc425a_read_raw(struct iio_dev *indio_dev,
58 struct iio_chan_spec const *chan, int *val,
61 struct hmc425a_state *st = iio_priv(indio_dev);
65 mutex_lock(&st->lock);
67 case IIO_CHAN_INFO_HARDWAREGAIN:
80 *val2 = (gain % 1000) * 1000;
82 ret = IIO_VAL_INT_PLUS_MICRO_DB;
87 mutex_unlock(&st->lock);
92 static int hmc425a_write_raw(struct iio_dev *indio_dev,
93 struct iio_chan_spec const *chan, int val,
96 struct hmc425a_state *st = iio_priv(indio_dev);
97 struct hmc425a_chip_info *inf = st->chip_info;
102 gain = (val * 1000) - (val2 / 1000);
104 gain = (val * 1000) + (val2 / 1000);
106 if (gain > inf->gain_max || gain < inf->gain_min)
111 code = ~((abs(gain) / 500) & 0x3F);
114 code = ~((abs(gain) / 1000) & 0xF);
118 mutex_lock(&st->lock);
120 case IIO_CHAN_INFO_HARDWAREGAIN:
123 ret = hmc425a_write(indio_dev, st->gain);
128 mutex_unlock(&st->lock);
133 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
134 struct iio_chan_spec const *chan,
138 case IIO_CHAN_INFO_HARDWAREGAIN:
139 return IIO_VAL_INT_PLUS_MICRO_DB;
145 static const struct iio_info hmc425a_info = {
146 .read_raw = &hmc425a_read_raw,
147 .write_raw = &hmc425a_write_raw,
148 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
151 #define HMC425A_CHAN(_channel) \
153 .type = IIO_VOLTAGE, \
156 .channel = _channel, \
157 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
160 static const struct iio_chan_spec hmc425a_channels[] = {
164 /* Match table for of_platform binding */
165 static const struct of_device_id hmc425a_of_match[] = {
166 { .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
167 { .compatible = "adi,hmc540s", .data = (void *)ID_HMC540S },
170 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
172 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
175 .channels = hmc425a_channels,
176 .num_channels = ARRAY_SIZE(hmc425a_channels),
180 .default_gain = -0x40, /* set default gain -31.5db*/
184 .channels = hmc425a_channels,
185 .num_channels = ARRAY_SIZE(hmc425a_channels),
189 .default_gain = -0x10, /* set default gain -15.0db*/
193 static int hmc425a_probe(struct platform_device *pdev)
195 struct iio_dev *indio_dev;
196 struct hmc425a_state *st;
199 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
203 st = iio_priv(indio_dev);
204 st->type = (uintptr_t)device_get_match_data(&pdev->dev);
206 st->chip_info = &hmc425a_chip_info_tbl[st->type];
207 indio_dev->num_channels = st->chip_info->num_channels;
208 indio_dev->channels = st->chip_info->channels;
209 indio_dev->name = st->chip_info->name;
210 st->gain = st->chip_info->default_gain;
212 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
213 if (IS_ERR(st->gpios))
214 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
215 "failed to get gpios\n");
217 if (st->gpios->ndescs != st->chip_info->num_gpios) {
218 dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
219 st->chip_info->num_gpios);
223 ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
227 mutex_init(&st->lock);
229 indio_dev->info = &hmc425a_info;
230 indio_dev->modes = INDIO_DIRECT_MODE;
232 return devm_iio_device_register(&pdev->dev, indio_dev);
235 static struct platform_driver hmc425a_driver = {
237 .name = KBUILD_MODNAME,
238 .of_match_table = hmc425a_of_match,
240 .probe = hmc425a_probe,
242 module_platform_driver(hmc425a_driver);
244 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
245 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
246 MODULE_LICENSE("GPL v2");