summaryrefslogtreecommitdiffstats
path: root/drivers/iio/amplifiers/adl8113.c
blob: b8a431b6616bbfa4b1ef8f32e9b8d611e499f3d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: GPL-2.0
/*
 * ADL8113 Low Noise Amplifier with integrated bypass switches
 *
 * Copyright 2025 Analog Devices Inc.
 */

#include <linux/array_size.h>
#include <linux/bitmap.h>
#include <linux/device/driver.h>
#include <linux/dev_printk.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/iio/iio.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/regulator/consumer.h>
#include <linux/types.h>

enum adl8113_signal_path {
	ADL8113_INTERNAL_AMP,
	ADL8113_INTERNAL_BYPASS,
	ADL8113_EXTERNAL_A,
	ADL8113_EXTERNAL_B,
};

struct adl8113_gain_config {
	enum adl8113_signal_path path;
	int gain_db;
};

struct adl8113_state {
	struct gpio_descs *gpios;
	struct adl8113_gain_config *gain_configs;
	unsigned int num_gain_configs;
	enum adl8113_signal_path current_path;
};

static const char * const adl8113_supply_names[] = {
	"vdd1",
	"vss2",
	"vdd2",
};

static int adl8113_set_path(struct adl8113_state *st,
			    enum adl8113_signal_path path)
{
	DECLARE_BITMAP(values, 2);
	int ret;

	/*
	 * Determine GPIO values based on signal path.
	 * Va: bit 0, Vb: bit 1.
	 */
	switch (path) {
	case ADL8113_INTERNAL_AMP:
		bitmap_write(values, 0x00, 0, 2);
		break;
	case ADL8113_INTERNAL_BYPASS:
		bitmap_write(values, 0x03, 0, 2);
		break;
	case ADL8113_EXTERNAL_A:
		bitmap_write(values, 0x02, 0, 2);
		break;
	case ADL8113_EXTERNAL_B:
		bitmap_write(values, 0x01, 0, 2);
		break;
	default:
		return -EINVAL;
	}

	ret = gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
					     st->gpios->info, values);
	if (ret)
		return ret;

	st->current_path = path;
	return 0;
}

static int adl8113_find_gain_config(struct adl8113_state *st, int gain_db)
{
	unsigned int i;

	for (i = 0; i < st->num_gain_configs; i++) {
		if (st->gain_configs[i].gain_db == gain_db)
			return i;
	}
	return -EINVAL;
}

static const struct iio_chan_spec adl8113_channels[] = {
	{
		.type = IIO_VOLTAGE,
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN),
	},
};

static int adl8113_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct adl8113_state *st = iio_priv(indio_dev);
	unsigned int i;

	switch (mask) {
	case IIO_CHAN_INFO_HARDWAREGAIN:
		/* Find current gain configuration */
		for (i = 0; i < st->num_gain_configs; i++) {
			if (st->gain_configs[i].path == st->current_path) {
				*val = st->gain_configs[i].gain_db;
				*val2 = 0;
				return IIO_VAL_INT_PLUS_MICRO_DB;
			}
		}
		return -EINVAL;
	default:
		return -EINVAL;
	}
}

static int adl8113_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	struct adl8113_state *st = iio_priv(indio_dev);
	int config_idx;

	switch (mask) {
	case IIO_CHAN_INFO_HARDWAREGAIN:
		if (val2 != 0)
			return -EINVAL;

		config_idx = adl8113_find_gain_config(st, val);
		if (config_idx < 0)
			return config_idx;

		return adl8113_set_path(st, st->gain_configs[config_idx].path);
	default:
		return -EINVAL;
	}
}

static const struct iio_info adl8113_info = {
	.read_raw = adl8113_read_raw,
	.write_raw = adl8113_write_raw,
};

static int adl8113_init_gain_configs(struct device *dev, struct adl8113_state *st)
{
	int external_a_gain, external_b_gain;
	unsigned int i;

	/*
	 * Allocate for all 4 possible paths:
	 * - Internal amp and bypass (always present)
	 * - External bypass A and B (optional if configured)
	 */
	st->gain_configs = devm_kcalloc(dev, 4, sizeof(*st->gain_configs),
					GFP_KERNEL);
	if (!st->gain_configs)
		return -ENOMEM;

	/* Start filling the gain configurations with data */
	i = 0;

	/* Always include internal amplifier (14dB) */
	st->gain_configs[i++] = (struct adl8113_gain_config) {
		.path = ADL8113_INTERNAL_AMP,
		.gain_db = 14,
	};

	/* Always include internal bypass (-2dB insertion loss) */
	st->gain_configs[i++] = (struct adl8113_gain_config) {
		.path = ADL8113_INTERNAL_BYPASS,
		.gain_db = -2,
	};

	/* Add external bypass A if configured */
	if (!device_property_read_u32(dev, "adi,external-bypass-a-gain-db",
				      &external_a_gain)) {
		st->gain_configs[i++] = (struct adl8113_gain_config) {
			.path = ADL8113_EXTERNAL_A,
			.gain_db = external_a_gain,
		};
	}

	/* Add external bypass B if configured */
	if (!device_property_read_u32(dev, "adi,external-bypass-b-gain-db",
				      &external_b_gain)) {
		st->gain_configs[i++] = (struct adl8113_gain_config) {
			.path = ADL8113_EXTERNAL_B,
			.gain_db = external_b_gain,
		};
	}

	st->num_gain_configs = i;

	return 0;
}

static int adl8113_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct adl8113_state *st;
	struct iio_dev *indio_dev;
	int ret;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
	if (!indio_dev)
		return -ENOMEM;

	st = iio_priv(indio_dev);

	st->gpios = devm_gpiod_get_array(dev, "ctrl", GPIOD_OUT_LOW);
	if (IS_ERR(st->gpios))
		return dev_err_probe(dev, PTR_ERR(st->gpios),
				     "failed to get control GPIOs\n");

	if (st->gpios->ndescs != 2)
		return dev_err_probe(dev, -EINVAL,
				     "expected 2 control GPIOs, got %u\n",
				     st->gpios->ndescs);

	ret = devm_regulator_bulk_get_enable(dev,
					     ARRAY_SIZE(adl8113_supply_names),
					     adl8113_supply_names);
	if (ret)
		return dev_err_probe(dev, ret,
				     "failed to get and enable supplies\n");

	/* Initialize gain configurations from devicetree */
	ret = adl8113_init_gain_configs(dev, st);
	if (ret)
		return ret;

	/* Initialize to internal amplifier path (14dB) */
	ret = adl8113_set_path(st, ADL8113_INTERNAL_AMP);
	if (ret)
		return ret;

	indio_dev->info = &adl8113_info;
	indio_dev->name = "adl8113";
	indio_dev->channels = adl8113_channels;
	indio_dev->num_channels = ARRAY_SIZE(adl8113_channels);

	return devm_iio_device_register(dev, indio_dev);
}

static const struct of_device_id adl8113_of_match[] = {
	{ .compatible = "adi,adl8113" },
	{ }
};
MODULE_DEVICE_TABLE(of, adl8113_of_match);

static struct platform_driver adl8113_driver = {
	.driver = {
		.name = "adl8113",
		.of_match_table = adl8113_of_match,
	},
	.probe = adl8113_probe,
};
module_platform_driver(adl8113_driver);

MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
MODULE_DESCRIPTION("Analog Devices ADL8113 Low Noise Amplifier");
MODULE_LICENSE("GPL");