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