1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * HMC425A and similar Gain Amplifiers
4 *
5 * Copyright 2020, 2024 Analog Devices Inc.
6 */
7
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/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/slab.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/sysfs.h>
23
24 /*
25 * The LTC6373 amplifier supports configuring gain using GPIO's with the following
26 * values (OUTPUT_V / INPUT_V): 0(shutdown), 0.25, 0.5, 1, 2, 4, 8, 16
27 *
28 * Except for the shutdown value, all can be converted to dB using 20 * log10(x)
29 * From here, it is observed that all values are multiples of the '2' gain setting,
30 * with the correspondent of 6.020dB.
31 */
32 #define LTC6373_CONVERSION_CONSTANT 6020
33 #define LTC6373_MIN_GAIN_CODE 0x6
34 #define LTC6373_CONVERSION_MASK GENMASK(2, 0)
35 #define LTC6373_SHUTDOWN GENMASK(2, 0)
36
37 enum hmc425a_type {
38 ID_HMC425A,
39 ID_HMC540S,
40 ID_ADRF5740,
41 ID_LTC6373,
42 };
43
44 struct hmc425a_chip_info {
45 const char *name;
46 const struct iio_chan_spec *channels;
47 unsigned int num_channels;
48 unsigned int num_gpios;
49 int gain_min;
50 int gain_max;
51 int default_gain;
52 int powerdown_val;
53 bool has_powerdown;
54
55 int (*gain_dB_to_code)(int gain, int *code);
56 int (*code_to_gain_dB)(int code, int *val, int *val2);
57 };
58
59 struct hmc425a_state {
60 struct mutex lock; /* protect sensor state */
61 const struct hmc425a_chip_info *chip_info;
62 struct gpio_descs *gpios;
63 u32 gain;
64 bool powerdown;
65 };
66
gain_dB_to_code(struct hmc425a_state * st,int val,int val2,int * code)67 static int gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code)
68 {
69 const struct hmc425a_chip_info *inf = st->chip_info;
70 int gain;
71
72 if (val < 0)
73 gain = (val * 1000) - (val2 / 1000);
74 else
75 gain = (val * 1000) + (val2 / 1000);
76
77 if (gain > inf->gain_max || gain < inf->gain_min)
78 return -EINVAL;
79 if (st->powerdown)
80 return -EPERM;
81
82 return st->chip_info->gain_dB_to_code(gain, code);
83 }
84
hmc425a_gain_dB_to_code(int gain,int * code)85 static int hmc425a_gain_dB_to_code(int gain, int *code)
86 {
87 *code = ~((abs(gain) / 500) & 0x3F);
88 return 0;
89 }
90
hmc540s_gain_dB_to_code(int gain,int * code)91 static int hmc540s_gain_dB_to_code(int gain, int *code)
92 {
93 *code = ~((abs(gain) / 1000) & 0xF);
94 return 0;
95 }
96
adrf5740_gain_dB_to_code(int gain,int * code)97 static int adrf5740_gain_dB_to_code(int gain, int *code)
98 {
99 int temp = (abs(gain) / 2000) & 0xF;
100
101 /* Bit [0-3]: 2dB 4dB 8dB 8dB */
102 *code = temp & BIT(3) ? temp | BIT(2) : temp;
103 return 0;
104 }
105
ltc6373_gain_dB_to_code(int gain,int * code)106 static int ltc6373_gain_dB_to_code(int gain, int *code)
107 {
108 *code = ~(DIV_ROUND_CLOSEST(gain, LTC6373_CONVERSION_CONSTANT) + 3)
109 & LTC6373_CONVERSION_MASK;
110 return 0;
111 }
112
code_to_gain_dB(struct hmc425a_state * st,int * val,int * val2)113 static int code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2)
114 {
115 if (st->powerdown)
116 return -EPERM;
117 return st->chip_info->code_to_gain_dB(st->gain, val, val2);
118 }
119
hmc425a_code_to_gain_dB(int code,int * val,int * val2)120 static int hmc425a_code_to_gain_dB(int code, int *val, int *val2)
121 {
122 *val = (~code * -500) / 1000;
123 *val2 = ((~code * -500) % 1000) * 1000;
124 return 0;
125 }
126
hmc540s_code_to_gain_dB(int code,int * val,int * val2)127 static int hmc540s_code_to_gain_dB(int code, int *val, int *val2)
128 {
129 *val = (~code * -1000) / 1000;
130 *val2 = ((~code * -1000) % 1000) * 1000;
131 return 0;
132 }
133
adrf5740_code_to_gain_dB(int code,int * val,int * val2)134 static int adrf5740_code_to_gain_dB(int code, int *val, int *val2)
135 {
136 /*
137 * Bit [0-3]: 2dB 4dB 8dB 8dB
138 * When BIT(3) is set, unset BIT(2) and use 3 as double the place value
139 */
140 code = code & BIT(3) ? code & ~BIT(2) : code;
141 *val = (code * -2000) / 1000;
142 *val2 = ((code * -2000) % 1000) * 1000;
143 return 0;
144 }
145
ltc6373_code_to_gain_dB(int code,int * val,int * val2)146 static int ltc6373_code_to_gain_dB(int code, int *val, int *val2)
147 {
148 int gain = ((~code & LTC6373_CONVERSION_MASK) - 3) *
149 LTC6373_CONVERSION_CONSTANT;
150
151 *val = gain / 1000;
152 *val2 = (gain % 1000) * 1000;
153 return 0;
154 }
155
hmc425a_write(struct iio_dev * indio_dev,u32 value)156 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
157 {
158 struct hmc425a_state *st = iio_priv(indio_dev);
159 DECLARE_BITMAP(values, BITS_PER_TYPE(value));
160
161 values[0] = value;
162
163 gpiod_multi_set_value_cansleep(st->gpios, values);
164 return 0;
165 }
166
hmc425a_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)167 static int hmc425a_read_raw(struct iio_dev *indio_dev,
168 struct iio_chan_spec const *chan, int *val,
169 int *val2, long m)
170 {
171 struct hmc425a_state *st = iio_priv(indio_dev);
172 int ret;
173
174 mutex_lock(&st->lock);
175 switch (m) {
176 case IIO_CHAN_INFO_HARDWAREGAIN:
177 ret = code_to_gain_dB(st, val, val2);
178 if (ret)
179 break;
180 ret = IIO_VAL_INT_PLUS_MICRO_DB;
181 break;
182 default:
183 ret = -EINVAL;
184 }
185 mutex_unlock(&st->lock);
186
187 return ret;
188 };
189
hmc425a_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)190 static int hmc425a_write_raw(struct iio_dev *indio_dev,
191 struct iio_chan_spec const *chan, int val,
192 int val2, long mask)
193 {
194 struct hmc425a_state *st = iio_priv(indio_dev);
195 int code = 0, ret;
196
197 mutex_lock(&st->lock);
198 switch (mask) {
199 case IIO_CHAN_INFO_HARDWAREGAIN:
200 ret = gain_dB_to_code(st, val, val2, &code);
201 if (ret)
202 break;
203 st->gain = code;
204
205 ret = hmc425a_write(indio_dev, st->gain);
206 break;
207 default:
208 ret = -EINVAL;
209 }
210 mutex_unlock(&st->lock);
211
212 return ret;
213 }
214
hmc425a_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)215 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
216 struct iio_chan_spec const *chan,
217 long mask)
218 {
219 switch (mask) {
220 case IIO_CHAN_INFO_HARDWAREGAIN:
221 return IIO_VAL_INT_PLUS_MICRO_DB;
222 default:
223 return -EINVAL;
224 }
225 }
226
227 static const struct iio_info hmc425a_info = {
228 .read_raw = &hmc425a_read_raw,
229 .write_raw = &hmc425a_write_raw,
230 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
231 };
232
ltc6373_read_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,char * buf)233 static ssize_t ltc6373_read_powerdown(struct iio_dev *indio_dev,
234 uintptr_t private,
235 const struct iio_chan_spec *chan,
236 char *buf)
237 {
238 struct hmc425a_state *st = iio_priv(indio_dev);
239
240 return sysfs_emit(buf, "%d\n", st->powerdown);
241 }
242
ltc6373_write_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,const char * buf,size_t len)243 static ssize_t ltc6373_write_powerdown(struct iio_dev *indio_dev,
244 uintptr_t private,
245 const struct iio_chan_spec *chan,
246 const char *buf,
247 size_t len)
248 {
249 struct hmc425a_state *st = iio_priv(indio_dev);
250 bool powerdown;
251 int code, ret;
252
253 ret = kstrtobool(buf, &powerdown);
254 if (ret)
255 return ret;
256
257 mutex_lock(&st->lock);
258 st->powerdown = powerdown;
259 code = (powerdown) ? LTC6373_SHUTDOWN : st->gain;
260 hmc425a_write(indio_dev, code);
261 mutex_unlock(&st->lock);
262 return len;
263 }
264
265 static const struct iio_chan_spec_ext_info ltc6373_ext_info[] = {
266 {
267 .name = "powerdown",
268 .read = ltc6373_read_powerdown,
269 .write = ltc6373_write_powerdown,
270 .shared = IIO_SEPARATE,
271 },
272 { }
273 };
274
275 #define HMC425A_CHAN(_channel) \
276 { \
277 .type = IIO_VOLTAGE, \
278 .output = 1, \
279 .indexed = 1, \
280 .channel = _channel, \
281 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
282 }
283
284 #define LTC6373_CHAN(_channel) \
285 { \
286 .type = IIO_VOLTAGE, \
287 .output = 1, \
288 .indexed = 1, \
289 .channel = _channel, \
290 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
291 .ext_info = ltc6373_ext_info, \
292 }
293
294 static const struct iio_chan_spec hmc425a_channels[] = {
295 HMC425A_CHAN(0),
296 };
297
298 static const struct iio_chan_spec ltc6373_channels[] = {
299 LTC6373_CHAN(0),
300 };
301
302 static const struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
303 [ID_HMC425A] = {
304 .name = "hmc425a",
305 .channels = hmc425a_channels,
306 .num_channels = ARRAY_SIZE(hmc425a_channels),
307 .num_gpios = 6,
308 .gain_min = -31500,
309 .gain_max = 0,
310 .default_gain = -0x40, /* set default gain -31.5db*/
311 .gain_dB_to_code = hmc425a_gain_dB_to_code,
312 .code_to_gain_dB = hmc425a_code_to_gain_dB,
313 },
314 [ID_HMC540S] = {
315 .name = "hmc540s",
316 .channels = hmc425a_channels,
317 .num_channels = ARRAY_SIZE(hmc425a_channels),
318 .num_gpios = 4,
319 .gain_min = -15000,
320 .gain_max = 0,
321 .default_gain = -0x10, /* set default gain -15.0db*/
322 .gain_dB_to_code = hmc540s_gain_dB_to_code,
323 .code_to_gain_dB = hmc540s_code_to_gain_dB,
324 },
325 [ID_ADRF5740] = {
326 .name = "adrf5740",
327 .channels = hmc425a_channels,
328 .num_channels = ARRAY_SIZE(hmc425a_channels),
329 .num_gpios = 4,
330 .gain_min = -22000,
331 .gain_max = 0,
332 .default_gain = 0xF, /* set default gain -22.0db*/
333 .gain_dB_to_code = adrf5740_gain_dB_to_code,
334 .code_to_gain_dB = adrf5740_code_to_gain_dB,
335 },
336 [ID_LTC6373] = {
337 .name = "ltc6373",
338 .channels = ltc6373_channels,
339 .num_channels = ARRAY_SIZE(ltc6373_channels),
340 .num_gpios = 3,
341 .gain_min = -12041, /* gain setting x0.25*/
342 .gain_max = 24082, /* gain setting x16 */
343 .default_gain = LTC6373_MIN_GAIN_CODE,
344 .powerdown_val = LTC6373_SHUTDOWN,
345 .has_powerdown = true,
346 .gain_dB_to_code = ltc6373_gain_dB_to_code,
347 .code_to_gain_dB = ltc6373_code_to_gain_dB,
348 },
349 };
350
hmc425a_probe(struct platform_device * pdev)351 static int hmc425a_probe(struct platform_device *pdev)
352 {
353 struct iio_dev *indio_dev;
354 struct hmc425a_state *st;
355 int ret;
356
357 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
358 if (!indio_dev)
359 return -ENOMEM;
360
361 st = iio_priv(indio_dev);
362
363 st->chip_info = device_get_match_data(&pdev->dev);
364 indio_dev->num_channels = st->chip_info->num_channels;
365 indio_dev->channels = st->chip_info->channels;
366 indio_dev->name = st->chip_info->name;
367 st->gain = st->chip_info->default_gain;
368
369 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
370 if (IS_ERR(st->gpios))
371 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
372 "failed to get gpios\n");
373
374 if (st->gpios->ndescs != st->chip_info->num_gpios) {
375 dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
376 st->chip_info->num_gpios);
377 return -ENODEV;
378 }
379
380 ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply");
381 if (ret)
382 return ret;
383
384 mutex_init(&st->lock);
385
386 indio_dev->info = &hmc425a_info;
387 indio_dev->modes = INDIO_DIRECT_MODE;
388
389 if (st->chip_info->has_powerdown) {
390 st->powerdown = true;
391 hmc425a_write(indio_dev, st->chip_info->powerdown_val);
392 } else {
393 /* Set default gain */
394 hmc425a_write(indio_dev, st->gain);
395 }
396
397 return devm_iio_device_register(&pdev->dev, indio_dev);
398 }
399
400 static const struct of_device_id hmc425a_of_match[] = {
401 { .compatible = "adi,hmc425a",
402 .data = &hmc425a_chip_info_tbl[ID_HMC425A]},
403 { .compatible = "adi,hmc540s",
404 .data = &hmc425a_chip_info_tbl[ID_HMC540S]},
405 { .compatible = "adi,adrf5740",
406 .data = &hmc425a_chip_info_tbl[ID_ADRF5740]},
407 { .compatible = "adi,ltc6373",
408 .data = &hmc425a_chip_info_tbl[ID_LTC6373]},
409 { }
410 };
411 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
412
413 static struct platform_driver hmc425a_driver = {
414 .driver = {
415 .name = KBUILD_MODNAME,
416 .of_match_table = hmc425a_of_match,
417 },
418 .probe = hmc425a_probe,
419 };
420 module_platform_driver(hmc425a_driver);
421
422 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
423 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
424 MODULE_LICENSE("GPL v2");
425