xref: /openbmc/linux/drivers/iio/amplifiers/hmc425a.c (revision 11976fe2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * HMC425A and similar Gain Amplifiers
4  *
5  * Copyright 2020 Analog Devices Inc.
6  */
7 
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/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>
21 
22 enum hmc425a_type {
23 	ID_HMC425A,
24 };
25 
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;
34 };
35 
36 struct hmc425a_state {
37 	struct	mutex lock; /* protect sensor state */
38 	struct	hmc425a_chip_info *chip_info;
39 	struct	gpio_descs *gpios;
40 	enum	hmc425a_type type;
41 	u32	gain;
42 };
43 
44 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
45 {
46 	struct hmc425a_state *st = iio_priv(indio_dev);
47 	DECLARE_BITMAP(values, BITS_PER_TYPE(value));
48 
49 	values[0] = value;
50 
51 	gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
52 				       NULL, values);
53 	return 0;
54 }
55 
56 static int hmc425a_read_raw(struct iio_dev *indio_dev,
57 			    struct iio_chan_spec const *chan, int *val,
58 			    int *val2, long m)
59 {
60 	struct hmc425a_state *st = iio_priv(indio_dev);
61 	int code, gain = 0;
62 	int ret;
63 
64 	mutex_lock(&st->lock);
65 	switch (m) {
66 	case IIO_CHAN_INFO_HARDWAREGAIN:
67 		code = st->gain;
68 
69 		switch (st->type) {
70 		case ID_HMC425A:
71 			gain = ~code * -500;
72 			break;
73 		}
74 
75 		*val = gain / 1000;
76 		*val2 = (gain % 1000) * 1000;
77 
78 		ret = IIO_VAL_INT_PLUS_MICRO_DB;
79 		break;
80 	default:
81 		ret = -EINVAL;
82 	}
83 	mutex_unlock(&st->lock);
84 
85 	return ret;
86 };
87 
88 static int hmc425a_write_raw(struct iio_dev *indio_dev,
89 			     struct iio_chan_spec const *chan, int val,
90 			     int val2, long mask)
91 {
92 	struct hmc425a_state *st = iio_priv(indio_dev);
93 	struct hmc425a_chip_info *inf = st->chip_info;
94 	int code = 0, gain;
95 	int ret;
96 
97 	if (val < 0)
98 		gain = (val * 1000) - (val2 / 1000);
99 	else
100 		gain = (val * 1000) + (val2 / 1000);
101 
102 	if (gain > inf->gain_max || gain < inf->gain_min)
103 		return -EINVAL;
104 
105 	switch (st->type) {
106 	case ID_HMC425A:
107 		code = ~((abs(gain) / 500) & 0x3F);
108 		break;
109 	}
110 
111 	mutex_lock(&st->lock);
112 	switch (mask) {
113 	case IIO_CHAN_INFO_HARDWAREGAIN:
114 		st->gain = code;
115 
116 		ret = hmc425a_write(indio_dev, st->gain);
117 		break;
118 	default:
119 		ret = -EINVAL;
120 	}
121 	mutex_unlock(&st->lock);
122 
123 	return ret;
124 }
125 
126 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
127 				     struct iio_chan_spec const *chan,
128 				     long mask)
129 {
130 	switch (mask) {
131 	case IIO_CHAN_INFO_HARDWAREGAIN:
132 		return IIO_VAL_INT_PLUS_MICRO_DB;
133 	default:
134 		return -EINVAL;
135 	}
136 }
137 
138 static const struct iio_info hmc425a_info = {
139 	.read_raw = &hmc425a_read_raw,
140 	.write_raw = &hmc425a_write_raw,
141 	.write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
142 };
143 
144 #define HMC425A_CHAN(_channel)						\
145 {									\
146 	.type = IIO_VOLTAGE,						\
147 	.output = 1,							\
148 	.indexed = 1,							\
149 	.channel = _channel,						\
150 	.info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),		\
151 }
152 
153 static const struct iio_chan_spec hmc425a_channels[] = {
154 	HMC425A_CHAN(0),
155 };
156 
157 /* Match table for of_platform binding */
158 static const struct of_device_id hmc425a_of_match[] = {
159 	{ .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
160 	{},
161 };
162 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
163 
164 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
165 	[ID_HMC425A] = {
166 		.name = "hmc425a",
167 		.channels = hmc425a_channels,
168 		.num_channels = ARRAY_SIZE(hmc425a_channels),
169 		.num_gpios = 6,
170 		.gain_min = -31500,
171 		.gain_max = 0,
172 		.default_gain = -0x40, /* set default gain -31.5db*/
173 	},
174 };
175 
176 static int hmc425a_probe(struct platform_device *pdev)
177 {
178 	struct iio_dev *indio_dev;
179 	struct hmc425a_state *st;
180 	int ret;
181 
182 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
183 	if (!indio_dev)
184 		return -ENOMEM;
185 
186 	st = iio_priv(indio_dev);
187 	st->type = (uintptr_t)device_get_match_data(&pdev->dev);
188 
189 	st->chip_info = &hmc425a_chip_info_tbl[st->type];
190 	indio_dev->num_channels = st->chip_info->num_channels;
191 	indio_dev->channels = st->chip_info->channels;
192 	indio_dev->name = st->chip_info->name;
193 	st->gain = st->chip_info->default_gain;
194 
195 	st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
196 	if (IS_ERR(st->gpios))
197 		return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
198 				     "failed to get gpios\n");
199 
200 	if (st->gpios->ndescs != st->chip_info->num_gpios) {
201 		dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
202 			st->chip_info->num_gpios);
203 		return -ENODEV;
204 	}
205 
206 	ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
207 	if (ret)
208 		return ret;
209 
210 	mutex_init(&st->lock);
211 
212 	indio_dev->info = &hmc425a_info;
213 	indio_dev->modes = INDIO_DIRECT_MODE;
214 
215 	return devm_iio_device_register(&pdev->dev, indio_dev);
216 }
217 
218 static struct platform_driver hmc425a_driver = {
219 	.driver = {
220 		.name = KBUILD_MODNAME,
221 		.of_match_table = hmc425a_of_match,
222 	},
223 	.probe = hmc425a_probe,
224 };
225 module_platform_driver(hmc425a_driver);
226 
227 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
228 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
229 MODULE_LICENSE("GPL v2");
230