hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/iio/dac/ad5758.c
....@@ -1,4 +1,4 @@
1
-// SPDX-License-Identifier: GPL-2.0+
1
+// SPDX-License-Identifier: GPL-2.0
22 /*
33 * AD5758 Digital to analog converters driver
44 *
....@@ -11,7 +11,10 @@
1111 #include <linux/kernel.h>
1212 #include <linux/module.h>
1313 #include <linux/property.h>
14
+#include <linux/of.h>
15
+#include <linux/of_device.h>
1416 #include <linux/spi/spi.h>
17
+#include <linux/gpio/consumer.h>
1518
1619 #include <linux/iio/iio.h>
1720 #include <linux/iio/sysfs.h>
....@@ -71,8 +74,6 @@
7174 #define AD5758_DCDC_CONFIG1_DCDC_VPROG_MODE(x) (((x) & 0x1F) << 0)
7275 #define AD5758_DCDC_CONFIG1_DCDC_MODE_MSK GENMASK(6, 5)
7376 #define AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(x) (((x) & 0x3) << 5)
74
-#define AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK BIT(7)
75
-#define AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(x) (((x) & 0x1) << 7)
7677
7778 /* AD5758_DCDC_CONFIG2 */
7879 #define AD5758_DCDC_CONFIG2_ILIMIT_MSK GENMASK(3, 1)
....@@ -83,21 +84,13 @@
8384 /* AD5758_DIGITAL_DIAG_RESULTS */
8485 #define AD5758_CAL_MEM_UNREFRESHED_MSK BIT(15)
8586
87
+/* AD5758_ADC_CONFIG */
88
+#define AD5758_ADC_CONFIG_PPC_BUF_EN(x) (((x) & 0x1) << 11)
89
+#define AD5758_ADC_CONFIG_PPC_BUF_MSK BIT(11)
90
+
8691 #define AD5758_WR_FLAG_MSK(x) (0x80 | ((x) & 0x1F))
8792
8893 #define AD5758_FULL_SCALE_MICRO 65535000000ULL
89
-
90
-/**
91
- * struct ad5758_state - driver instance specific data
92
- * @spi: spi_device
93
- * @lock: mutex lock
94
- * @out_range: struct which stores the output range
95
- * @dc_dc_mode: variable which stores the mode of operation
96
- * @dc_dc_ilim: variable which stores the dc-to-dc converter current limit
97
- * @slew_time: variable which stores the target slew time
98
- * @pwr_down: variable which contains whether a channel is powered down or not
99
- * @data: spi transfer buffers
100
- */
10194
10295 struct ad5758_range {
10396 int reg;
....@@ -105,9 +98,22 @@
10598 int max;
10699 };
107100
101
+/**
102
+ * struct ad5758_state - driver instance specific data
103
+ * @spi: spi_device
104
+ * @lock: mutex lock
105
+ * @gpio_reset: gpio descriptor for the reset line
106
+ * @out_range: struct which stores the output range
107
+ * @dc_dc_mode: variable which stores the mode of operation
108
+ * @dc_dc_ilim: variable which stores the dc-to-dc converter current limit
109
+ * @slew_time: variable which stores the target slew time
110
+ * @pwr_down: variable which contains whether a channel is powered down or not
111
+ * @d32: spi transfer buffers
112
+ */
108113 struct ad5758_state {
109114 struct spi_device *spi;
110115 struct mutex lock;
116
+ struct gpio_desc *gpio_reset;
111117 struct ad5758_range out_range;
112118 unsigned int dc_dc_mode;
113119 unsigned int dc_dc_ilim;
....@@ -116,7 +122,7 @@
116122 __be32 d32[3];
117123 };
118124
119
-/**
125
+/*
120126 * Output ranges corresponding to bits [3:0] from DAC_CONFIG register
121127 * 0000: 0 V to 5 V voltage range
122128 * 0001: 0 V to 10 V voltage range
....@@ -313,6 +319,18 @@
313319 {
314320 int ret;
315321
322
+ /*
323
+ * The ENABLE_PPC_BUFFERS bit must be set prior to enabling PPC current
324
+ * mode.
325
+ */
326
+ if (mode == AD5758_DCDC_MODE_PPC_CURRENT) {
327
+ ret = ad5758_spi_write_mask(st, AD5758_ADC_CONFIG,
328
+ AD5758_ADC_CONFIG_PPC_BUF_MSK,
329
+ AD5758_ADC_CONFIG_PPC_BUF_EN(1));
330
+ if (ret < 0)
331
+ return ret;
332
+ }
333
+
316334 ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
317335 AD5758_DCDC_CONFIG1_DCDC_MODE_MSK,
318336 AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(mode));
....@@ -442,23 +460,6 @@
442460 AD5758_CAL_MEM_UNREFRESHED_MSK);
443461 }
444462
445
-static int ad5758_fault_prot_switch_en(struct ad5758_state *st, bool enable)
446
-{
447
- int ret;
448
-
449
- ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
450
- AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK,
451
- AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(enable));
452
- if (ret < 0)
453
- return ret;
454
- /*
455
- * Poll the BUSY_3WI bit in the DCDC_CONFIG2 register until it is 0.
456
- * This allows the 3-wire interface communication to complete.
457
- */
458
- return ad5758_wait_for_task_complete(st, AD5758_DCDC_CONFIG2,
459
- AD5758_DCDC_CONFIG2_BUSY_3WI_MSK);
460
-}
461
-
462463 static int ad5758_internal_buffers_en(struct ad5758_state *st, bool enable)
463464 {
464465 int ret;
....@@ -472,6 +473,21 @@
472473 /* Wait to allow time for the internal calibrations to complete */
473474 return ad5758_wait_for_task_complete(st, AD5758_DIGITAL_DIAG_RESULTS,
474475 AD5758_CAL_MEM_UNREFRESHED_MSK);
476
+}
477
+
478
+static int ad5758_reset(struct ad5758_state *st)
479
+{
480
+ if (st->gpio_reset) {
481
+ gpiod_set_value(st->gpio_reset, 0);
482
+ usleep_range(100, 1000);
483
+ gpiod_set_value(st->gpio_reset, 1);
484
+ usleep_range(100, 1000);
485
+
486
+ return 0;
487
+ } else {
488
+ /* Perform a software reset */
489
+ return ad5758_soft_reset(st);
490
+ }
475491 }
476492
477493 static int ad5758_reg_access(struct iio_dev *indio_dev,
....@@ -568,8 +584,8 @@
568584 {
569585 struct ad5758_state *st = iio_priv(indio_dev);
570586 bool pwr_down;
571
- unsigned int dcdc_config1_mode, dc_dc_mode, dac_config_mode, val;
572
- unsigned long int dcdc_config1_msk, dac_config_msk;
587
+ unsigned int dac_config_mode, val;
588
+ unsigned long int dac_config_msk;
573589 int ret;
574590
575591 ret = kstrtobool(buf, &pwr_down);
....@@ -577,24 +593,10 @@
577593 return ret;
578594
579595 mutex_lock(&st->lock);
580
- if (pwr_down) {
581
- dc_dc_mode = AD5758_DCDC_MODE_POWER_OFF;
596
+ if (pwr_down)
582597 val = 0;
583
- } else {
584
- dc_dc_mode = st->dc_dc_mode;
598
+ else
585599 val = 1;
586
- }
587
-
588
- dcdc_config1_mode = AD5758_DCDC_CONFIG1_DCDC_MODE_MODE(dc_dc_mode) |
589
- AD5758_DCDC_CONFIG1_PROT_SW_EN_MODE(val);
590
- dcdc_config1_msk = AD5758_DCDC_CONFIG1_DCDC_MODE_MSK |
591
- AD5758_DCDC_CONFIG1_PROT_SW_EN_MSK;
592
-
593
- ret = ad5758_spi_write_mask(st, AD5758_DCDC_CONFIG1,
594
- dcdc_config1_msk,
595
- dcdc_config1_mode);
596
- if (ret < 0)
597
- goto err_unlock;
598600
599601 dac_config_mode = AD5758_DAC_CONFIG_OUT_EN_MODE(val) |
600602 AD5758_DAC_CONFIG_INT_EN_MODE(val);
....@@ -768,13 +770,18 @@
768770 {
769771 int regval, ret;
770772
773
+ st->gpio_reset = devm_gpiod_get_optional(&st->spi->dev, "reset",
774
+ GPIOD_OUT_HIGH);
775
+ if (IS_ERR(st->gpio_reset))
776
+ return PTR_ERR(st->gpio_reset);
777
+
771778 /* Disable CRC checks */
772779 ret = ad5758_crc_disable(st);
773780 if (ret < 0)
774781 return ret;
775782
776
- /* Perform a software reset */
777
- ret = ad5758_soft_reset(st);
783
+ /* Perform a reset */
784
+ ret = ad5758_reset(st);
778785 if (ret < 0)
779786 return ret;
780787
....@@ -819,11 +826,6 @@
819826 return ret;
820827 }
821828
822
- /* Enable the VIOUT fault protection switch (FPS is closed) */
823
- ret = ad5758_fault_prot_switch_en(st, 1);
824
- if (ret < 0)
825
- return ret;
826
-
827829 /* Power up the DAC and internal (INT) amplifiers */
828830 ret = ad5758_internal_buffers_en(st, 1);
829831 if (ret < 0)
....@@ -852,7 +854,6 @@
852854
853855 mutex_init(&st->lock);
854856
855
- indio_dev->dev.parent = &spi->dev;
856857 indio_dev->name = spi_get_device_id(spi)->name;
857858 indio_dev->info = &ad5758_info;
858859 indio_dev->modes = INDIO_DIRECT_MODE;
....@@ -882,9 +883,16 @@
882883 };
883884 MODULE_DEVICE_TABLE(spi, ad5758_id);
884885
886
+static const struct of_device_id ad5758_of_match[] = {
887
+ { .compatible = "adi,ad5758" },
888
+ { },
889
+};
890
+MODULE_DEVICE_TABLE(of, ad5758_of_match);
891
+
885892 static struct spi_driver ad5758_driver = {
886893 .driver = {
887894 .name = KBUILD_MODNAME,
895
+ .of_match_table = ad5758_of_match,
888896 },
889897 .probe = ad5758_probe,
890898 .id_table = ad5758_id,