WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / amplifiers / hmc425a.c
blob9efa692151f0f3b3e9bb3aad3096aac369feb009
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * HMC425A and similar Gain Amplifiers
5 * Copyright 2020 Analog Devices Inc.
6 */
8 #include <linux/device.h>
9 #include <linux/err.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/module.h>
15 #include <linux/of_device.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/sysfs.h>
22 enum hmc425a_type {
23 ID_HMC425A,
26 struct hmc425a_chip_info {
27 const char *name;
28 const struct iio_chan_spec *channels;
29 unsigned int num_channels;
30 unsigned int num_gpios;
31 int gain_min;
32 int gain_max;
33 int default_gain;
36 struct hmc425a_state {
37 struct regulator *reg;
38 struct mutex lock; /* protect sensor state */
39 struct hmc425a_chip_info *chip_info;
40 struct gpio_descs *gpios;
41 enum hmc425a_type type;
42 u32 gain;
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));
50 values[0] = value;
52 gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
53 NULL, values);
54 return 0;
57 static int hmc425a_read_raw(struct iio_dev *indio_dev,
58 struct iio_chan_spec const *chan, int *val,
59 int *val2, long m)
61 struct hmc425a_state *st = iio_priv(indio_dev);
62 int code, gain = 0;
63 int ret;
65 mutex_lock(&st->lock);
66 switch (m) {
67 case IIO_CHAN_INFO_HARDWAREGAIN:
68 code = st->gain;
70 switch (st->type) {
71 case ID_HMC425A:
72 gain = ~code * -500;
73 break;
76 *val = gain / 1000;
77 *val2 = (gain % 1000) * 1000;
79 ret = IIO_VAL_INT_PLUS_MICRO_DB;
80 break;
81 default:
82 ret = -EINVAL;
84 mutex_unlock(&st->lock);
86 return ret;
89 static int hmc425a_write_raw(struct iio_dev *indio_dev,
90 struct iio_chan_spec const *chan, int val,
91 int val2, long mask)
93 struct hmc425a_state *st = iio_priv(indio_dev);
94 struct hmc425a_chip_info *inf = st->chip_info;
95 int code = 0, gain;
96 int ret;
98 if (val < 0)
99 gain = (val * 1000) - (val2 / 1000);
100 else
101 gain = (val * 1000) + (val2 / 1000);
103 if (gain > inf->gain_max || gain < inf->gain_min)
104 return -EINVAL;
106 switch (st->type) {
107 case ID_HMC425A:
108 code = ~((abs(gain) / 500) & 0x3F);
109 break;
112 mutex_lock(&st->lock);
113 switch (mask) {
114 case IIO_CHAN_INFO_HARDWAREGAIN:
115 st->gain = code;
117 ret = hmc425a_write(indio_dev, st->gain);
118 break;
119 default:
120 ret = -EINVAL;
122 mutex_unlock(&st->lock);
124 return ret;
127 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
128 struct iio_chan_spec const *chan,
129 long mask)
131 switch (mask) {
132 case IIO_CHAN_INFO_HARDWAREGAIN:
133 return IIO_VAL_INT_PLUS_MICRO_DB;
134 default:
135 return -EINVAL;
139 static const struct iio_info hmc425a_info = {
140 .read_raw = &hmc425a_read_raw,
141 .write_raw = &hmc425a_write_raw,
142 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
145 #define HMC425A_CHAN(_channel) \
147 .type = IIO_VOLTAGE, \
148 .output = 1, \
149 .indexed = 1, \
150 .channel = _channel, \
151 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
154 static const struct iio_chan_spec hmc425a_channels[] = {
155 HMC425A_CHAN(0),
158 /* Match table for of_platform binding */
159 static const struct of_device_id hmc425a_of_match[] = {
160 { .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
163 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
165 static void hmc425a_reg_disable(void *data)
167 struct hmc425a_state *st = data;
169 regulator_disable(st->reg);
172 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
173 [ID_HMC425A] = {
174 .name = "hmc425a",
175 .channels = hmc425a_channels,
176 .num_channels = ARRAY_SIZE(hmc425a_channels),
177 .num_gpios = 6,
178 .gain_min = -31500,
179 .gain_max = 0,
180 .default_gain = -0x40, /* set default gain -31.5db*/
184 static int hmc425a_probe(struct platform_device *pdev)
186 struct iio_dev *indio_dev;
187 struct hmc425a_state *st;
188 int ret;
190 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
191 if (!indio_dev)
192 return -ENOMEM;
194 st = iio_priv(indio_dev);
195 st->type = (enum hmc425a_type)of_device_get_match_data(&pdev->dev);
197 st->chip_info = &hmc425a_chip_info_tbl[st->type];
198 indio_dev->num_channels = st->chip_info->num_channels;
199 indio_dev->channels = st->chip_info->channels;
200 indio_dev->name = st->chip_info->name;
201 st->gain = st->chip_info->default_gain;
203 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
204 if (IS_ERR(st->gpios))
205 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
206 "failed to get gpios\n");
208 if (st->gpios->ndescs != st->chip_info->num_gpios) {
209 dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
210 st->chip_info->num_gpios);
211 return -ENODEV;
214 st->reg = devm_regulator_get(&pdev->dev, "vcc-supply");
215 if (IS_ERR(st->reg))
216 return PTR_ERR(st->reg);
218 ret = regulator_enable(st->reg);
219 if (ret)
220 return ret;
221 ret = devm_add_action_or_reset(&pdev->dev, hmc425a_reg_disable, st);
222 if (ret)
223 return ret;
225 mutex_init(&st->lock);
227 indio_dev->info = &hmc425a_info;
228 indio_dev->modes = INDIO_DIRECT_MODE;
230 return devm_iio_device_register(&pdev->dev, indio_dev);
233 static struct platform_driver hmc425a_driver = {
234 .driver = {
235 .name = KBUILD_MODNAME,
236 .of_match_table = hmc425a_of_match,
238 .probe = hmc425a_probe,
240 module_platform_driver(hmc425a_driver);
242 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
243 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
244 MODULE_LICENSE("GPL v2");