drm/panel: simple: add Multi-Inno Technology MI0700A2T-30
[drm/drm-misc.git] / drivers / iio / filter / admv8818.c
blob848baa6e3bbf5d3f19242d128e19a52e67de3f80
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ADMV8818 driver
5 * Copyright 2021 Analog Devices Inc.
6 */
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/iio/iio.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
19 #include <linux/units.h>
21 /* ADMV8818 Register Map */
22 #define ADMV8818_REG_SPI_CONFIG_A 0x0
23 #define ADMV8818_REG_SPI_CONFIG_B 0x1
24 #define ADMV8818_REG_CHIPTYPE 0x3
25 #define ADMV8818_REG_PRODUCT_ID_L 0x4
26 #define ADMV8818_REG_PRODUCT_ID_H 0x5
27 #define ADMV8818_REG_FAST_LATCH_POINTER 0x10
28 #define ADMV8818_REG_FAST_LATCH_STOP 0x11
29 #define ADMV8818_REG_FAST_LATCH_START 0x12
30 #define ADMV8818_REG_FAST_LATCH_DIRECTION 0x13
31 #define ADMV8818_REG_FAST_LATCH_STATE 0x14
32 #define ADMV8818_REG_WR0_SW 0x20
33 #define ADMV8818_REG_WR0_FILTER 0x21
34 #define ADMV8818_REG_WR1_SW 0x22
35 #define ADMV8818_REG_WR1_FILTER 0x23
36 #define ADMV8818_REG_WR2_SW 0x24
37 #define ADMV8818_REG_WR2_FILTER 0x25
38 #define ADMV8818_REG_WR3_SW 0x26
39 #define ADMV8818_REG_WR3_FILTER 0x27
40 #define ADMV8818_REG_WR4_SW 0x28
41 #define ADMV8818_REG_WR4_FILTER 0x29
42 #define ADMV8818_REG_LUT0_SW 0x100
43 #define ADMV8818_REG_LUT0_FILTER 0x101
44 #define ADMV8818_REG_LUT127_SW 0x1FE
45 #define ADMV8818_REG_LUT127_FILTER 0x1FF
47 /* ADMV8818_REG_SPI_CONFIG_A Map */
48 #define ADMV8818_SOFTRESET_N_MSK BIT(7)
49 #define ADMV8818_LSB_FIRST_N_MSK BIT(6)
50 #define ADMV8818_ENDIAN_N_MSK BIT(5)
51 #define ADMV8818_SDOACTIVE_N_MSK BIT(4)
52 #define ADMV8818_SDOACTIVE_MSK BIT(3)
53 #define ADMV8818_ENDIAN_MSK BIT(2)
54 #define ADMV8818_LSBFIRST_MSK BIT(1)
55 #define ADMV8818_SOFTRESET_MSK BIT(0)
57 /* ADMV8818_REG_SPI_CONFIG_B Map */
58 #define ADMV8818_SINGLE_INSTRUCTION_MSK BIT(7)
59 #define ADMV8818_CSB_STALL_MSK BIT(6)
60 #define ADMV8818_MASTER_SLAVE_RB_MSK BIT(5)
61 #define ADMV8818_MASTER_SLAVE_TRANSFER_MSK BIT(0)
63 /* ADMV8818_REG_WR0_SW Map */
64 #define ADMV8818_SW_IN_SET_WR0_MSK BIT(7)
65 #define ADMV8818_SW_OUT_SET_WR0_MSK BIT(6)
66 #define ADMV8818_SW_IN_WR0_MSK GENMASK(5, 3)
67 #define ADMV8818_SW_OUT_WR0_MSK GENMASK(2, 0)
69 /* ADMV8818_REG_WR0_FILTER Map */
70 #define ADMV8818_HPF_WR0_MSK GENMASK(7, 4)
71 #define ADMV8818_LPF_WR0_MSK GENMASK(3, 0)
73 enum {
74 ADMV8818_BW_FREQ,
75 ADMV8818_CENTER_FREQ
78 enum {
79 ADMV8818_AUTO_MODE,
80 ADMV8818_MANUAL_MODE,
81 ADMV8818_BYPASS_MODE,
84 struct admv8818_state {
85 struct spi_device *spi;
86 struct regmap *regmap;
87 struct clk *clkin;
88 struct notifier_block nb;
89 /* Protect against concurrent accesses to the device and data content*/
90 struct mutex lock;
91 unsigned int filter_mode;
92 u64 cf_hz;
95 static const unsigned long long freq_range_hpf[4][2] = {
96 {1750000000ULL, 3550000000ULL},
97 {3400000000ULL, 7250000000ULL},
98 {6600000000, 12000000000},
99 {12500000000, 19900000000}
102 static const unsigned long long freq_range_lpf[4][2] = {
103 {2050000000ULL, 3850000000ULL},
104 {3350000000ULL, 7250000000ULL},
105 {7000000000, 13000000000},
106 {12550000000, 18500000000}
109 static const struct regmap_config admv8818_regmap_config = {
110 .reg_bits = 16,
111 .val_bits = 8,
112 .read_flag_mask = 0x80,
113 .max_register = 0x1FF,
116 static const char * const admv8818_modes[] = {
117 [0] = "auto",
118 [1] = "manual",
119 [2] = "bypass"
122 static int __admv8818_hpf_select(struct admv8818_state *st, u64 freq)
124 unsigned int hpf_step = 0, hpf_band = 0, i, j;
125 u64 freq_step;
126 int ret;
128 if (freq < freq_range_hpf[0][0])
129 goto hpf_write;
131 if (freq > freq_range_hpf[3][1]) {
132 hpf_step = 15;
133 hpf_band = 4;
135 goto hpf_write;
138 for (i = 0; i < 4; i++) {
139 freq_step = div_u64((freq_range_hpf[i][1] -
140 freq_range_hpf[i][0]), 15);
142 if (freq > freq_range_hpf[i][0] &&
143 (freq < freq_range_hpf[i][1] + freq_step)) {
144 hpf_band = i + 1;
146 for (j = 1; j <= 16; j++) {
147 if (freq < (freq_range_hpf[i][0] + (freq_step * j))) {
148 hpf_step = j - 1;
149 break;
152 break;
156 /* Close HPF frequency gap between 12 and 12.5 GHz */
157 if (freq >= 12000 * HZ_PER_MHZ && freq <= 12500 * HZ_PER_MHZ) {
158 hpf_band = 3;
159 hpf_step = 15;
162 hpf_write:
163 ret = regmap_update_bits(st->regmap, ADMV8818_REG_WR0_SW,
164 ADMV8818_SW_IN_SET_WR0_MSK |
165 ADMV8818_SW_IN_WR0_MSK,
166 FIELD_PREP(ADMV8818_SW_IN_SET_WR0_MSK, 1) |
167 FIELD_PREP(ADMV8818_SW_IN_WR0_MSK, hpf_band));
168 if (ret)
169 return ret;
171 return regmap_update_bits(st->regmap, ADMV8818_REG_WR0_FILTER,
172 ADMV8818_HPF_WR0_MSK,
173 FIELD_PREP(ADMV8818_HPF_WR0_MSK, hpf_step));
176 static int admv8818_hpf_select(struct admv8818_state *st, u64 freq)
178 int ret;
180 mutex_lock(&st->lock);
181 ret = __admv8818_hpf_select(st, freq);
182 mutex_unlock(&st->lock);
184 return ret;
187 static int __admv8818_lpf_select(struct admv8818_state *st, u64 freq)
189 unsigned int lpf_step = 0, lpf_band = 0, i, j;
190 u64 freq_step;
191 int ret;
193 if (freq > freq_range_lpf[3][1])
194 goto lpf_write;
196 if (freq < freq_range_lpf[0][0]) {
197 lpf_band = 1;
199 goto lpf_write;
202 for (i = 0; i < 4; i++) {
203 if (freq > freq_range_lpf[i][0] && freq < freq_range_lpf[i][1]) {
204 lpf_band = i + 1;
205 freq_step = div_u64((freq_range_lpf[i][1] - freq_range_lpf[i][0]), 15);
207 for (j = 0; j <= 15; j++) {
208 if (freq < (freq_range_lpf[i][0] + (freq_step * j))) {
209 lpf_step = j;
210 break;
213 break;
217 lpf_write:
218 ret = regmap_update_bits(st->regmap, ADMV8818_REG_WR0_SW,
219 ADMV8818_SW_OUT_SET_WR0_MSK |
220 ADMV8818_SW_OUT_WR0_MSK,
221 FIELD_PREP(ADMV8818_SW_OUT_SET_WR0_MSK, 1) |
222 FIELD_PREP(ADMV8818_SW_OUT_WR0_MSK, lpf_band));
223 if (ret)
224 return ret;
226 return regmap_update_bits(st->regmap, ADMV8818_REG_WR0_FILTER,
227 ADMV8818_LPF_WR0_MSK,
228 FIELD_PREP(ADMV8818_LPF_WR0_MSK, lpf_step));
231 static int admv8818_lpf_select(struct admv8818_state *st, u64 freq)
233 int ret;
235 mutex_lock(&st->lock);
236 ret = __admv8818_lpf_select(st, freq);
237 mutex_unlock(&st->lock);
239 return ret;
242 static int admv8818_rfin_band_select(struct admv8818_state *st)
244 int ret;
246 st->cf_hz = clk_get_rate(st->clkin);
248 mutex_lock(&st->lock);
250 ret = __admv8818_hpf_select(st, st->cf_hz);
251 if (ret)
252 goto exit;
254 ret = __admv8818_lpf_select(st, st->cf_hz);
255 exit:
256 mutex_unlock(&st->lock);
257 return ret;
260 static int __admv8818_read_hpf_freq(struct admv8818_state *st, u64 *hpf_freq)
262 unsigned int data, hpf_band, hpf_state;
263 int ret;
265 ret = regmap_read(st->regmap, ADMV8818_REG_WR0_SW, &data);
266 if (ret)
267 return ret;
269 hpf_band = FIELD_GET(ADMV8818_SW_IN_WR0_MSK, data);
270 if (!hpf_band || hpf_band > 4) {
271 *hpf_freq = 0;
272 return ret;
275 ret = regmap_read(st->regmap, ADMV8818_REG_WR0_FILTER, &data);
276 if (ret)
277 return ret;
279 hpf_state = FIELD_GET(ADMV8818_HPF_WR0_MSK, data);
281 *hpf_freq = div_u64(freq_range_hpf[hpf_band - 1][1] - freq_range_hpf[hpf_band - 1][0], 15);
282 *hpf_freq = freq_range_hpf[hpf_band - 1][0] + (*hpf_freq * hpf_state);
284 return ret;
287 static int admv8818_read_hpf_freq(struct admv8818_state *st, u64 *hpf_freq)
289 int ret;
291 mutex_lock(&st->lock);
292 ret = __admv8818_read_hpf_freq(st, hpf_freq);
293 mutex_unlock(&st->lock);
295 return ret;
298 static int __admv8818_read_lpf_freq(struct admv8818_state *st, u64 *lpf_freq)
300 unsigned int data, lpf_band, lpf_state;
301 int ret;
303 ret = regmap_read(st->regmap, ADMV8818_REG_WR0_SW, &data);
304 if (ret)
305 return ret;
307 lpf_band = FIELD_GET(ADMV8818_SW_OUT_WR0_MSK, data);
308 if (!lpf_band || lpf_band > 4) {
309 *lpf_freq = 0;
310 return ret;
313 ret = regmap_read(st->regmap, ADMV8818_REG_WR0_FILTER, &data);
314 if (ret)
315 return ret;
317 lpf_state = FIELD_GET(ADMV8818_LPF_WR0_MSK, data);
319 *lpf_freq = div_u64(freq_range_lpf[lpf_band - 1][1] - freq_range_lpf[lpf_band - 1][0], 15);
320 *lpf_freq = freq_range_lpf[lpf_band - 1][0] + (*lpf_freq * lpf_state);
322 return ret;
325 static int admv8818_read_lpf_freq(struct admv8818_state *st, u64 *lpf_freq)
327 int ret;
329 mutex_lock(&st->lock);
330 ret = __admv8818_read_lpf_freq(st, lpf_freq);
331 mutex_unlock(&st->lock);
333 return ret;
336 static int admv8818_write_raw(struct iio_dev *indio_dev,
337 struct iio_chan_spec const *chan,
338 int val, int val2, long info)
340 struct admv8818_state *st = iio_priv(indio_dev);
342 u64 freq = ((u64)val2 << 32 | (u32)val);
344 switch (info) {
345 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
346 return admv8818_lpf_select(st, freq);
347 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
348 return admv8818_hpf_select(st, freq);
349 default:
350 return -EINVAL;
354 static int admv8818_read_raw(struct iio_dev *indio_dev,
355 struct iio_chan_spec const *chan,
356 int *val, int *val2, long info)
358 struct admv8818_state *st = iio_priv(indio_dev);
359 int ret;
360 u64 freq;
362 switch (info) {
363 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
364 ret = admv8818_read_lpf_freq(st, &freq);
365 if (ret)
366 return ret;
368 *val = (u32)freq;
369 *val2 = (u32)(freq >> 32);
371 return IIO_VAL_INT_64;
372 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
373 ret = admv8818_read_hpf_freq(st, &freq);
374 if (ret)
375 return ret;
377 *val = (u32)freq;
378 *val2 = (u32)(freq >> 32);
380 return IIO_VAL_INT_64;
381 default:
382 return -EINVAL;
386 static int admv8818_reg_access(struct iio_dev *indio_dev,
387 unsigned int reg,
388 unsigned int write_val,
389 unsigned int *read_val)
391 struct admv8818_state *st = iio_priv(indio_dev);
393 if (read_val)
394 return regmap_read(st->regmap, reg, read_val);
395 else
396 return regmap_write(st->regmap, reg, write_val);
399 static int admv8818_filter_bypass(struct admv8818_state *st)
401 int ret;
403 mutex_lock(&st->lock);
405 ret = regmap_update_bits(st->regmap, ADMV8818_REG_WR0_SW,
406 ADMV8818_SW_IN_SET_WR0_MSK |
407 ADMV8818_SW_IN_WR0_MSK |
408 ADMV8818_SW_OUT_SET_WR0_MSK |
409 ADMV8818_SW_OUT_WR0_MSK,
410 FIELD_PREP(ADMV8818_SW_IN_SET_WR0_MSK, 1) |
411 FIELD_PREP(ADMV8818_SW_IN_WR0_MSK, 0) |
412 FIELD_PREP(ADMV8818_SW_OUT_SET_WR0_MSK, 1) |
413 FIELD_PREP(ADMV8818_SW_OUT_WR0_MSK, 0));
414 if (ret)
415 goto exit;
417 ret = regmap_update_bits(st->regmap, ADMV8818_REG_WR0_FILTER,
418 ADMV8818_HPF_WR0_MSK |
419 ADMV8818_LPF_WR0_MSK,
420 FIELD_PREP(ADMV8818_HPF_WR0_MSK, 0) |
421 FIELD_PREP(ADMV8818_LPF_WR0_MSK, 0));
423 exit:
424 mutex_unlock(&st->lock);
426 return ret;
429 static int admv8818_get_mode(struct iio_dev *indio_dev,
430 const struct iio_chan_spec *chan)
432 struct admv8818_state *st = iio_priv(indio_dev);
434 return st->filter_mode;
437 static int admv8818_set_mode(struct iio_dev *indio_dev,
438 const struct iio_chan_spec *chan,
439 unsigned int mode)
441 struct admv8818_state *st = iio_priv(indio_dev);
442 int ret = 0;
444 if (!st->clkin) {
445 if (mode == ADMV8818_MANUAL_MODE)
446 goto set_mode;
448 if (mode == ADMV8818_BYPASS_MODE) {
449 ret = admv8818_filter_bypass(st);
450 if (ret)
451 return ret;
453 goto set_mode;
456 return -EINVAL;
459 switch (mode) {
460 case ADMV8818_AUTO_MODE:
461 if (st->filter_mode == ADMV8818_AUTO_MODE)
462 return 0;
464 ret = clk_prepare_enable(st->clkin);
465 if (ret)
466 return ret;
468 ret = clk_notifier_register(st->clkin, &st->nb);
469 if (ret) {
470 clk_disable_unprepare(st->clkin);
472 return ret;
475 break;
476 case ADMV8818_MANUAL_MODE:
477 case ADMV8818_BYPASS_MODE:
478 if (st->filter_mode == ADMV8818_AUTO_MODE) {
479 clk_disable_unprepare(st->clkin);
481 ret = clk_notifier_unregister(st->clkin, &st->nb);
482 if (ret)
483 return ret;
486 if (mode == ADMV8818_BYPASS_MODE) {
487 ret = admv8818_filter_bypass(st);
488 if (ret)
489 return ret;
492 break;
493 default:
494 return -EINVAL;
497 set_mode:
498 st->filter_mode = mode;
500 return ret;
503 static const struct iio_info admv8818_info = {
504 .write_raw = admv8818_write_raw,
505 .read_raw = admv8818_read_raw,
506 .debugfs_reg_access = &admv8818_reg_access,
509 static const struct iio_enum admv8818_mode_enum = {
510 .items = admv8818_modes,
511 .num_items = ARRAY_SIZE(admv8818_modes),
512 .get = admv8818_get_mode,
513 .set = admv8818_set_mode,
516 static const struct iio_chan_spec_ext_info admv8818_ext_info[] = {
517 IIO_ENUM("filter_mode", IIO_SHARED_BY_ALL, &admv8818_mode_enum),
518 IIO_ENUM_AVAILABLE("filter_mode", IIO_SHARED_BY_ALL, &admv8818_mode_enum),
519 { },
522 #define ADMV8818_CHAN(_channel) { \
523 .type = IIO_ALTVOLTAGE, \
524 .output = 1, \
525 .indexed = 1, \
526 .channel = _channel, \
527 .info_mask_separate = \
528 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
529 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY) \
532 #define ADMV8818_CHAN_BW_CF(_channel, _admv8818_ext_info) { \
533 .type = IIO_ALTVOLTAGE, \
534 .output = 1, \
535 .indexed = 1, \
536 .channel = _channel, \
537 .ext_info = _admv8818_ext_info, \
540 static const struct iio_chan_spec admv8818_channels[] = {
541 ADMV8818_CHAN(0),
542 ADMV8818_CHAN_BW_CF(0, admv8818_ext_info),
545 static int admv8818_freq_change(struct notifier_block *nb, unsigned long action, void *data)
547 struct admv8818_state *st = container_of(nb, struct admv8818_state, nb);
549 if (action == POST_RATE_CHANGE)
550 return notifier_from_errno(admv8818_rfin_band_select(st));
552 return NOTIFY_OK;
555 static void admv8818_clk_notifier_unreg(void *data)
557 struct admv8818_state *st = data;
559 if (st->filter_mode == 0)
560 clk_notifier_unregister(st->clkin, &st->nb);
563 static void admv8818_clk_disable(void *data)
565 struct admv8818_state *st = data;
567 if (st->filter_mode == 0)
568 clk_disable_unprepare(st->clkin);
571 static int admv8818_init(struct admv8818_state *st)
573 int ret;
574 struct spi_device *spi = st->spi;
575 unsigned int chip_id;
577 ret = regmap_update_bits(st->regmap, ADMV8818_REG_SPI_CONFIG_A,
578 ADMV8818_SOFTRESET_N_MSK |
579 ADMV8818_SOFTRESET_MSK,
580 FIELD_PREP(ADMV8818_SOFTRESET_N_MSK, 1) |
581 FIELD_PREP(ADMV8818_SOFTRESET_MSK, 1));
582 if (ret) {
583 dev_err(&spi->dev, "ADMV8818 Soft Reset failed.\n");
584 return ret;
587 ret = regmap_update_bits(st->regmap, ADMV8818_REG_SPI_CONFIG_A,
588 ADMV8818_SDOACTIVE_N_MSK |
589 ADMV8818_SDOACTIVE_MSK,
590 FIELD_PREP(ADMV8818_SDOACTIVE_N_MSK, 1) |
591 FIELD_PREP(ADMV8818_SDOACTIVE_MSK, 1));
592 if (ret) {
593 dev_err(&spi->dev, "ADMV8818 SDO Enable failed.\n");
594 return ret;
597 ret = regmap_read(st->regmap, ADMV8818_REG_CHIPTYPE, &chip_id);
598 if (ret) {
599 dev_err(&spi->dev, "ADMV8818 Chip ID read failed.\n");
600 return ret;
603 if (chip_id != 0x1) {
604 dev_err(&spi->dev, "ADMV8818 Invalid Chip ID.\n");
605 return -EINVAL;
608 ret = regmap_update_bits(st->regmap, ADMV8818_REG_SPI_CONFIG_B,
609 ADMV8818_SINGLE_INSTRUCTION_MSK,
610 FIELD_PREP(ADMV8818_SINGLE_INSTRUCTION_MSK, 1));
611 if (ret) {
612 dev_err(&spi->dev, "ADMV8818 Single Instruction failed.\n");
613 return ret;
616 if (st->clkin)
617 return admv8818_rfin_band_select(st);
618 else
619 return 0;
622 static int admv8818_clk_setup(struct admv8818_state *st)
624 struct spi_device *spi = st->spi;
625 int ret;
627 st->clkin = devm_clk_get_optional(&spi->dev, "rf_in");
628 if (IS_ERR(st->clkin))
629 return dev_err_probe(&spi->dev, PTR_ERR(st->clkin),
630 "failed to get the input clock\n");
631 else if (!st->clkin)
632 return 0;
634 ret = clk_prepare_enable(st->clkin);
635 if (ret)
636 return ret;
638 ret = devm_add_action_or_reset(&spi->dev, admv8818_clk_disable, st);
639 if (ret)
640 return ret;
642 st->nb.notifier_call = admv8818_freq_change;
643 ret = clk_notifier_register(st->clkin, &st->nb);
644 if (ret < 0)
645 return ret;
647 return devm_add_action_or_reset(&spi->dev, admv8818_clk_notifier_unreg, st);
650 static int admv8818_probe(struct spi_device *spi)
652 struct iio_dev *indio_dev;
653 struct regmap *regmap;
654 struct admv8818_state *st;
655 int ret;
657 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
658 if (!indio_dev)
659 return -ENOMEM;
661 regmap = devm_regmap_init_spi(spi, &admv8818_regmap_config);
662 if (IS_ERR(regmap))
663 return PTR_ERR(regmap);
665 st = iio_priv(indio_dev);
666 st->regmap = regmap;
668 indio_dev->info = &admv8818_info;
669 indio_dev->name = "admv8818";
670 indio_dev->channels = admv8818_channels;
671 indio_dev->num_channels = ARRAY_SIZE(admv8818_channels);
673 st->spi = spi;
675 ret = admv8818_clk_setup(st);
676 if (ret)
677 return ret;
679 mutex_init(&st->lock);
681 ret = admv8818_init(st);
682 if (ret)
683 return ret;
685 return devm_iio_device_register(&spi->dev, indio_dev);
688 static const struct spi_device_id admv8818_id[] = {
689 { "admv8818", 0 },
692 MODULE_DEVICE_TABLE(spi, admv8818_id);
694 static const struct of_device_id admv8818_of_match[] = {
695 { .compatible = "adi,admv8818" },
698 MODULE_DEVICE_TABLE(of, admv8818_of_match);
700 static struct spi_driver admv8818_driver = {
701 .driver = {
702 .name = "admv8818",
703 .of_match_table = admv8818_of_match,
705 .probe = admv8818_probe,
706 .id_table = admv8818_id,
708 module_spi_driver(admv8818_driver);
710 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
711 MODULE_DESCRIPTION("Analog Devices ADMV8818");
712 MODULE_LICENSE("GPL v2");