1 // SPDX-License-Identifier: GPL-2.0
3 * ADMFM2000 Dual Microwave Down Converter
5 * Copyright 2024 Analog Devices Inc.
8 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/iio/iio.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
18 #define ADMFM2000_MIXER_MODE 0
19 #define ADMFM2000_DIRECT_IF_MODE 1
20 #define ADMFM2000_DSA_GPIOS 5
21 #define ADMFM2000_MODE_GPIOS 2
22 #define ADMFM2000_MAX_GAIN 0
23 #define ADMFM2000_MIN_GAIN -31000
24 #define ADMFM2000_DEFAULT_GAIN -0x20
26 struct admfm2000_state
{
27 struct mutex lock
; /* protect sensor state */
28 struct gpio_desc
*sw1_ch
[2];
29 struct gpio_desc
*sw2_ch
[2];
30 struct gpio_desc
*dsa1_gpios
[5];
31 struct gpio_desc
*dsa2_gpios
[5];
35 static int admfm2000_mode(struct iio_dev
*indio_dev
, u32 chan
, u32 mode
)
37 struct admfm2000_state
*st
= iio_priv(indio_dev
);
41 case ADMFM2000_MIXER_MODE
:
42 for (i
= 0; i
< ADMFM2000_MODE_GPIOS
; i
++) {
43 gpiod_set_value_cansleep(st
->sw1_ch
[i
], (chan
== 0) ? 1 : 0);
44 gpiod_set_value_cansleep(st
->sw2_ch
[i
], (chan
== 0) ? 0 : 1);
47 case ADMFM2000_DIRECT_IF_MODE
:
48 for (i
= 0; i
< ADMFM2000_MODE_GPIOS
; i
++) {
49 gpiod_set_value_cansleep(st
->sw1_ch
[i
], (chan
== 0) ? 0 : 1);
50 gpiod_set_value_cansleep(st
->sw2_ch
[i
], (chan
== 0) ? 1 : 0);
58 static int admfm2000_attenuation(struct iio_dev
*indio_dev
, u32 chan
, u32 value
)
60 struct admfm2000_state
*st
= iio_priv(indio_dev
);
65 for (i
= 0; i
< ADMFM2000_DSA_GPIOS
; i
++)
66 gpiod_set_value_cansleep(st
->dsa1_gpios
[i
], value
& (1 << i
));
69 for (i
= 0; i
< ADMFM2000_DSA_GPIOS
; i
++)
70 gpiod_set_value_cansleep(st
->dsa2_gpios
[i
], value
& (1 << i
));
77 static int admfm2000_read_raw(struct iio_dev
*indio_dev
,
78 struct iio_chan_spec
const *chan
, int *val
,
81 struct admfm2000_state
*st
= iio_priv(indio_dev
);
85 case IIO_CHAN_INFO_HARDWAREGAIN
:
86 mutex_lock(&st
->lock
);
87 gain
= ~(st
->gain
[chan
->channel
]) * -1000;
89 *val2
= (gain
% 1000) * 1000;
90 mutex_unlock(&st
->lock
);
92 return IIO_VAL_INT_PLUS_MICRO_DB
;
98 static int admfm2000_write_raw(struct iio_dev
*indio_dev
,
99 struct iio_chan_spec
const *chan
, int val
,
102 struct admfm2000_state
*st
= iio_priv(indio_dev
);
106 gain
= (val
* 1000) - (val2
/ 1000);
108 gain
= (val
* 1000) + (val2
/ 1000);
110 if (gain
> ADMFM2000_MAX_GAIN
|| gain
< ADMFM2000_MIN_GAIN
)
114 case IIO_CHAN_INFO_HARDWAREGAIN
:
115 mutex_lock(&st
->lock
);
116 st
->gain
[chan
->channel
] = ~((abs(gain
) / 1000) & 0x1F);
118 ret
= admfm2000_attenuation(indio_dev
, chan
->channel
,
119 st
->gain
[chan
->channel
]);
120 mutex_unlock(&st
->lock
);
127 static int admfm2000_write_raw_get_fmt(struct iio_dev
*indio_dev
,
128 struct iio_chan_spec
const *chan
,
132 case IIO_CHAN_INFO_HARDWAREGAIN
:
133 return IIO_VAL_INT_PLUS_MICRO_DB
;
139 static const struct iio_info admfm2000_info
= {
140 .read_raw
= &admfm2000_read_raw
,
141 .write_raw
= &admfm2000_write_raw
,
142 .write_raw_get_fmt
= &admfm2000_write_raw_get_fmt
,
145 #define ADMFM2000_CHAN(_channel) { \
146 .type = IIO_VOLTAGE, \
149 .channel = _channel, \
150 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \
153 static const struct iio_chan_spec admfm2000_channels
[] = {
158 static int admfm2000_channel_config(struct admfm2000_state
*st
,
159 struct iio_dev
*indio_dev
)
161 struct platform_device
*pdev
= to_platform_device(indio_dev
->dev
.parent
);
162 struct device
*dev
= &pdev
->dev
;
163 struct gpio_desc
**dsa
;
164 struct gpio_desc
**sw
;
169 device_for_each_child_node_scoped(dev
, child
) {
170 ret
= fwnode_property_read_u32(child
, "reg", ®
);
172 return dev_err_probe(dev
, ret
,
173 "Failed to get reg property\n");
175 if (reg
>= indio_dev
->num_channels
)
176 return dev_err_probe(dev
, -EINVAL
, "reg bigger than: %d\n",
177 indio_dev
->num_channels
);
179 if (fwnode_property_present(child
, "adi,mixer-mode"))
180 mode
= ADMFM2000_MIXER_MODE
;
182 mode
= ADMFM2000_DIRECT_IF_MODE
;
187 dsa
= st
->dsa1_gpios
;
191 dsa
= st
->dsa2_gpios
;
197 for (i
= 0; i
< ADMFM2000_MODE_GPIOS
; i
++) {
198 sw
[i
] = devm_fwnode_gpiod_get_index(dev
, child
, "switch",
199 i
, GPIOD_OUT_LOW
, NULL
);
201 return dev_err_probe(dev
, PTR_ERR(sw
[i
]),
202 "Failed to get gpios\n");
205 for (i
= 0; i
< ADMFM2000_DSA_GPIOS
; i
++) {
206 dsa
[i
] = devm_fwnode_gpiod_get_index(dev
, child
,
208 GPIOD_OUT_LOW
, NULL
);
210 return dev_err_probe(dev
, PTR_ERR(dsa
[i
]),
211 "Failed to get gpios\n");
214 ret
= admfm2000_mode(indio_dev
, reg
, mode
);
222 static int admfm2000_probe(struct platform_device
*pdev
)
224 struct device
*dev
= &pdev
->dev
;
225 struct admfm2000_state
*st
;
226 struct iio_dev
*indio_dev
;
229 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
233 st
= iio_priv(indio_dev
);
235 indio_dev
->name
= "admfm2000";
236 indio_dev
->num_channels
= ARRAY_SIZE(admfm2000_channels
);
237 indio_dev
->channels
= admfm2000_channels
;
238 indio_dev
->info
= &admfm2000_info
;
239 indio_dev
->modes
= INDIO_DIRECT_MODE
;
241 st
->gain
[0] = ADMFM2000_DEFAULT_GAIN
;
242 st
->gain
[1] = ADMFM2000_DEFAULT_GAIN
;
244 mutex_init(&st
->lock
);
246 ret
= admfm2000_channel_config(st
, indio_dev
);
250 return devm_iio_device_register(dev
, indio_dev
);
253 static const struct of_device_id admfm2000_of_match
[] = {
254 { .compatible
= "adi,admfm2000" },
257 MODULE_DEVICE_TABLE(of
, admfm2000_of_match
);
259 static struct platform_driver admfm2000_driver
= {
262 .of_match_table
= admfm2000_of_match
,
264 .probe
= admfm2000_probe
,
266 module_platform_driver(admfm2000_driver
);
268 MODULE_AUTHOR("Kim Seer Paller <kimseer.paller@analog.com>");
269 MODULE_DESCRIPTION("ADMFM2000 Dual Microwave Down Converter");
270 MODULE_LICENSE("GPL");