1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ADMFM2000 Dual Microwave Down Converter
4  *
5  * Copyright 2024 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/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 
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
25 
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];
32 	u32				gain[2];
33 };
34 
admfm2000_mode(struct iio_dev * indio_dev,u32 chan,u32 mode)35 static int admfm2000_mode(struct iio_dev *indio_dev, u32 chan, u32 mode)
36 {
37 	struct admfm2000_state *st = iio_priv(indio_dev);
38 	int i;
39 
40 	switch (mode) {
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);
45 		}
46 		return 0;
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);
51 		}
52 		return 0;
53 	default:
54 		return -EINVAL;
55 	}
56 }
57 
admfm2000_attenuation(struct iio_dev * indio_dev,u32 chan,u32 value)58 static int admfm2000_attenuation(struct iio_dev *indio_dev, u32 chan, u32 value)
59 {
60 	struct admfm2000_state *st = iio_priv(indio_dev);
61 	int i;
62 
63 	switch (chan) {
64 	case 0:
65 		for (i = 0; i < ADMFM2000_DSA_GPIOS; i++)
66 			gpiod_set_value_cansleep(st->dsa1_gpios[i], value & (1 << i));
67 		return 0;
68 	case 1:
69 		for (i = 0; i < ADMFM2000_DSA_GPIOS; i++)
70 			gpiod_set_value_cansleep(st->dsa2_gpios[i], value & (1 << i));
71 		return 0;
72 	default:
73 		return -EINVAL;
74 	}
75 }
76 
admfm2000_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)77 static int admfm2000_read_raw(struct iio_dev *indio_dev,
78 			      struct iio_chan_spec const *chan, int *val,
79 			      int *val2, long mask)
80 {
81 	struct admfm2000_state *st = iio_priv(indio_dev);
82 	int gain;
83 
84 	switch (mask) {
85 	case IIO_CHAN_INFO_HARDWAREGAIN:
86 		mutex_lock(&st->lock);
87 		gain = ~(st->gain[chan->channel]) * -1000;
88 		*val = gain / 1000;
89 		*val2 = (gain % 1000) * 1000;
90 		mutex_unlock(&st->lock);
91 
92 		return IIO_VAL_INT_PLUS_MICRO_DB;
93 	default:
94 		return -EINVAL;
95 	}
96 }
97 
admfm2000_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)98 static int admfm2000_write_raw(struct iio_dev *indio_dev,
99 			       struct iio_chan_spec const *chan, int val,
100 			       int val2, long mask)
101 {
102 	struct admfm2000_state *st = iio_priv(indio_dev);
103 	int gain, ret;
104 
105 	if (val < 0)
106 		gain = (val * 1000) - (val2 / 1000);
107 	else
108 		gain = (val * 1000) + (val2 / 1000);
109 
110 	if (gain > ADMFM2000_MAX_GAIN || gain < ADMFM2000_MIN_GAIN)
111 		return -EINVAL;
112 
113 	switch (mask) {
114 	case IIO_CHAN_INFO_HARDWAREGAIN:
115 		mutex_lock(&st->lock);
116 		st->gain[chan->channel] = ~((abs(gain) / 1000) & 0x1F);
117 
118 		ret = admfm2000_attenuation(indio_dev, chan->channel,
119 					    st->gain[chan->channel]);
120 		mutex_unlock(&st->lock);
121 		return ret;
122 	default:
123 		return -EINVAL;
124 	}
125 }
126 
admfm2000_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)127 static int admfm2000_write_raw_get_fmt(struct iio_dev *indio_dev,
128 				       struct iio_chan_spec const *chan,
129 				       long mask)
130 {
131 	switch (mask) {
132 	case IIO_CHAN_INFO_HARDWAREGAIN:
133 		return IIO_VAL_INT_PLUS_MICRO_DB;
134 	default:
135 		return -EINVAL;
136 	}
137 }
138 
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,
143 };
144 
145 #define ADMFM2000_CHAN(_channel) {					\
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 admfm2000_channels[] = {
154 	ADMFM2000_CHAN(0),
155 	ADMFM2000_CHAN(1),
156 };
157 
admfm2000_channel_config(struct admfm2000_state * st,struct iio_dev * indio_dev)158 static int admfm2000_channel_config(struct admfm2000_state *st,
159 				    struct iio_dev *indio_dev)
160 {
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;
165 	int ret, i;
166 	bool mode;
167 	u32 reg;
168 
169 	device_for_each_child_node_scoped(dev, child) {
170 		ret = fwnode_property_read_u32(child, "reg", &reg);
171 		if (ret)
172 			return dev_err_probe(dev, ret,
173 					     "Failed to get reg property\n");
174 
175 		if (reg >= indio_dev->num_channels)
176 			return dev_err_probe(dev, -EINVAL, "reg bigger than: %d\n",
177 					     indio_dev->num_channels);
178 
179 		if (fwnode_property_present(child, "adi,mixer-mode"))
180 			mode = ADMFM2000_MIXER_MODE;
181 		else
182 			mode = ADMFM2000_DIRECT_IF_MODE;
183 
184 		switch (reg) {
185 		case 0:
186 			sw = st->sw1_ch;
187 			dsa = st->dsa1_gpios;
188 			break;
189 		case 1:
190 			sw = st->sw2_ch;
191 			dsa = st->dsa2_gpios;
192 			break;
193 		default:
194 			return -EINVAL;
195 		}
196 
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);
200 			if (IS_ERR(sw[i]))
201 				return dev_err_probe(dev, PTR_ERR(sw[i]),
202 						     "Failed to get gpios\n");
203 		}
204 
205 		for (i = 0; i < ADMFM2000_DSA_GPIOS; i++) {
206 			dsa[i] = devm_fwnode_gpiod_get_index(dev, child,
207 							     "attenuation", i,
208 							     GPIOD_OUT_LOW, NULL);
209 			if (IS_ERR(dsa[i]))
210 				return dev_err_probe(dev, PTR_ERR(dsa[i]),
211 						     "Failed to get gpios\n");
212 		}
213 
214 		ret = admfm2000_mode(indio_dev, reg, mode);
215 		if (ret)
216 			return ret;
217 	}
218 
219 	return 0;
220 }
221 
admfm2000_probe(struct platform_device * pdev)222 static int admfm2000_probe(struct platform_device *pdev)
223 {
224 	struct device *dev = &pdev->dev;
225 	struct admfm2000_state *st;
226 	struct iio_dev *indio_dev;
227 	int ret;
228 
229 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
230 	if (!indio_dev)
231 		return -ENOMEM;
232 
233 	st = iio_priv(indio_dev);
234 
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;
240 
241 	st->gain[0] = ADMFM2000_DEFAULT_GAIN;
242 	st->gain[1] = ADMFM2000_DEFAULT_GAIN;
243 
244 	mutex_init(&st->lock);
245 
246 	ret = admfm2000_channel_config(st, indio_dev);
247 	if (ret)
248 		return ret;
249 
250 	return devm_iio_device_register(dev, indio_dev);
251 }
252 
253 static const struct of_device_id admfm2000_of_match[] = {
254 	{ .compatible = "adi,admfm2000" },
255 	{ }
256 };
257 MODULE_DEVICE_TABLE(of, admfm2000_of_match);
258 
259 static struct platform_driver admfm2000_driver = {
260 	.driver = {
261 		.name = "admfm2000",
262 		.of_match_table = admfm2000_of_match,
263 	},
264 	.probe = admfm2000_probe,
265 };
266 module_platform_driver(admfm2000_driver);
267 
268 MODULE_AUTHOR("Kim Seer Paller <kimseer.paller@analog.com>");
269 MODULE_DESCRIPTION("ADMFM2000 Dual Microwave Down Converter");
270 MODULE_LICENSE("GPL");
271