1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2022 Analog Devices, Inc.
4  * Author: Cosmin Tanislav <[email protected]>
5  */
6 
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/spi/spi.h>
23 #include <linux/units.h>
24 
25 #include <asm/div64.h>
26 #include <linux/unaligned.h>
27 
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/kfifo_buf.h>
31 #include <linux/iio/sysfs.h>
32 
33 #define AD4130_NAME				"ad4130"
34 
35 #define AD4130_COMMS_READ_MASK			BIT(6)
36 
37 #define AD4130_STATUS_REG			0x00
38 
39 #define AD4130_ADC_CONTROL_REG			0x01
40 #define AD4130_ADC_CONTROL_BIPOLAR_MASK		BIT(14)
41 #define AD4130_ADC_CONTROL_INT_REF_VAL_MASK	BIT(13)
42 #define AD4130_INT_REF_2_5V			2500000
43 #define AD4130_INT_REF_1_25V			1250000
44 #define AD4130_ADC_CONTROL_CSB_EN_MASK		BIT(9)
45 #define AD4130_ADC_CONTROL_INT_REF_EN_MASK	BIT(8)
46 #define AD4130_ADC_CONTROL_MODE_MASK		GENMASK(5, 2)
47 #define AD4130_ADC_CONTROL_MCLK_SEL_MASK	GENMASK(1, 0)
48 #define AD4130_MCLK_FREQ_76_8KHZ		76800
49 #define AD4130_MCLK_FREQ_153_6KHZ		153600
50 
51 #define AD4130_DATA_REG				0x02
52 
53 #define AD4130_IO_CONTROL_REG			0x03
54 #define AD4130_IO_CONTROL_INT_PIN_SEL_MASK	GENMASK(9, 8)
55 #define AD4130_IO_CONTROL_GPIO_DATA_MASK	GENMASK(7, 4)
56 #define AD4130_IO_CONTROL_GPIO_CTRL_MASK	GENMASK(3, 0)
57 
58 #define AD4130_VBIAS_REG			0x04
59 
60 #define AD4130_ID_REG				0x05
61 
62 #define AD4130_ERROR_REG			0x06
63 
64 #define AD4130_ERROR_EN_REG			0x07
65 
66 #define AD4130_MCLK_COUNT_REG			0x08
67 
68 #define AD4130_CHANNEL_X_REG(x)			(0x09 + (x))
69 #define AD4130_CHANNEL_EN_MASK			BIT(23)
70 #define AD4130_CHANNEL_SETUP_MASK		GENMASK(22, 20)
71 #define AD4130_CHANNEL_AINP_MASK		GENMASK(17, 13)
72 #define AD4130_CHANNEL_AINM_MASK		GENMASK(12, 8)
73 #define AD4130_CHANNEL_IOUT1_MASK		GENMASK(7, 4)
74 #define AD4130_CHANNEL_IOUT2_MASK		GENMASK(3, 0)
75 
76 #define AD4130_CONFIG_X_REG(x)			(0x19 + (x))
77 #define AD4130_CONFIG_IOUT1_VAL_MASK		GENMASK(15, 13)
78 #define AD4130_CONFIG_IOUT2_VAL_MASK		GENMASK(12, 10)
79 #define AD4130_CONFIG_BURNOUT_MASK		GENMASK(9, 8)
80 #define AD4130_CONFIG_REF_BUFP_MASK		BIT(7)
81 #define AD4130_CONFIG_REF_BUFM_MASK		BIT(6)
82 #define AD4130_CONFIG_REF_SEL_MASK		GENMASK(5, 4)
83 #define AD4130_CONFIG_PGA_MASK			GENMASK(3, 1)
84 
85 #define AD4130_FILTER_X_REG(x)			(0x21 + (x))
86 #define AD4130_FILTER_MODE_MASK			GENMASK(15, 12)
87 #define AD4130_FILTER_SELECT_MASK		GENMASK(10, 0)
88 #define AD4130_FILTER_SELECT_MIN		1
89 
90 #define AD4130_OFFSET_X_REG(x)			(0x29 + (x))
91 
92 #define AD4130_GAIN_X_REG(x)			(0x31 + (x))
93 
94 #define AD4130_MISC_REG				0x39
95 
96 #define AD4130_FIFO_CONTROL_REG			0x3a
97 #define AD4130_FIFO_CONTROL_HEADER_MASK		BIT(18)
98 #define AD4130_FIFO_CONTROL_MODE_MASK		GENMASK(17, 16)
99 #define AD4130_FIFO_CONTROL_WM_INT_EN_MASK	BIT(9)
100 #define AD4130_FIFO_CONTROL_WM_MASK		GENMASK(7, 0)
101 #define AD4130_WATERMARK_256			0
102 
103 #define AD4130_FIFO_STATUS_REG			0x3b
104 
105 #define AD4130_FIFO_THRESHOLD_REG		0x3c
106 
107 #define AD4130_FIFO_DATA_REG			0x3d
108 #define AD4130_FIFO_SIZE			256
109 #define AD4130_FIFO_MAX_SAMPLE_SIZE		3
110 
111 #define AD4130_MAX_ANALOG_PINS			16
112 #define AD4130_MAX_CHANNELS			16
113 #define AD4130_MAX_DIFF_INPUTS			30
114 #define AD4130_MAX_GPIOS			4
115 #define AD4130_MAX_ODR				2400
116 #define AD4130_MAX_PGA				8
117 #define AD4130_MAX_SETUPS			8
118 
119 #define AD4130_AIN2_P1				0x2
120 #define AD4130_AIN3_P2				0x3
121 
122 #define AD4130_RESET_BUF_SIZE			8
123 #define AD4130_RESET_SLEEP_US			(160 * MICRO / AD4130_MCLK_FREQ_76_8KHZ)
124 
125 #define AD4130_INVALID_SLOT			-1
126 
127 static const unsigned int ad4130_reg_size[] = {
128 	[AD4130_STATUS_REG] = 1,
129 	[AD4130_ADC_CONTROL_REG] = 2,
130 	[AD4130_DATA_REG] = 3,
131 	[AD4130_IO_CONTROL_REG] = 2,
132 	[AD4130_VBIAS_REG] = 2,
133 	[AD4130_ID_REG] = 1,
134 	[AD4130_ERROR_REG] = 2,
135 	[AD4130_ERROR_EN_REG] = 2,
136 	[AD4130_MCLK_COUNT_REG] = 1,
137 	[AD4130_CHANNEL_X_REG(0) ... AD4130_CHANNEL_X_REG(AD4130_MAX_CHANNELS - 1)] = 3,
138 	[AD4130_CONFIG_X_REG(0) ... AD4130_CONFIG_X_REG(AD4130_MAX_SETUPS - 1)] = 2,
139 	[AD4130_FILTER_X_REG(0) ... AD4130_FILTER_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
140 	[AD4130_OFFSET_X_REG(0) ... AD4130_OFFSET_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
141 	[AD4130_GAIN_X_REG(0) ... AD4130_GAIN_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
142 	[AD4130_MISC_REG] = 2,
143 	[AD4130_FIFO_CONTROL_REG] = 3,
144 	[AD4130_FIFO_STATUS_REG] = 1,
145 	[AD4130_FIFO_THRESHOLD_REG] = 3,
146 	[AD4130_FIFO_DATA_REG] = 3,
147 };
148 
149 enum ad4130_int_ref_val {
150 	AD4130_INT_REF_VAL_2_5V,
151 	AD4130_INT_REF_VAL_1_25V,
152 };
153 
154 enum ad4130_mclk_sel {
155 	AD4130_MCLK_76_8KHZ,
156 	AD4130_MCLK_76_8KHZ_OUT,
157 	AD4130_MCLK_76_8KHZ_EXT,
158 	AD4130_MCLK_153_6KHZ_EXT,
159 };
160 
161 enum ad4130_int_pin_sel {
162 	AD4130_INT_PIN_INT,
163 	AD4130_INT_PIN_CLK,
164 	AD4130_INT_PIN_P2,
165 	AD4130_INT_PIN_DOUT,
166 };
167 
168 enum ad4130_iout {
169 	AD4130_IOUT_OFF,
170 	AD4130_IOUT_10000NA,
171 	AD4130_IOUT_20000NA,
172 	AD4130_IOUT_50000NA,
173 	AD4130_IOUT_100000NA,
174 	AD4130_IOUT_150000NA,
175 	AD4130_IOUT_200000NA,
176 	AD4130_IOUT_100NA,
177 	AD4130_IOUT_MAX
178 };
179 
180 enum ad4130_burnout {
181 	AD4130_BURNOUT_OFF,
182 	AD4130_BURNOUT_500NA,
183 	AD4130_BURNOUT_2000NA,
184 	AD4130_BURNOUT_4000NA,
185 	AD4130_BURNOUT_MAX
186 };
187 
188 enum ad4130_ref_sel {
189 	AD4130_REF_REFIN1,
190 	AD4130_REF_REFIN2,
191 	AD4130_REF_REFOUT_AVSS,
192 	AD4130_REF_AVDD_AVSS,
193 	AD4130_REF_SEL_MAX
194 };
195 
196 enum ad4130_fifo_mode {
197 	AD4130_FIFO_MODE_DISABLED = 0b00,
198 	AD4130_FIFO_MODE_WM = 0b01,
199 };
200 
201 enum ad4130_mode {
202 	AD4130_MODE_CONTINUOUS = 0b0000,
203 	AD4130_MODE_IDLE = 0b0100,
204 };
205 
206 enum ad4130_filter_mode {
207 	AD4130_FILTER_SINC4,
208 	AD4130_FILTER_SINC4_SINC1,
209 	AD4130_FILTER_SINC3,
210 	AD4130_FILTER_SINC3_REJ60,
211 	AD4130_FILTER_SINC3_SINC1,
212 	AD4130_FILTER_SINC3_PF1,
213 	AD4130_FILTER_SINC3_PF2,
214 	AD4130_FILTER_SINC3_PF3,
215 	AD4130_FILTER_SINC3_PF4,
216 };
217 
218 enum ad4130_pin_function {
219 	AD4130_PIN_FN_NONE,
220 	AD4130_PIN_FN_SPECIAL = BIT(0),
221 	AD4130_PIN_FN_DIFF = BIT(1),
222 	AD4130_PIN_FN_EXCITATION = BIT(2),
223 	AD4130_PIN_FN_VBIAS = BIT(3),
224 };
225 
226 /*
227  * If you make adaptations in this struct, you most likely also have to adapt
228  * ad4130_setup_info_eq(), too.
229  */
230 struct ad4130_setup_info {
231 	unsigned int			iout0_val;
232 	unsigned int			iout1_val;
233 	unsigned int			burnout;
234 	unsigned int			pga;
235 	unsigned int			fs;
236 	u32				ref_sel;
237 	enum ad4130_filter_mode		filter_mode;
238 	bool				ref_bufp;
239 	bool				ref_bufm;
240 };
241 
242 struct ad4130_slot_info {
243 	struct ad4130_setup_info	setup;
244 	unsigned int			enabled_channels;
245 	unsigned int			channels;
246 };
247 
248 struct ad4130_chan_info {
249 	struct ad4130_setup_info	setup;
250 	u32				iout0;
251 	u32				iout1;
252 	int				slot;
253 	bool				enabled;
254 	bool				initialized;
255 };
256 
257 struct ad4130_filter_config {
258 	enum ad4130_filter_mode		filter_mode;
259 	unsigned int			odr_div;
260 	unsigned int			fs_max;
261 	enum iio_available_type		samp_freq_avail_type;
262 	int				samp_freq_avail_len;
263 	int				samp_freq_avail[3][2];
264 };
265 
266 struct ad4130_state {
267 	struct regmap			*regmap;
268 	struct spi_device		*spi;
269 	struct clk			*mclk;
270 	struct regulator_bulk_data	regulators[4];
271 	u32				irq_trigger;
272 	u32				inv_irq_trigger;
273 
274 	/*
275 	 * Synchronize access to members the of driver state, and ensure
276 	 * atomicity of consecutive regmap operations.
277 	 */
278 	struct mutex			lock;
279 	struct completion		completion;
280 
281 	struct iio_chan_spec		chans[AD4130_MAX_CHANNELS];
282 	struct ad4130_chan_info		chans_info[AD4130_MAX_CHANNELS];
283 	struct ad4130_slot_info		slots_info[AD4130_MAX_SETUPS];
284 	enum ad4130_pin_function	pins_fn[AD4130_MAX_ANALOG_PINS];
285 	u32				vbias_pins[AD4130_MAX_ANALOG_PINS];
286 	u32				num_vbias_pins;
287 	int				scale_tbls[AD4130_REF_SEL_MAX][AD4130_MAX_PGA][2];
288 	struct gpio_chip		gc;
289 	struct clk_hw			int_clk_hw;
290 
291 	u32			int_pin_sel;
292 	u32			int_ref_uv;
293 	u32			mclk_sel;
294 	bool			int_ref_en;
295 	bool			bipolar;
296 
297 	unsigned int		num_enabled_channels;
298 	unsigned int		effective_watermark;
299 	unsigned int		watermark;
300 
301 	struct spi_message	fifo_msg;
302 	struct spi_transfer	fifo_xfer[2];
303 
304 	/*
305 	 * DMA (thus cache coherency maintenance) requires any transfer
306 	 * buffers to live in their own cache lines. As the use of these
307 	 * buffers is synchronous, all of the buffers used for DMA in this
308 	 * driver may share a cache line.
309 	 */
310 	u8			reset_buf[AD4130_RESET_BUF_SIZE] __aligned(IIO_DMA_MINALIGN);
311 	u8			reg_write_tx_buf[4];
312 	u8			reg_read_tx_buf[1];
313 	u8			reg_read_rx_buf[3];
314 	u8			fifo_tx_buf[2];
315 	u8			fifo_rx_buf[AD4130_FIFO_SIZE *
316 					    AD4130_FIFO_MAX_SAMPLE_SIZE];
317 };
318 
319 static const char * const ad4130_int_pin_names[] = {
320 	[AD4130_INT_PIN_INT] = "int",
321 	[AD4130_INT_PIN_CLK] = "clk",
322 	[AD4130_INT_PIN_P2] = "p2",
323 	[AD4130_INT_PIN_DOUT] = "dout",
324 };
325 
326 static const unsigned int ad4130_iout_current_na_tbl[AD4130_IOUT_MAX] = {
327 	[AD4130_IOUT_OFF] = 0,
328 	[AD4130_IOUT_100NA] = 100,
329 	[AD4130_IOUT_10000NA] = 10000,
330 	[AD4130_IOUT_20000NA] = 20000,
331 	[AD4130_IOUT_50000NA] = 50000,
332 	[AD4130_IOUT_100000NA] = 100000,
333 	[AD4130_IOUT_150000NA] = 150000,
334 	[AD4130_IOUT_200000NA] = 200000,
335 };
336 
337 static const unsigned int ad4130_burnout_current_na_tbl[AD4130_BURNOUT_MAX] = {
338 	[AD4130_BURNOUT_OFF] = 0,
339 	[AD4130_BURNOUT_500NA] = 500,
340 	[AD4130_BURNOUT_2000NA] = 2000,
341 	[AD4130_BURNOUT_4000NA] = 4000,
342 };
343 
344 #define AD4130_VARIABLE_ODR_CONFIG(_filter_mode, _odr_div, _fs_max)	\
345 {									\
346 		.filter_mode = (_filter_mode),				\
347 		.odr_div = (_odr_div),					\
348 		.fs_max = (_fs_max),					\
349 		.samp_freq_avail_type = IIO_AVAIL_RANGE,		\
350 		.samp_freq_avail = {					\
351 			{ AD4130_MAX_ODR, (_odr_div) * (_fs_max) },	\
352 			{ AD4130_MAX_ODR, (_odr_div) * (_fs_max) },	\
353 			{ AD4130_MAX_ODR, (_odr_div) },			\
354 		},							\
355 }
356 
357 #define AD4130_FIXED_ODR_CONFIG(_filter_mode, _odr_div)			\
358 {									\
359 		.filter_mode = (_filter_mode),				\
360 		.odr_div = (_odr_div),					\
361 		.fs_max = AD4130_FILTER_SELECT_MIN,			\
362 		.samp_freq_avail_type = IIO_AVAIL_LIST,			\
363 		.samp_freq_avail_len = 1,				\
364 		.samp_freq_avail = {					\
365 			{ AD4130_MAX_ODR, (_odr_div) },			\
366 		},							\
367 }
368 
369 static const struct ad4130_filter_config ad4130_filter_configs[] = {
370 	AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4,       1,  10),
371 	AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4_SINC1, 11, 10),
372 	AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3,       1,  2047),
373 	AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_REJ60, 1,  2047),
374 	AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_SINC1, 10, 2047),
375 	AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF1,      92),
376 	AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF2,      100),
377 	AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF3,      124),
378 	AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF4,      148),
379 };
380 
381 static const char * const ad4130_filter_modes_str[] = {
382 	[AD4130_FILTER_SINC4] = "sinc4",
383 	[AD4130_FILTER_SINC4_SINC1] = "sinc4+sinc1",
384 	[AD4130_FILTER_SINC3] = "sinc3",
385 	[AD4130_FILTER_SINC3_REJ60] = "sinc3+rej60",
386 	[AD4130_FILTER_SINC3_SINC1] = "sinc3+sinc1",
387 	[AD4130_FILTER_SINC3_PF1] = "sinc3+pf1",
388 	[AD4130_FILTER_SINC3_PF2] = "sinc3+pf2",
389 	[AD4130_FILTER_SINC3_PF3] = "sinc3+pf3",
390 	[AD4130_FILTER_SINC3_PF4] = "sinc3+pf4",
391 };
392 
ad4130_get_reg_size(struct ad4130_state * st,unsigned int reg,unsigned int * size)393 static int ad4130_get_reg_size(struct ad4130_state *st, unsigned int reg,
394 			       unsigned int *size)
395 {
396 	if (reg >= ARRAY_SIZE(ad4130_reg_size))
397 		return -EINVAL;
398 
399 	*size = ad4130_reg_size[reg];
400 
401 	return 0;
402 }
403 
ad4130_data_reg_size(struct ad4130_state * st)404 static unsigned int ad4130_data_reg_size(struct ad4130_state *st)
405 {
406 	unsigned int data_reg_size;
407 	int ret;
408 
409 	ret = ad4130_get_reg_size(st, AD4130_DATA_REG, &data_reg_size);
410 	if (ret)
411 		return 0;
412 
413 	return data_reg_size;
414 }
415 
ad4130_resolution(struct ad4130_state * st)416 static unsigned int ad4130_resolution(struct ad4130_state *st)
417 {
418 	return ad4130_data_reg_size(st) * BITS_PER_BYTE;
419 }
420 
ad4130_reg_write(void * context,unsigned int reg,unsigned int val)421 static int ad4130_reg_write(void *context, unsigned int reg, unsigned int val)
422 {
423 	struct ad4130_state *st = context;
424 	unsigned int size;
425 	int ret;
426 
427 	ret = ad4130_get_reg_size(st, reg, &size);
428 	if (ret)
429 		return ret;
430 
431 	st->reg_write_tx_buf[0] = reg;
432 
433 	switch (size) {
434 	case 3:
435 		put_unaligned_be24(val, &st->reg_write_tx_buf[1]);
436 		break;
437 	case 2:
438 		put_unaligned_be16(val, &st->reg_write_tx_buf[1]);
439 		break;
440 	case 1:
441 		st->reg_write_tx_buf[1] = val;
442 		break;
443 	default:
444 		return -EINVAL;
445 	}
446 
447 	return spi_write(st->spi, st->reg_write_tx_buf, size + 1);
448 }
449 
ad4130_reg_read(void * context,unsigned int reg,unsigned int * val)450 static int ad4130_reg_read(void *context, unsigned int reg, unsigned int *val)
451 {
452 	struct ad4130_state *st = context;
453 	struct spi_transfer t[] = {
454 		{
455 			.tx_buf = st->reg_read_tx_buf,
456 			.len = sizeof(st->reg_read_tx_buf),
457 		},
458 		{
459 			.rx_buf = st->reg_read_rx_buf,
460 		},
461 	};
462 	unsigned int size;
463 	int ret;
464 
465 	ret = ad4130_get_reg_size(st, reg, &size);
466 	if (ret)
467 		return ret;
468 
469 	st->reg_read_tx_buf[0] = AD4130_COMMS_READ_MASK | reg;
470 	t[1].len = size;
471 
472 	ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
473 	if (ret)
474 		return ret;
475 
476 	switch (size) {
477 	case 3:
478 		*val = get_unaligned_be24(st->reg_read_rx_buf);
479 		break;
480 	case 2:
481 		*val = get_unaligned_be16(st->reg_read_rx_buf);
482 		break;
483 	case 1:
484 		*val = st->reg_read_rx_buf[0];
485 		break;
486 	default:
487 		return -EINVAL;
488 	}
489 
490 	return 0;
491 }
492 
493 static const struct regmap_config ad4130_regmap_config = {
494 	.reg_read = ad4130_reg_read,
495 	.reg_write = ad4130_reg_write,
496 };
497 
ad4130_gpio_init_valid_mask(struct gpio_chip * gc,unsigned long * valid_mask,unsigned int ngpios)498 static int ad4130_gpio_init_valid_mask(struct gpio_chip *gc,
499 				       unsigned long *valid_mask,
500 				       unsigned int ngpios)
501 {
502 	struct ad4130_state *st = gpiochip_get_data(gc);
503 	unsigned int i;
504 
505 	/*
506 	 * Output-only GPIO functionality is available on pins AIN2 through
507 	 * AIN5. If these pins are used for anything else, do not expose them.
508 	 */
509 	for (i = 0; i < ngpios; i++) {
510 		unsigned int pin = i + AD4130_AIN2_P1;
511 		bool valid = st->pins_fn[pin] == AD4130_PIN_FN_NONE;
512 
513 		__assign_bit(i, valid_mask, valid);
514 	}
515 
516 	return 0;
517 }
518 
ad4130_gpio_get_direction(struct gpio_chip * gc,unsigned int offset)519 static int ad4130_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
520 {
521 	return GPIO_LINE_DIRECTION_OUT;
522 }
523 
ad4130_gpio_set(struct gpio_chip * gc,unsigned int offset,int value)524 static void ad4130_gpio_set(struct gpio_chip *gc, unsigned int offset,
525 			    int value)
526 {
527 	struct ad4130_state *st = gpiochip_get_data(gc);
528 	unsigned int mask = FIELD_PREP(AD4130_IO_CONTROL_GPIO_DATA_MASK,
529 				       BIT(offset));
530 
531 	regmap_update_bits(st->regmap, AD4130_IO_CONTROL_REG, mask,
532 			   value ? mask : 0);
533 }
534 
ad4130_set_mode(struct ad4130_state * st,enum ad4130_mode mode)535 static int ad4130_set_mode(struct ad4130_state *st, enum ad4130_mode mode)
536 {
537 	return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
538 				  AD4130_ADC_CONTROL_MODE_MASK,
539 				  FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, mode));
540 }
541 
ad4130_set_watermark_interrupt_en(struct ad4130_state * st,bool en)542 static int ad4130_set_watermark_interrupt_en(struct ad4130_state *st, bool en)
543 {
544 	return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
545 				  AD4130_FIFO_CONTROL_WM_INT_EN_MASK,
546 				  FIELD_PREP(AD4130_FIFO_CONTROL_WM_INT_EN_MASK, en));
547 }
548 
ad4130_watermark_reg_val(unsigned int val)549 static unsigned int ad4130_watermark_reg_val(unsigned int val)
550 {
551 	if (val == AD4130_FIFO_SIZE)
552 		val = AD4130_WATERMARK_256;
553 
554 	return val;
555 }
556 
ad4130_set_fifo_mode(struct ad4130_state * st,enum ad4130_fifo_mode mode)557 static int ad4130_set_fifo_mode(struct ad4130_state *st,
558 				enum ad4130_fifo_mode mode)
559 {
560 	return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
561 				  AD4130_FIFO_CONTROL_MODE_MASK,
562 				  FIELD_PREP(AD4130_FIFO_CONTROL_MODE_MASK, mode));
563 }
564 
ad4130_push_fifo_data(struct iio_dev * indio_dev)565 static void ad4130_push_fifo_data(struct iio_dev *indio_dev)
566 {
567 	struct ad4130_state *st = iio_priv(indio_dev);
568 	unsigned int data_reg_size = ad4130_data_reg_size(st);
569 	unsigned int transfer_len = st->effective_watermark * data_reg_size;
570 	unsigned int set_size = st->num_enabled_channels * data_reg_size;
571 	unsigned int i;
572 	int ret;
573 
574 	st->fifo_tx_buf[1] = ad4130_watermark_reg_val(st->effective_watermark);
575 	st->fifo_xfer[1].len = transfer_len;
576 
577 	ret = spi_sync(st->spi, &st->fifo_msg);
578 	if (ret)
579 		return;
580 
581 	for (i = 0; i < transfer_len; i += set_size)
582 		iio_push_to_buffers(indio_dev, &st->fifo_rx_buf[i]);
583 }
584 
ad4130_irq_handler(int irq,void * private)585 static irqreturn_t ad4130_irq_handler(int irq, void *private)
586 {
587 	struct iio_dev *indio_dev = private;
588 	struct ad4130_state *st = iio_priv(indio_dev);
589 
590 	if (iio_buffer_enabled(indio_dev))
591 		ad4130_push_fifo_data(indio_dev);
592 	else
593 		complete(&st->completion);
594 
595 	return IRQ_HANDLED;
596 }
597 
ad4130_setup_info_eq(struct ad4130_setup_info * a,struct ad4130_setup_info * b)598 static bool ad4130_setup_info_eq(struct ad4130_setup_info *a,
599 				 struct ad4130_setup_info *b)
600 {
601 	/*
602 	 * This is just to make sure that the comparison is adapted after
603 	 * struct ad4130_setup_info was changed.
604 	 */
605 	static_assert(sizeof(*a) ==
606 		      sizeof(struct {
607 				     unsigned int iout0_val;
608 				     unsigned int iout1_val;
609 				     unsigned int burnout;
610 				     unsigned int pga;
611 				     unsigned int fs;
612 				     u32 ref_sel;
613 				     enum ad4130_filter_mode filter_mode;
614 				     bool ref_bufp;
615 				     bool ref_bufm;
616 			     }));
617 
618 	if (a->iout0_val != b->iout0_val ||
619 	    a->iout1_val != b->iout1_val ||
620 	    a->burnout != b->burnout ||
621 	    a->pga != b->pga ||
622 	    a->fs != b->fs ||
623 	    a->ref_sel != b->ref_sel ||
624 	    a->filter_mode != b->filter_mode ||
625 	    a->ref_bufp != b->ref_bufp ||
626 	    a->ref_bufm != b->ref_bufm)
627 		return false;
628 
629 	return true;
630 }
631 
ad4130_find_slot(struct ad4130_state * st,struct ad4130_setup_info * target_setup_info,unsigned int * slot,bool * overwrite)632 static int ad4130_find_slot(struct ad4130_state *st,
633 			    struct ad4130_setup_info *target_setup_info,
634 			    unsigned int *slot, bool *overwrite)
635 {
636 	unsigned int i;
637 
638 	*slot = AD4130_INVALID_SLOT;
639 	*overwrite = false;
640 
641 	for (i = 0; i < AD4130_MAX_SETUPS; i++) {
642 		struct ad4130_slot_info *slot_info = &st->slots_info[i];
643 
644 		/* Immediately accept a matching setup info. */
645 		if (ad4130_setup_info_eq(target_setup_info, &slot_info->setup)) {
646 			*slot = i;
647 			return 0;
648 		}
649 
650 		/* Ignore all setups which are used by enabled channels. */
651 		if (slot_info->enabled_channels)
652 			continue;
653 
654 		/* Find the least used slot. */
655 		if (*slot == AD4130_INVALID_SLOT ||
656 		    slot_info->channels < st->slots_info[*slot].channels)
657 			*slot = i;
658 	}
659 
660 	if (*slot == AD4130_INVALID_SLOT)
661 		return -EINVAL;
662 
663 	*overwrite = true;
664 
665 	return 0;
666 }
667 
ad4130_unlink_channel(struct ad4130_state * st,unsigned int channel)668 static void ad4130_unlink_channel(struct ad4130_state *st, unsigned int channel)
669 {
670 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
671 	struct ad4130_slot_info *slot_info = &st->slots_info[chan_info->slot];
672 
673 	chan_info->slot = AD4130_INVALID_SLOT;
674 	slot_info->channels--;
675 }
676 
ad4130_unlink_slot(struct ad4130_state * st,unsigned int slot)677 static int ad4130_unlink_slot(struct ad4130_state *st, unsigned int slot)
678 {
679 	unsigned int i;
680 
681 	for (i = 0; i < AD4130_MAX_CHANNELS; i++) {
682 		struct ad4130_chan_info *chan_info = &st->chans_info[i];
683 
684 		if (!chan_info->initialized || chan_info->slot != slot)
685 			continue;
686 
687 		ad4130_unlink_channel(st, i);
688 	}
689 
690 	return 0;
691 }
692 
ad4130_link_channel_slot(struct ad4130_state * st,unsigned int channel,unsigned int slot)693 static int ad4130_link_channel_slot(struct ad4130_state *st,
694 				    unsigned int channel, unsigned int slot)
695 {
696 	struct ad4130_slot_info *slot_info = &st->slots_info[slot];
697 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
698 	int ret;
699 
700 	ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
701 				 AD4130_CHANNEL_SETUP_MASK,
702 				 FIELD_PREP(AD4130_CHANNEL_SETUP_MASK, slot));
703 	if (ret)
704 		return ret;
705 
706 	chan_info->slot = slot;
707 	slot_info->channels++;
708 
709 	return 0;
710 }
711 
ad4130_write_slot_setup(struct ad4130_state * st,unsigned int slot,struct ad4130_setup_info * setup_info)712 static int ad4130_write_slot_setup(struct ad4130_state *st,
713 				   unsigned int slot,
714 				   struct ad4130_setup_info *setup_info)
715 {
716 	unsigned int val;
717 	int ret;
718 
719 	val = FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout0_val) |
720 	      FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout1_val) |
721 	      FIELD_PREP(AD4130_CONFIG_BURNOUT_MASK, setup_info->burnout) |
722 	      FIELD_PREP(AD4130_CONFIG_REF_BUFP_MASK, setup_info->ref_bufp) |
723 	      FIELD_PREP(AD4130_CONFIG_REF_BUFM_MASK, setup_info->ref_bufm) |
724 	      FIELD_PREP(AD4130_CONFIG_REF_SEL_MASK, setup_info->ref_sel) |
725 	      FIELD_PREP(AD4130_CONFIG_PGA_MASK, setup_info->pga);
726 
727 	ret = regmap_write(st->regmap, AD4130_CONFIG_X_REG(slot), val);
728 	if (ret)
729 		return ret;
730 
731 	val = FIELD_PREP(AD4130_FILTER_MODE_MASK, setup_info->filter_mode) |
732 	      FIELD_PREP(AD4130_FILTER_SELECT_MASK, setup_info->fs);
733 
734 	ret = regmap_write(st->regmap, AD4130_FILTER_X_REG(slot), val);
735 	if (ret)
736 		return ret;
737 
738 	memcpy(&st->slots_info[slot].setup, setup_info, sizeof(*setup_info));
739 
740 	return 0;
741 }
742 
ad4130_write_channel_setup(struct ad4130_state * st,unsigned int channel,bool on_enable)743 static int ad4130_write_channel_setup(struct ad4130_state *st,
744 				      unsigned int channel, bool on_enable)
745 {
746 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
747 	struct ad4130_setup_info *setup_info = &chan_info->setup;
748 	bool overwrite;
749 	int slot;
750 	int ret;
751 
752 	/*
753 	 * The following cases need to be handled.
754 	 *
755 	 * 1. Enabled and linked channel with setup changes:
756 	 *    - Find a slot. If not possible, return error.
757 	 *    - Unlink channel from current slot.
758 	 *    - If the slot has channels linked to it, unlink all channels, and
759 	 *      write the new setup to it.
760 	 *    - Link channel to new slot.
761 	 *
762 	 * 2. Soon to be enabled and unlinked channel:
763 	 *    - Find a slot. If not possible, return error.
764 	 *    - If the slot has channels linked to it, unlink all channels, and
765 	 *      write the new setup to it.
766 	 *    - Link channel to the slot.
767 	 *
768 	 * 3. Disabled and linked channel with setup changes:
769 	 *    - Unlink channel from current slot.
770 	 *
771 	 * 4. Soon to be enabled and linked channel:
772 	 * 5. Disabled and unlinked channel with setup changes:
773 	 *    - Do nothing.
774 	 */
775 
776 	/* Case 4 */
777 	if (on_enable && chan_info->slot != AD4130_INVALID_SLOT)
778 		return 0;
779 
780 	if (!on_enable && !chan_info->enabled) {
781 		if (chan_info->slot != AD4130_INVALID_SLOT)
782 			/* Case 3 */
783 			ad4130_unlink_channel(st, channel);
784 
785 		/* Cases 3 & 5 */
786 		return 0;
787 	}
788 
789 	/* Cases 1 & 2 */
790 	ret = ad4130_find_slot(st, setup_info, &slot, &overwrite);
791 	if (ret)
792 		return ret;
793 
794 	if (chan_info->slot != AD4130_INVALID_SLOT)
795 		/* Case 1 */
796 		ad4130_unlink_channel(st, channel);
797 
798 	if (overwrite) {
799 		ret = ad4130_unlink_slot(st, slot);
800 		if (ret)
801 			return ret;
802 
803 		ret = ad4130_write_slot_setup(st, slot, setup_info);
804 		if (ret)
805 			return ret;
806 	}
807 
808 	return ad4130_link_channel_slot(st, channel, slot);
809 }
810 
ad4130_set_channel_enable(struct ad4130_state * st,unsigned int channel,bool status)811 static int ad4130_set_channel_enable(struct ad4130_state *st,
812 				     unsigned int channel, bool status)
813 {
814 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
815 	struct ad4130_slot_info *slot_info;
816 	int ret;
817 
818 	if (chan_info->enabled == status)
819 		return 0;
820 
821 	if (status) {
822 		ret = ad4130_write_channel_setup(st, channel, true);
823 		if (ret)
824 			return ret;
825 	}
826 
827 	slot_info = &st->slots_info[chan_info->slot];
828 
829 	ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
830 				 AD4130_CHANNEL_EN_MASK,
831 				 FIELD_PREP(AD4130_CHANNEL_EN_MASK, status));
832 	if (ret)
833 		return ret;
834 
835 	slot_info->enabled_channels += status ? 1 : -1;
836 	chan_info->enabled = status;
837 
838 	return 0;
839 }
840 
841 /*
842  * Table 58. FILTER_MODE_n bits and Filter Types of the datasheet describes
843  * the relation between filter mode, ODR and FS.
844  *
845  * Notice that the max ODR of each filter mode is not necessarily the
846  * absolute max ODR supported by the chip.
847  *
848  * The ODR divider is not explicitly specified, but it can be deduced based
849  * on the ODR range of each filter mode.
850  *
851  * For example, for Sinc4+Sinc1, max ODR is 218.18. That means that the
852  * absolute max ODR is divided by 11 to achieve the max ODR of this filter
853  * mode.
854  *
855  * The formulas for converting between ODR and FS for a specific filter
856  * mode can be deduced from the same table.
857  *
858  * Notice that FS = 1 actually means max ODR, and that ODR decreases by
859  * (maximum ODR / maximum FS) for each increment of FS.
860  *
861  * odr = MAX_ODR / odr_div * (1 - (fs - 1) / fs_max) <=>
862  * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
863  * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
864  * odr = MAX_ODR * (fs_max - fs + 1) / (fs_max * odr_div)
865  * (used in ad4130_fs_to_freq)
866  *
867  * For the opposite formula, FS can be extracted from the last one.
868  *
869  * MAX_ODR * (fs_max - fs + 1) = fs_max * odr_div * odr <=>
870  * fs_max - fs + 1 = fs_max * odr_div * odr / MAX_ODR <=>
871  * fs = 1 + fs_max - fs_max * odr_div * odr / MAX_ODR
872  * (used in ad4130_fs_to_freq)
873  */
874 
ad4130_freq_to_fs(enum ad4130_filter_mode filter_mode,int val,int val2,unsigned int * fs)875 static void ad4130_freq_to_fs(enum ad4130_filter_mode filter_mode,
876 			      int val, int val2, unsigned int *fs)
877 {
878 	const struct ad4130_filter_config *filter_config =
879 		&ad4130_filter_configs[filter_mode];
880 	u64 dividend, divisor;
881 	int temp;
882 
883 	dividend = filter_config->fs_max * filter_config->odr_div *
884 		   ((u64)val * NANO + val2);
885 	divisor = (u64)AD4130_MAX_ODR * NANO;
886 
887 	temp = AD4130_FILTER_SELECT_MIN + filter_config->fs_max -
888 	       DIV64_U64_ROUND_CLOSEST(dividend, divisor);
889 
890 	if (temp < AD4130_FILTER_SELECT_MIN)
891 		temp = AD4130_FILTER_SELECT_MIN;
892 	else if (temp > filter_config->fs_max)
893 		temp = filter_config->fs_max;
894 
895 	*fs = temp;
896 }
897 
ad4130_fs_to_freq(enum ad4130_filter_mode filter_mode,unsigned int fs,int * val,int * val2)898 static void ad4130_fs_to_freq(enum ad4130_filter_mode filter_mode,
899 			      unsigned int fs, int *val, int *val2)
900 {
901 	const struct ad4130_filter_config *filter_config =
902 		&ad4130_filter_configs[filter_mode];
903 	unsigned int dividend, divisor;
904 	u64 temp;
905 
906 	dividend = (filter_config->fs_max - fs + AD4130_FILTER_SELECT_MIN) *
907 		   AD4130_MAX_ODR;
908 	divisor = filter_config->fs_max * filter_config->odr_div;
909 
910 	temp = div_u64((u64)dividend * NANO, divisor);
911 	*val = div_u64_rem(temp, NANO, val2);
912 }
913 
ad4130_set_filter_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int val)914 static int ad4130_set_filter_mode(struct iio_dev *indio_dev,
915 				  const struct iio_chan_spec *chan,
916 				  unsigned int val)
917 {
918 	struct ad4130_state *st = iio_priv(indio_dev);
919 	unsigned int channel = chan->scan_index;
920 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
921 	struct ad4130_setup_info *setup_info = &chan_info->setup;
922 	enum ad4130_filter_mode old_filter_mode;
923 	int freq_val, freq_val2;
924 	unsigned int old_fs;
925 	int ret = 0;
926 
927 	guard(mutex)(&st->lock);
928 	if (setup_info->filter_mode == val)
929 		return 0;
930 
931 	old_fs = setup_info->fs;
932 	old_filter_mode = setup_info->filter_mode;
933 
934 	/*
935 	 * When switching between filter modes, try to match the ODR as
936 	 * close as possible. To do this, convert the current FS into ODR
937 	 * using the old filter mode, then convert it back into FS using
938 	 * the new filter mode.
939 	 */
940 	ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs,
941 			  &freq_val, &freq_val2);
942 
943 	ad4130_freq_to_fs(val, freq_val, freq_val2, &setup_info->fs);
944 
945 	setup_info->filter_mode = val;
946 
947 	ret = ad4130_write_channel_setup(st, channel, false);
948 	if (ret) {
949 		setup_info->fs = old_fs;
950 		setup_info->filter_mode = old_filter_mode;
951 		return ret;
952 	}
953 
954 	return 0;
955 }
956 
ad4130_get_filter_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)957 static int ad4130_get_filter_mode(struct iio_dev *indio_dev,
958 				  const struct iio_chan_spec *chan)
959 {
960 	struct ad4130_state *st = iio_priv(indio_dev);
961 	unsigned int channel = chan->scan_index;
962 	struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
963 	enum ad4130_filter_mode filter_mode;
964 
965 	guard(mutex)(&st->lock);
966 	filter_mode = setup_info->filter_mode;
967 
968 	return filter_mode;
969 }
970 
971 static const struct iio_enum ad4130_filter_mode_enum = {
972 	.items = ad4130_filter_modes_str,
973 	.num_items = ARRAY_SIZE(ad4130_filter_modes_str),
974 	.set = ad4130_set_filter_mode,
975 	.get = ad4130_get_filter_mode,
976 };
977 
978 static const struct iio_chan_spec_ext_info ad4130_filter_mode_ext_info[] = {
979 	IIO_ENUM("filter_mode", IIO_SEPARATE, &ad4130_filter_mode_enum),
980 	IIO_ENUM_AVAILABLE("filter_mode", IIO_SHARED_BY_TYPE,
981 			   &ad4130_filter_mode_enum),
982 	{ }
983 };
984 
985 static const struct iio_chan_spec ad4130_channel_template = {
986 	.type = IIO_VOLTAGE,
987 	.indexed = 1,
988 	.differential = 1,
989 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
990 			      BIT(IIO_CHAN_INFO_SCALE) |
991 			      BIT(IIO_CHAN_INFO_OFFSET) |
992 			      BIT(IIO_CHAN_INFO_SAMP_FREQ),
993 	.info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE) |
994 					BIT(IIO_CHAN_INFO_SAMP_FREQ),
995 	.ext_info = ad4130_filter_mode_ext_info,
996 	.scan_type = {
997 		.sign = 'u',
998 		.endianness = IIO_BE,
999 	},
1000 };
1001 
ad4130_set_channel_pga(struct ad4130_state * st,unsigned int channel,int val,int val2)1002 static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel,
1003 				  int val, int val2)
1004 {
1005 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
1006 	struct ad4130_setup_info *setup_info = &chan_info->setup;
1007 	unsigned int pga, old_pga;
1008 	int ret;
1009 
1010 	for (pga = 0; pga < AD4130_MAX_PGA; pga++)
1011 		if (val == st->scale_tbls[setup_info->ref_sel][pga][0] &&
1012 		    val2 == st->scale_tbls[setup_info->ref_sel][pga][1])
1013 			break;
1014 
1015 	if (pga == AD4130_MAX_PGA)
1016 		return -EINVAL;
1017 
1018 	guard(mutex)(&st->lock);
1019 	if (pga == setup_info->pga)
1020 		return 0;
1021 
1022 	old_pga = setup_info->pga;
1023 	setup_info->pga = pga;
1024 
1025 	ret = ad4130_write_channel_setup(st, channel, false);
1026 	if (ret) {
1027 		setup_info->pga = old_pga;
1028 		return ret;
1029 	}
1030 
1031 	return 0;
1032 }
1033 
ad4130_set_channel_freq(struct ad4130_state * st,unsigned int channel,int val,int val2)1034 static int ad4130_set_channel_freq(struct ad4130_state *st,
1035 				   unsigned int channel, int val, int val2)
1036 {
1037 	struct ad4130_chan_info *chan_info = &st->chans_info[channel];
1038 	struct ad4130_setup_info *setup_info = &chan_info->setup;
1039 	unsigned int fs, old_fs;
1040 	int ret;
1041 
1042 	guard(mutex)(&st->lock);
1043 	old_fs = setup_info->fs;
1044 
1045 	ad4130_freq_to_fs(setup_info->filter_mode, val, val2, &fs);
1046 
1047 	if (fs == setup_info->fs)
1048 		return 0;
1049 
1050 	setup_info->fs = fs;
1051 
1052 	ret = ad4130_write_channel_setup(st, channel, false);
1053 	if (ret) {
1054 		setup_info->fs = old_fs;
1055 		return ret;
1056 	}
1057 
1058 	return 0;
1059 }
1060 
_ad4130_read_sample(struct iio_dev * indio_dev,unsigned int channel,int * val)1061 static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
1062 			       int *val)
1063 {
1064 	struct ad4130_state *st = iio_priv(indio_dev);
1065 	int ret;
1066 
1067 	ret = ad4130_set_channel_enable(st, channel, true);
1068 	if (ret)
1069 		return ret;
1070 
1071 	reinit_completion(&st->completion);
1072 
1073 	ret = ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
1074 	if (ret)
1075 		return ret;
1076 
1077 	ret = wait_for_completion_timeout(&st->completion,
1078 					  msecs_to_jiffies(1000));
1079 	if (!ret)
1080 		return -ETIMEDOUT;
1081 
1082 	ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
1083 	if (ret)
1084 		return ret;
1085 
1086 	ret = regmap_read(st->regmap, AD4130_DATA_REG, val);
1087 	if (ret)
1088 		return ret;
1089 
1090 	ret = ad4130_set_channel_enable(st, channel, false);
1091 	if (ret)
1092 		return ret;
1093 
1094 	return IIO_VAL_INT;
1095 }
1096 
ad4130_read_sample(struct iio_dev * indio_dev,unsigned int channel,int * val)1097 static int ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
1098 			      int *val)
1099 {
1100 	iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
1101 		struct ad4130_state *st = iio_priv(indio_dev);
1102 
1103 		guard(mutex)(&st->lock);
1104 		return _ad4130_read_sample(indio_dev, channel, val);
1105 	}
1106 	unreachable();
1107 }
1108 
ad4130_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)1109 static int ad4130_read_raw(struct iio_dev *indio_dev,
1110 			   struct iio_chan_spec const *chan,
1111 			   int *val, int *val2, long info)
1112 {
1113 	struct ad4130_state *st = iio_priv(indio_dev);
1114 	unsigned int channel = chan->scan_index;
1115 	struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
1116 
1117 	switch (info) {
1118 	case IIO_CHAN_INFO_RAW:
1119 		return ad4130_read_sample(indio_dev, channel, val);
1120 	case IIO_CHAN_INFO_SCALE: {
1121 		guard(mutex)(&st->lock);
1122 		*val = st->scale_tbls[setup_info->ref_sel][setup_info->pga][0];
1123 		*val2 = st->scale_tbls[setup_info->ref_sel][setup_info->pga][1];
1124 
1125 		return IIO_VAL_INT_PLUS_NANO;
1126 	}
1127 	case IIO_CHAN_INFO_OFFSET:
1128 		*val = st->bipolar ? -BIT(chan->scan_type.realbits - 1) : 0;
1129 
1130 		return IIO_VAL_INT;
1131 	case IIO_CHAN_INFO_SAMP_FREQ: {
1132 		guard(mutex)(&st->lock);
1133 		ad4130_fs_to_freq(setup_info->filter_mode, setup_info->fs,
1134 				  val, val2);
1135 
1136 		return IIO_VAL_INT_PLUS_NANO;
1137 	}
1138 	default:
1139 		return -EINVAL;
1140 	}
1141 }
1142 
ad4130_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long info)1143 static int ad4130_read_avail(struct iio_dev *indio_dev,
1144 			     struct iio_chan_spec const *chan,
1145 			     const int **vals, int *type, int *length,
1146 			     long info)
1147 {
1148 	struct ad4130_state *st = iio_priv(indio_dev);
1149 	unsigned int channel = chan->scan_index;
1150 	struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
1151 	const struct ad4130_filter_config *filter_config;
1152 
1153 	switch (info) {
1154 	case IIO_CHAN_INFO_SCALE:
1155 		*vals = (int *)st->scale_tbls[setup_info->ref_sel];
1156 		*length = ARRAY_SIZE(st->scale_tbls[setup_info->ref_sel]) * 2;
1157 
1158 		*type = IIO_VAL_INT_PLUS_NANO;
1159 
1160 		return IIO_AVAIL_LIST;
1161 	case IIO_CHAN_INFO_SAMP_FREQ:
1162 		scoped_guard(mutex, &st->lock) {
1163 			filter_config = &ad4130_filter_configs[setup_info->filter_mode];
1164 		}
1165 
1166 		*vals = (int *)filter_config->samp_freq_avail;
1167 		*length = filter_config->samp_freq_avail_len * 2;
1168 		*type = IIO_VAL_FRACTIONAL;
1169 
1170 		return filter_config->samp_freq_avail_type;
1171 	default:
1172 		return -EINVAL;
1173 	}
1174 }
1175 
ad4130_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long info)1176 static int ad4130_write_raw_get_fmt(struct iio_dev *indio_dev,
1177 				    struct iio_chan_spec const *chan,
1178 				    long info)
1179 {
1180 	switch (info) {
1181 	case IIO_CHAN_INFO_SCALE:
1182 	case IIO_CHAN_INFO_SAMP_FREQ:
1183 		return IIO_VAL_INT_PLUS_NANO;
1184 	default:
1185 		return -EINVAL;
1186 	}
1187 }
1188 
ad4130_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)1189 static int ad4130_write_raw(struct iio_dev *indio_dev,
1190 			    struct iio_chan_spec const *chan,
1191 			    int val, int val2, long info)
1192 {
1193 	struct ad4130_state *st = iio_priv(indio_dev);
1194 	unsigned int channel = chan->scan_index;
1195 
1196 	switch (info) {
1197 	case IIO_CHAN_INFO_SCALE:
1198 		return ad4130_set_channel_pga(st, channel, val, val2);
1199 	case IIO_CHAN_INFO_SAMP_FREQ:
1200 		return ad4130_set_channel_freq(st, channel, val, val2);
1201 	default:
1202 		return -EINVAL;
1203 	}
1204 }
1205 
ad4130_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)1206 static int ad4130_reg_access(struct iio_dev *indio_dev, unsigned int reg,
1207 			     unsigned int writeval, unsigned int *readval)
1208 {
1209 	struct ad4130_state *st = iio_priv(indio_dev);
1210 
1211 	if (readval)
1212 		return regmap_read(st->regmap, reg, readval);
1213 
1214 	return regmap_write(st->regmap, reg, writeval);
1215 }
1216 
ad4130_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)1217 static int ad4130_update_scan_mode(struct iio_dev *indio_dev,
1218 				   const unsigned long *scan_mask)
1219 {
1220 	struct ad4130_state *st = iio_priv(indio_dev);
1221 	unsigned int channel;
1222 	unsigned int val = 0;
1223 	int ret;
1224 
1225 	guard(mutex)(&st->lock);
1226 
1227 	for_each_set_bit(channel, scan_mask, indio_dev->num_channels) {
1228 		ret = ad4130_set_channel_enable(st, channel, true);
1229 		if (ret)
1230 			return ret;
1231 
1232 		val++;
1233 	}
1234 
1235 	st->num_enabled_channels = val;
1236 
1237 	return 0;
1238 }
1239 
ad4130_set_fifo_watermark(struct iio_dev * indio_dev,unsigned int val)1240 static int ad4130_set_fifo_watermark(struct iio_dev *indio_dev, unsigned int val)
1241 {
1242 	struct ad4130_state *st = iio_priv(indio_dev);
1243 	unsigned int eff;
1244 	int ret;
1245 
1246 	if (val > AD4130_FIFO_SIZE)
1247 		return -EINVAL;
1248 
1249 	eff = val * st->num_enabled_channels;
1250 	if (eff > AD4130_FIFO_SIZE)
1251 		/*
1252 		 * Always set watermark to a multiple of the number of
1253 		 * enabled channels to avoid making the FIFO unaligned.
1254 		 */
1255 		eff = rounddown(AD4130_FIFO_SIZE, st->num_enabled_channels);
1256 
1257 	guard(mutex)(&st->lock);
1258 
1259 	ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
1260 				 AD4130_FIFO_CONTROL_WM_MASK,
1261 				 FIELD_PREP(AD4130_FIFO_CONTROL_WM_MASK,
1262 					    ad4130_watermark_reg_val(eff)));
1263 	if (ret)
1264 		return ret;
1265 
1266 	st->effective_watermark = eff;
1267 	st->watermark = val;
1268 
1269 	return 0;
1270 }
1271 
1272 static const struct iio_info ad4130_info = {
1273 	.read_raw = ad4130_read_raw,
1274 	.read_avail = ad4130_read_avail,
1275 	.write_raw_get_fmt = ad4130_write_raw_get_fmt,
1276 	.write_raw = ad4130_write_raw,
1277 	.update_scan_mode = ad4130_update_scan_mode,
1278 	.hwfifo_set_watermark = ad4130_set_fifo_watermark,
1279 	.debugfs_reg_access = ad4130_reg_access,
1280 };
1281 
ad4130_buffer_postenable(struct iio_dev * indio_dev)1282 static int ad4130_buffer_postenable(struct iio_dev *indio_dev)
1283 {
1284 	struct ad4130_state *st = iio_priv(indio_dev);
1285 	int ret;
1286 
1287 	guard(mutex)(&st->lock);
1288 
1289 	ret = ad4130_set_watermark_interrupt_en(st, true);
1290 	if (ret)
1291 		return ret;
1292 
1293 	ret = irq_set_irq_type(st->spi->irq, st->inv_irq_trigger);
1294 	if (ret)
1295 		return ret;
1296 
1297 	ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_WM);
1298 	if (ret)
1299 		return ret;
1300 
1301 	return ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
1302 }
1303 
ad4130_buffer_predisable(struct iio_dev * indio_dev)1304 static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
1305 {
1306 	struct ad4130_state *st = iio_priv(indio_dev);
1307 	unsigned int i;
1308 	int ret;
1309 
1310 	guard(mutex)(&st->lock);
1311 
1312 	ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
1313 	if (ret)
1314 		return ret;
1315 
1316 	ret = irq_set_irq_type(st->spi->irq, st->irq_trigger);
1317 	if (ret)
1318 		return ret;
1319 
1320 	ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_DISABLED);
1321 	if (ret)
1322 		return ret;
1323 
1324 	ret = ad4130_set_watermark_interrupt_en(st, false);
1325 	if (ret)
1326 		return ret;
1327 
1328 	/*
1329 	 * update_scan_mode() is not called in the disable path, disable all
1330 	 * channels here.
1331 	 */
1332 	for (i = 0; i < indio_dev->num_channels; i++) {
1333 		ret = ad4130_set_channel_enable(st, i, false);
1334 		if (ret)
1335 			return ret;
1336 	}
1337 
1338 	return 0;
1339 }
1340 
1341 static const struct iio_buffer_setup_ops ad4130_buffer_ops = {
1342 	.postenable = ad4130_buffer_postenable,
1343 	.predisable = ad4130_buffer_predisable,
1344 };
1345 
hwfifo_watermark_show(struct device * dev,struct device_attribute * attr,char * buf)1346 static ssize_t hwfifo_watermark_show(struct device *dev,
1347 				     struct device_attribute *attr, char *buf)
1348 {
1349 	struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
1350 	unsigned int val;
1351 
1352 	guard(mutex)(&st->lock);
1353 	val = st->watermark;
1354 
1355 	return sysfs_emit(buf, "%d\n", val);
1356 }
1357 
hwfifo_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)1358 static ssize_t hwfifo_enabled_show(struct device *dev,
1359 				   struct device_attribute *attr, char *buf)
1360 {
1361 	struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
1362 	unsigned int val;
1363 	int ret;
1364 
1365 	ret = regmap_read(st->regmap, AD4130_FIFO_CONTROL_REG, &val);
1366 	if (ret)
1367 		return ret;
1368 
1369 	val = FIELD_GET(AD4130_FIFO_CONTROL_MODE_MASK, val);
1370 
1371 	return sysfs_emit(buf, "%d\n", val != AD4130_FIFO_MODE_DISABLED);
1372 }
1373 
hwfifo_watermark_min_show(struct device * dev,struct device_attribute * attr,char * buf)1374 static ssize_t hwfifo_watermark_min_show(struct device *dev,
1375 					 struct device_attribute *attr,
1376 					 char *buf)
1377 {
1378 	return sysfs_emit(buf, "%s\n", "1");
1379 }
1380 
hwfifo_watermark_max_show(struct device * dev,struct device_attribute * attr,char * buf)1381 static ssize_t hwfifo_watermark_max_show(struct device *dev,
1382 					 struct device_attribute *attr,
1383 					 char *buf)
1384 {
1385 	return sysfs_emit(buf, "%s\n", __stringify(AD4130_FIFO_SIZE));
1386 }
1387 
1388 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
1389 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
1390 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
1391 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
1392 
1393 static const struct iio_dev_attr *ad4130_fifo_attributes[] = {
1394 	&iio_dev_attr_hwfifo_watermark_min,
1395 	&iio_dev_attr_hwfifo_watermark_max,
1396 	&iio_dev_attr_hwfifo_watermark,
1397 	&iio_dev_attr_hwfifo_enabled,
1398 	NULL
1399 };
1400 
_ad4130_find_table_index(const unsigned int * tbl,size_t len,unsigned int val)1401 static int _ad4130_find_table_index(const unsigned int *tbl, size_t len,
1402 				    unsigned int val)
1403 {
1404 	unsigned int i;
1405 
1406 	for (i = 0; i < len; i++)
1407 		if (tbl[i] == val)
1408 			return i;
1409 
1410 	return -EINVAL;
1411 }
1412 
1413 #define ad4130_find_table_index(table, val) \
1414 	_ad4130_find_table_index(table, ARRAY_SIZE(table), val)
1415 
ad4130_get_ref_voltage(struct ad4130_state * st,enum ad4130_ref_sel ref_sel)1416 static int ad4130_get_ref_voltage(struct ad4130_state *st,
1417 				  enum ad4130_ref_sel ref_sel)
1418 {
1419 	switch (ref_sel) {
1420 	case AD4130_REF_REFIN1:
1421 		return regulator_get_voltage(st->regulators[2].consumer);
1422 	case AD4130_REF_REFIN2:
1423 		return regulator_get_voltage(st->regulators[3].consumer);
1424 	case AD4130_REF_AVDD_AVSS:
1425 		return regulator_get_voltage(st->regulators[0].consumer);
1426 	case AD4130_REF_REFOUT_AVSS:
1427 		return st->int_ref_uv;
1428 	default:
1429 		return -EINVAL;
1430 	}
1431 }
1432 
ad4130_parse_fw_setup(struct ad4130_state * st,struct fwnode_handle * child,struct ad4130_setup_info * setup_info)1433 static int ad4130_parse_fw_setup(struct ad4130_state *st,
1434 				 struct fwnode_handle *child,
1435 				 struct ad4130_setup_info *setup_info)
1436 {
1437 	struct device *dev = &st->spi->dev;
1438 	u32 tmp;
1439 	int ret;
1440 
1441 	tmp = 0;
1442 	fwnode_property_read_u32(child, "adi,excitation-current-0-nanoamp", &tmp);
1443 	ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
1444 	if (ret < 0)
1445 		return dev_err_probe(dev, ret,
1446 				     "Invalid excitation current %unA\n", tmp);
1447 	setup_info->iout0_val = ret;
1448 
1449 	tmp = 0;
1450 	fwnode_property_read_u32(child, "adi,excitation-current-1-nanoamp", &tmp);
1451 	ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
1452 	if (ret < 0)
1453 		return dev_err_probe(dev, ret,
1454 				     "Invalid excitation current %unA\n", tmp);
1455 	setup_info->iout1_val = ret;
1456 
1457 	tmp = 0;
1458 	fwnode_property_read_u32(child, "adi,burnout-current-nanoamp", &tmp);
1459 	ret = ad4130_find_table_index(ad4130_burnout_current_na_tbl, tmp);
1460 	if (ret < 0)
1461 		return dev_err_probe(dev, ret,
1462 				     "Invalid burnout current %unA\n", tmp);
1463 	setup_info->burnout = ret;
1464 
1465 	setup_info->ref_bufp = fwnode_property_read_bool(child, "adi,buffered-positive");
1466 	setup_info->ref_bufm = fwnode_property_read_bool(child, "adi,buffered-negative");
1467 
1468 	setup_info->ref_sel = AD4130_REF_REFIN1;
1469 	fwnode_property_read_u32(child, "adi,reference-select",
1470 				 &setup_info->ref_sel);
1471 	if (setup_info->ref_sel >= AD4130_REF_SEL_MAX)
1472 		return dev_err_probe(dev, -EINVAL,
1473 				     "Invalid reference selected %u\n",
1474 				     setup_info->ref_sel);
1475 
1476 	if (setup_info->ref_sel == AD4130_REF_REFOUT_AVSS)
1477 		st->int_ref_en = true;
1478 
1479 	ret = ad4130_get_ref_voltage(st, setup_info->ref_sel);
1480 	if (ret < 0)
1481 		return dev_err_probe(dev, ret, "Cannot use reference %u\n",
1482 				     setup_info->ref_sel);
1483 
1484 	return 0;
1485 }
1486 
ad4130_validate_diff_channel(struct ad4130_state * st,u32 pin)1487 static int ad4130_validate_diff_channel(struct ad4130_state *st, u32 pin)
1488 {
1489 	struct device *dev = &st->spi->dev;
1490 
1491 	if (pin >= AD4130_MAX_DIFF_INPUTS)
1492 		return dev_err_probe(dev, -EINVAL,
1493 				     "Invalid differential channel %u\n", pin);
1494 
1495 	if (pin >= AD4130_MAX_ANALOG_PINS)
1496 		return 0;
1497 
1498 	if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1499 		return dev_err_probe(dev, -EINVAL,
1500 				     "Pin %u already used with fn %u\n", pin,
1501 				     st->pins_fn[pin]);
1502 
1503 	st->pins_fn[pin] |= AD4130_PIN_FN_DIFF;
1504 
1505 	return 0;
1506 }
1507 
ad4130_validate_diff_channels(struct ad4130_state * st,u32 * pins,unsigned int len)1508 static int ad4130_validate_diff_channels(struct ad4130_state *st,
1509 					 u32 *pins, unsigned int len)
1510 {
1511 	unsigned int i;
1512 	int ret;
1513 
1514 	for (i = 0; i < len; i++) {
1515 		ret = ad4130_validate_diff_channel(st, pins[i]);
1516 		if (ret)
1517 			return ret;
1518 	}
1519 
1520 	return 0;
1521 }
1522 
ad4130_validate_excitation_pin(struct ad4130_state * st,u32 pin)1523 static int ad4130_validate_excitation_pin(struct ad4130_state *st, u32 pin)
1524 {
1525 	struct device *dev = &st->spi->dev;
1526 
1527 	if (pin >= AD4130_MAX_ANALOG_PINS)
1528 		return dev_err_probe(dev, -EINVAL,
1529 				     "Invalid excitation pin %u\n", pin);
1530 
1531 	if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1532 		return dev_err_probe(dev, -EINVAL,
1533 				     "Pin %u already used with fn %u\n", pin,
1534 				     st->pins_fn[pin]);
1535 
1536 	st->pins_fn[pin] |= AD4130_PIN_FN_EXCITATION;
1537 
1538 	return 0;
1539 }
1540 
ad4130_validate_vbias_pin(struct ad4130_state * st,u32 pin)1541 static int ad4130_validate_vbias_pin(struct ad4130_state *st, u32 pin)
1542 {
1543 	struct device *dev = &st->spi->dev;
1544 
1545 	if (pin >= AD4130_MAX_ANALOG_PINS)
1546 		return dev_err_probe(dev, -EINVAL, "Invalid vbias pin %u\n",
1547 				     pin);
1548 
1549 	if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
1550 		return dev_err_probe(dev, -EINVAL,
1551 				     "Pin %u already used with fn %u\n", pin,
1552 				     st->pins_fn[pin]);
1553 
1554 	st->pins_fn[pin] |= AD4130_PIN_FN_VBIAS;
1555 
1556 	return 0;
1557 }
1558 
ad4130_validate_vbias_pins(struct ad4130_state * st,u32 * pins,unsigned int len)1559 static int ad4130_validate_vbias_pins(struct ad4130_state *st,
1560 				      u32 *pins, unsigned int len)
1561 {
1562 	unsigned int i;
1563 	int ret;
1564 
1565 	for (i = 0; i < st->num_vbias_pins; i++) {
1566 		ret = ad4130_validate_vbias_pin(st, pins[i]);
1567 		if (ret)
1568 			return ret;
1569 	}
1570 
1571 	return 0;
1572 }
1573 
ad4130_parse_fw_channel(struct iio_dev * indio_dev,struct fwnode_handle * child)1574 static int ad4130_parse_fw_channel(struct iio_dev *indio_dev,
1575 				   struct fwnode_handle *child)
1576 {
1577 	struct ad4130_state *st = iio_priv(indio_dev);
1578 	unsigned int resolution = ad4130_resolution(st);
1579 	unsigned int index = indio_dev->num_channels++;
1580 	struct device *dev = &st->spi->dev;
1581 	struct ad4130_chan_info *chan_info;
1582 	struct iio_chan_spec *chan;
1583 	u32 pins[2];
1584 	int ret;
1585 
1586 	if (index >= AD4130_MAX_CHANNELS)
1587 		return dev_err_probe(dev, -EINVAL, "Too many channels\n");
1588 
1589 	chan = &st->chans[index];
1590 	chan_info = &st->chans_info[index];
1591 
1592 	*chan = ad4130_channel_template;
1593 	chan->scan_type.realbits = resolution;
1594 	chan->scan_type.storagebits = resolution;
1595 	chan->scan_index = index;
1596 
1597 	chan_info->slot = AD4130_INVALID_SLOT;
1598 	chan_info->setup.fs = AD4130_FILTER_SELECT_MIN;
1599 	chan_info->initialized = true;
1600 
1601 	ret = fwnode_property_read_u32_array(child, "diff-channels", pins,
1602 					     ARRAY_SIZE(pins));
1603 	if (ret)
1604 		return ret;
1605 
1606 	ret = ad4130_validate_diff_channels(st, pins, ARRAY_SIZE(pins));
1607 	if (ret)
1608 		return ret;
1609 
1610 	chan->channel = pins[0];
1611 	chan->channel2 = pins[1];
1612 
1613 	ret = ad4130_parse_fw_setup(st, child, &chan_info->setup);
1614 	if (ret)
1615 		return ret;
1616 
1617 	fwnode_property_read_u32(child, "adi,excitation-pin-0",
1618 				 &chan_info->iout0);
1619 	if (chan_info->setup.iout0_val != AD4130_IOUT_OFF) {
1620 		ret = ad4130_validate_excitation_pin(st, chan_info->iout0);
1621 		if (ret)
1622 			return ret;
1623 	}
1624 
1625 	fwnode_property_read_u32(child, "adi,excitation-pin-1",
1626 				 &chan_info->iout1);
1627 	if (chan_info->setup.iout1_val != AD4130_IOUT_OFF) {
1628 		ret = ad4130_validate_excitation_pin(st, chan_info->iout1);
1629 		if (ret)
1630 			return ret;
1631 	}
1632 
1633 	return 0;
1634 }
1635 
ad4130_parse_fw_children(struct iio_dev * indio_dev)1636 static int ad4130_parse_fw_children(struct iio_dev *indio_dev)
1637 {
1638 	struct ad4130_state *st = iio_priv(indio_dev);
1639 	struct device *dev = &st->spi->dev;
1640 	int ret;
1641 
1642 	indio_dev->channels = st->chans;
1643 
1644 	device_for_each_child_node_scoped(dev, child) {
1645 		ret = ad4130_parse_fw_channel(indio_dev, child);
1646 		if (ret)
1647 			return ret;
1648 	}
1649 
1650 	return 0;
1651 }
1652 
ad4310_parse_fw(struct iio_dev * indio_dev)1653 static int ad4310_parse_fw(struct iio_dev *indio_dev)
1654 {
1655 	struct ad4130_state *st = iio_priv(indio_dev);
1656 	struct device *dev = &st->spi->dev;
1657 	u32 ext_clk_freq = AD4130_MCLK_FREQ_76_8KHZ;
1658 	unsigned int i;
1659 	int avdd_uv;
1660 	int irq;
1661 	int ret;
1662 
1663 	st->mclk = devm_clk_get_optional(dev, "mclk");
1664 	if (IS_ERR(st->mclk))
1665 		return dev_err_probe(dev, PTR_ERR(st->mclk),
1666 				     "Failed to get mclk\n");
1667 
1668 	st->int_pin_sel = AD4130_INT_PIN_INT;
1669 
1670 	for (i = 0; i < ARRAY_SIZE(ad4130_int_pin_names); i++) {
1671 		irq = fwnode_irq_get_byname(dev_fwnode(dev),
1672 					    ad4130_int_pin_names[i]);
1673 		if (irq > 0) {
1674 			st->int_pin_sel = i;
1675 			break;
1676 		}
1677 	}
1678 
1679 	if (st->int_pin_sel == AD4130_INT_PIN_DOUT)
1680 		return dev_err_probe(dev, -EINVAL,
1681 				     "Cannot use DOUT as interrupt pin\n");
1682 
1683 	if (st->int_pin_sel == AD4130_INT_PIN_P2)
1684 		st->pins_fn[AD4130_AIN3_P2] = AD4130_PIN_FN_SPECIAL;
1685 
1686 	device_property_read_u32(dev, "adi,ext-clk-freq-hz", &ext_clk_freq);
1687 	if (ext_clk_freq != AD4130_MCLK_FREQ_153_6KHZ &&
1688 	    ext_clk_freq != AD4130_MCLK_FREQ_76_8KHZ)
1689 		return dev_err_probe(dev, -EINVAL,
1690 				     "Invalid external clock frequency %u\n",
1691 				     ext_clk_freq);
1692 
1693 	if (st->mclk && ext_clk_freq == AD4130_MCLK_FREQ_153_6KHZ)
1694 		st->mclk_sel = AD4130_MCLK_153_6KHZ_EXT;
1695 	else if (st->mclk)
1696 		st->mclk_sel = AD4130_MCLK_76_8KHZ_EXT;
1697 	else
1698 		st->mclk_sel = AD4130_MCLK_76_8KHZ;
1699 
1700 	if (st->int_pin_sel == AD4130_INT_PIN_CLK &&
1701 	    st->mclk_sel != AD4130_MCLK_76_8KHZ)
1702 		return dev_err_probe(dev, -EINVAL,
1703 				     "Invalid clock %u for interrupt pin %u\n",
1704 				     st->mclk_sel, st->int_pin_sel);
1705 
1706 	st->int_ref_uv = AD4130_INT_REF_2_5V;
1707 
1708 	/*
1709 	 * When the AVDD supply is set to below 2.5V the internal reference of
1710 	 * 1.25V should be selected.
1711 	 * See datasheet page 37, section ADC REFERENCE.
1712 	 */
1713 	avdd_uv = regulator_get_voltage(st->regulators[0].consumer);
1714 	if (avdd_uv > 0 && avdd_uv < AD4130_INT_REF_2_5V)
1715 		st->int_ref_uv = AD4130_INT_REF_1_25V;
1716 
1717 	st->bipolar = device_property_read_bool(dev, "adi,bipolar");
1718 
1719 	ret = device_property_count_u32(dev, "adi,vbias-pins");
1720 	if (ret > 0) {
1721 		if (ret > AD4130_MAX_ANALOG_PINS)
1722 			return dev_err_probe(dev, -EINVAL,
1723 					     "Too many vbias pins %u\n", ret);
1724 
1725 		st->num_vbias_pins = ret;
1726 
1727 		ret = device_property_read_u32_array(dev, "adi,vbias-pins",
1728 						     st->vbias_pins,
1729 						     st->num_vbias_pins);
1730 		if (ret)
1731 			return dev_err_probe(dev, ret,
1732 					     "Failed to read vbias pins\n");
1733 
1734 		ret = ad4130_validate_vbias_pins(st, st->vbias_pins,
1735 						 st->num_vbias_pins);
1736 		if (ret)
1737 			return ret;
1738 	}
1739 
1740 	ret = ad4130_parse_fw_children(indio_dev);
1741 	if (ret)
1742 		return ret;
1743 
1744 	return 0;
1745 }
1746 
ad4130_fill_scale_tbls(struct ad4130_state * st)1747 static void ad4130_fill_scale_tbls(struct ad4130_state *st)
1748 {
1749 	unsigned int pow = ad4130_resolution(st) - st->bipolar;
1750 	unsigned int i, j;
1751 
1752 	for (i = 0; i < AD4130_REF_SEL_MAX; i++) {
1753 		int ret;
1754 		u64 nv;
1755 
1756 		ret = ad4130_get_ref_voltage(st, i);
1757 		if (ret < 0)
1758 			continue;
1759 
1760 		nv = (u64)ret * NANO;
1761 
1762 		for (j = 0; j < AD4130_MAX_PGA; j++)
1763 			st->scale_tbls[i][j][1] = div_u64(nv >> (pow + j), MILLI);
1764 	}
1765 }
1766 
ad4130_clk_disable_unprepare(void * clk)1767 static void ad4130_clk_disable_unprepare(void *clk)
1768 {
1769 	clk_disable_unprepare(clk);
1770 }
1771 
ad4130_set_mclk_sel(struct ad4130_state * st,enum ad4130_mclk_sel mclk_sel)1772 static int ad4130_set_mclk_sel(struct ad4130_state *st,
1773 			       enum ad4130_mclk_sel mclk_sel)
1774 {
1775 	return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
1776 				 AD4130_ADC_CONTROL_MCLK_SEL_MASK,
1777 				 FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK,
1778 					    mclk_sel));
1779 }
1780 
ad4130_int_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)1781 static unsigned long ad4130_int_clk_recalc_rate(struct clk_hw *hw,
1782 						unsigned long parent_rate)
1783 {
1784 	return AD4130_MCLK_FREQ_76_8KHZ;
1785 }
1786 
ad4130_int_clk_is_enabled(struct clk_hw * hw)1787 static int ad4130_int_clk_is_enabled(struct clk_hw *hw)
1788 {
1789 	struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1790 
1791 	return st->mclk_sel == AD4130_MCLK_76_8KHZ_OUT;
1792 }
1793 
ad4130_int_clk_prepare(struct clk_hw * hw)1794 static int ad4130_int_clk_prepare(struct clk_hw *hw)
1795 {
1796 	struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1797 	int ret;
1798 
1799 	ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ_OUT);
1800 	if (ret)
1801 		return ret;
1802 
1803 	st->mclk_sel = AD4130_MCLK_76_8KHZ_OUT;
1804 
1805 	return 0;
1806 }
1807 
ad4130_int_clk_unprepare(struct clk_hw * hw)1808 static void ad4130_int_clk_unprepare(struct clk_hw *hw)
1809 {
1810 	struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
1811 	int ret;
1812 
1813 	ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ);
1814 	if (ret)
1815 		return;
1816 
1817 	st->mclk_sel = AD4130_MCLK_76_8KHZ;
1818 }
1819 
1820 static const struct clk_ops ad4130_int_clk_ops = {
1821 	.recalc_rate = ad4130_int_clk_recalc_rate,
1822 	.is_enabled = ad4130_int_clk_is_enabled,
1823 	.prepare = ad4130_int_clk_prepare,
1824 	.unprepare = ad4130_int_clk_unprepare,
1825 };
1826 
ad4130_setup_int_clk(struct ad4130_state * st)1827 static int ad4130_setup_int_clk(struct ad4130_state *st)
1828 {
1829 	struct device *dev = &st->spi->dev;
1830 	struct device_node *of_node = dev_of_node(dev);
1831 	struct clk_init_data init = {};
1832 	const char *clk_name;
1833 	int ret;
1834 
1835 	if (st->int_pin_sel == AD4130_INT_PIN_CLK ||
1836 	    st->mclk_sel != AD4130_MCLK_76_8KHZ)
1837 		return 0;
1838 
1839 	if (!of_node)
1840 		return 0;
1841 
1842 	clk_name = of_node->name;
1843 	of_property_read_string(of_node, "clock-output-names", &clk_name);
1844 
1845 	init.name = clk_name;
1846 	init.ops = &ad4130_int_clk_ops;
1847 
1848 	st->int_clk_hw.init = &init;
1849 	ret = devm_clk_hw_register(dev, &st->int_clk_hw);
1850 	if (ret)
1851 		return ret;
1852 
1853 	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
1854 					   &st->int_clk_hw);
1855 }
1856 
ad4130_setup(struct iio_dev * indio_dev)1857 static int ad4130_setup(struct iio_dev *indio_dev)
1858 {
1859 	struct ad4130_state *st = iio_priv(indio_dev);
1860 	struct device *dev = &st->spi->dev;
1861 	unsigned int int_ref_val;
1862 	unsigned long rate = AD4130_MCLK_FREQ_76_8KHZ;
1863 	unsigned int val;
1864 	unsigned int i;
1865 	int ret;
1866 
1867 	if (st->mclk_sel == AD4130_MCLK_153_6KHZ_EXT)
1868 		rate = AD4130_MCLK_FREQ_153_6KHZ;
1869 
1870 	ret = clk_set_rate(st->mclk, rate);
1871 	if (ret)
1872 		return ret;
1873 
1874 	ret = clk_prepare_enable(st->mclk);
1875 	if (ret)
1876 		return ret;
1877 
1878 	ret = devm_add_action_or_reset(dev, ad4130_clk_disable_unprepare,
1879 				       st->mclk);
1880 	if (ret)
1881 		return ret;
1882 
1883 	if (st->int_ref_uv == AD4130_INT_REF_2_5V)
1884 		int_ref_val = AD4130_INT_REF_VAL_2_5V;
1885 	else
1886 		int_ref_val = AD4130_INT_REF_VAL_1_25V;
1887 
1888 	/* Switch to SPI 4-wire mode. */
1889 	val =  FIELD_PREP(AD4130_ADC_CONTROL_CSB_EN_MASK, 1);
1890 	val |= FIELD_PREP(AD4130_ADC_CONTROL_BIPOLAR_MASK, st->bipolar);
1891 	val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_EN_MASK, st->int_ref_en);
1892 	val |= FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, AD4130_MODE_IDLE);
1893 	val |= FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK, st->mclk_sel);
1894 	val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_VAL_MASK, int_ref_val);
1895 
1896 	ret = regmap_write(st->regmap, AD4130_ADC_CONTROL_REG, val);
1897 	if (ret)
1898 		return ret;
1899 
1900 	/*
1901 	 * Configure unused GPIOs for output. If configured, the interrupt
1902 	 * function of P2 takes priority over the GPIO out function.
1903 	 */
1904 	val = 0;
1905 	for (i = 0; i < AD4130_MAX_GPIOS; i++)
1906 		if (st->pins_fn[i + AD4130_AIN2_P1] == AD4130_PIN_FN_NONE)
1907 			val |= FIELD_PREP(AD4130_IO_CONTROL_GPIO_CTRL_MASK, BIT(i));
1908 
1909 	val |= FIELD_PREP(AD4130_IO_CONTROL_INT_PIN_SEL_MASK, st->int_pin_sel);
1910 
1911 	ret = regmap_write(st->regmap, AD4130_IO_CONTROL_REG, val);
1912 	if (ret)
1913 		return ret;
1914 
1915 	val = 0;
1916 	for (i = 0; i < st->num_vbias_pins; i++)
1917 		val |= BIT(st->vbias_pins[i]);
1918 
1919 	ret = regmap_write(st->regmap, AD4130_VBIAS_REG, val);
1920 	if (ret)
1921 		return ret;
1922 
1923 	ret = regmap_clear_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
1924 				AD4130_FIFO_CONTROL_HEADER_MASK);
1925 	if (ret)
1926 		return ret;
1927 
1928 	/* FIFO watermark interrupt starts out as enabled, disable it. */
1929 	ret = ad4130_set_watermark_interrupt_en(st, false);
1930 	if (ret)
1931 		return ret;
1932 
1933 	/* Setup channels. */
1934 	for (i = 0; i < indio_dev->num_channels; i++) {
1935 		struct ad4130_chan_info *chan_info = &st->chans_info[i];
1936 		struct iio_chan_spec *chan = &st->chans[i];
1937 		unsigned int val;
1938 
1939 		val = FIELD_PREP(AD4130_CHANNEL_AINP_MASK, chan->channel) |
1940 		      FIELD_PREP(AD4130_CHANNEL_AINM_MASK, chan->channel2) |
1941 		      FIELD_PREP(AD4130_CHANNEL_IOUT1_MASK, chan_info->iout0) |
1942 		      FIELD_PREP(AD4130_CHANNEL_IOUT2_MASK, chan_info->iout1);
1943 
1944 		ret = regmap_write(st->regmap, AD4130_CHANNEL_X_REG(i), val);
1945 		if (ret)
1946 			return ret;
1947 	}
1948 
1949 	return 0;
1950 }
1951 
ad4130_soft_reset(struct ad4130_state * st)1952 static int ad4130_soft_reset(struct ad4130_state *st)
1953 {
1954 	int ret;
1955 
1956 	ret = spi_write(st->spi, st->reset_buf, sizeof(st->reset_buf));
1957 	if (ret)
1958 		return ret;
1959 
1960 	fsleep(AD4130_RESET_SLEEP_US);
1961 
1962 	return 0;
1963 }
1964 
ad4130_disable_regulators(void * data)1965 static void ad4130_disable_regulators(void *data)
1966 {
1967 	struct ad4130_state *st = data;
1968 
1969 	regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
1970 }
1971 
ad4130_probe(struct spi_device * spi)1972 static int ad4130_probe(struct spi_device *spi)
1973 {
1974 	struct device *dev = &spi->dev;
1975 	struct iio_dev *indio_dev;
1976 	struct ad4130_state *st;
1977 	int ret;
1978 
1979 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1980 	if (!indio_dev)
1981 		return -ENOMEM;
1982 
1983 	st = iio_priv(indio_dev);
1984 
1985 	memset(st->reset_buf, 0xff, sizeof(st->reset_buf));
1986 	init_completion(&st->completion);
1987 	mutex_init(&st->lock);
1988 	st->spi = spi;
1989 
1990 	/*
1991 	 * Xfer:   [ XFR1 ] [         XFR2         ]
1992 	 * Master:  0x7D N   ......................
1993 	 * Slave:   ......   DATA1 DATA2 ... DATAN
1994 	 */
1995 	st->fifo_tx_buf[0] = AD4130_COMMS_READ_MASK | AD4130_FIFO_DATA_REG;
1996 	st->fifo_xfer[0].tx_buf = st->fifo_tx_buf;
1997 	st->fifo_xfer[0].len = sizeof(st->fifo_tx_buf);
1998 	st->fifo_xfer[1].rx_buf = st->fifo_rx_buf;
1999 	spi_message_init_with_transfers(&st->fifo_msg, st->fifo_xfer,
2000 					ARRAY_SIZE(st->fifo_xfer));
2001 
2002 	indio_dev->name = AD4130_NAME;
2003 	indio_dev->modes = INDIO_DIRECT_MODE;
2004 	indio_dev->info = &ad4130_info;
2005 
2006 	st->regmap = devm_regmap_init(dev, NULL, st, &ad4130_regmap_config);
2007 	if (IS_ERR(st->regmap))
2008 		return PTR_ERR(st->regmap);
2009 
2010 	st->regulators[0].supply = "avdd";
2011 	st->regulators[1].supply = "iovdd";
2012 	st->regulators[2].supply = "refin1";
2013 	st->regulators[3].supply = "refin2";
2014 
2015 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
2016 				      st->regulators);
2017 	if (ret)
2018 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
2019 
2020 	ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
2021 	if (ret)
2022 		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
2023 
2024 	ret = devm_add_action_or_reset(dev, ad4130_disable_regulators, st);
2025 	if (ret)
2026 		return dev_err_probe(dev, ret,
2027 				     "Failed to add regulators disable action\n");
2028 
2029 	ret = ad4130_soft_reset(st);
2030 	if (ret)
2031 		return ret;
2032 
2033 	ret = ad4310_parse_fw(indio_dev);
2034 	if (ret)
2035 		return ret;
2036 
2037 	ret = ad4130_setup(indio_dev);
2038 	if (ret)
2039 		return ret;
2040 
2041 	ret = ad4130_setup_int_clk(st);
2042 	if (ret)
2043 		return ret;
2044 
2045 	ad4130_fill_scale_tbls(st);
2046 
2047 	st->gc.owner = THIS_MODULE;
2048 	st->gc.label = AD4130_NAME;
2049 	st->gc.base = -1;
2050 	st->gc.ngpio = AD4130_MAX_GPIOS;
2051 	st->gc.parent = dev;
2052 	st->gc.can_sleep = true;
2053 	st->gc.init_valid_mask = ad4130_gpio_init_valid_mask;
2054 	st->gc.get_direction = ad4130_gpio_get_direction;
2055 	st->gc.set = ad4130_gpio_set;
2056 
2057 	ret = devm_gpiochip_add_data(dev, &st->gc, st);
2058 	if (ret)
2059 		return ret;
2060 
2061 	ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev,
2062 					      &ad4130_buffer_ops,
2063 					      ad4130_fifo_attributes);
2064 	if (ret)
2065 		return ret;
2066 
2067 	ret = devm_request_threaded_irq(dev, spi->irq, NULL,
2068 					ad4130_irq_handler, IRQF_ONESHOT,
2069 					indio_dev->name, indio_dev);
2070 	if (ret)
2071 		return dev_err_probe(dev, ret, "Failed to request irq\n");
2072 
2073 	/*
2074 	 * When the chip enters FIFO mode, IRQ polarity is inverted.
2075 	 * When the chip exits FIFO mode, IRQ polarity returns to normal.
2076 	 * See datasheet pages: 65, FIFO Watermark Interrupt section,
2077 	 * and 71, Bit Descriptions for STATUS Register, RDYB.
2078 	 * Cache the normal and inverted IRQ triggers to set them when
2079 	 * entering and exiting FIFO mode.
2080 	 */
2081 	st->irq_trigger = irq_get_trigger_type(spi->irq);
2082 	if (st->irq_trigger & IRQF_TRIGGER_RISING)
2083 		st->inv_irq_trigger = IRQF_TRIGGER_FALLING;
2084 	else if (st->irq_trigger & IRQF_TRIGGER_FALLING)
2085 		st->inv_irq_trigger = IRQF_TRIGGER_RISING;
2086 	else
2087 		return dev_err_probe(dev, -EINVAL, "Invalid irq flags: %u\n",
2088 				     st->irq_trigger);
2089 
2090 	return devm_iio_device_register(dev, indio_dev);
2091 }
2092 
2093 static const struct of_device_id ad4130_of_match[] = {
2094 	{
2095 		.compatible = "adi,ad4130",
2096 	},
2097 	{ }
2098 };
2099 MODULE_DEVICE_TABLE(of, ad4130_of_match);
2100 
2101 static struct spi_driver ad4130_driver = {
2102 	.driver = {
2103 		.name = AD4130_NAME,
2104 		.of_match_table = ad4130_of_match,
2105 	},
2106 	.probe = ad4130_probe,
2107 };
2108 module_spi_driver(ad4130_driver);
2109 
2110 MODULE_AUTHOR("Cosmin Tanislav <[email protected]>");
2111 MODULE_DESCRIPTION("Analog Devices AD4130 SPI driver");
2112 MODULE_LICENSE("GPL");
2113