forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/iio/dac/ad7303.c
....@@ -1,19 +1,18 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * AD7303 Digital to analog converters driver
34 *
45 * Copyright 2013 Analog Devices Inc.
5
- *
6
- * Licensed under the GPL-2.
76 */
87
98 #include <linux/err.h>
109 #include <linux/module.h>
10
+#include <linux/mod_devicetable.h>
1111 #include <linux/kernel.h>
1212 #include <linux/spi/spi.h>
1313 #include <linux/slab.h>
1414 #include <linux/sysfs.h>
1515 #include <linux/regulator/consumer.h>
16
-#include <linux/of.h>
1716
1817 #include <linux/iio/iio.h>
1918 #include <linux/iio/sysfs.h>
....@@ -31,6 +30,9 @@
3130 * @spi: the device for this driver instance
3231 * @config: cached config register value
3332 * @dac_cache: current DAC raw value (chip does not support readback)
33
+ * @vdd_reg: reference to VDD regulator
34
+ * @vref_reg: reference to VREF regulator
35
+ * @lock: protect writes and cache updates
3436 * @data: spi transfer buffer
3537 */
3638
....@@ -42,6 +44,7 @@
4244 struct regulator *vdd_reg;
4345 struct regulator *vref_reg;
4446
47
+ struct mutex lock;
4548 /*
4649 * DMA (thus cache coherency maintenance) requires the
4750 * transfer buffers to live in their own cache lines.
....@@ -80,7 +83,7 @@
8083 if (ret)
8184 return ret;
8285
83
- mutex_lock(&indio_dev->mlock);
86
+ mutex_lock(&st->lock);
8487
8588 if (pwr_down)
8689 st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
....@@ -91,7 +94,7 @@
9194 * mode, so just write one of the DAC channels again */
9295 ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
9396
94
- mutex_unlock(&indio_dev->mlock);
97
+ mutex_unlock(&st->lock);
9598 return len;
9699 }
97100
....@@ -117,7 +120,9 @@
117120
118121 switch (info) {
119122 case IIO_CHAN_INFO_RAW:
123
+ mutex_lock(&st->lock);
120124 *val = st->dac_cache[chan->channel];
125
+ mutex_unlock(&st->lock);
121126 return IIO_VAL_INT;
122127 case IIO_CHAN_INFO_SCALE:
123128 vref_uv = ad7303_get_vref(st, chan);
....@@ -145,11 +150,11 @@
145150 if (val >= (1 << chan->scan_type.realbits) || val < 0)
146151 return -EINVAL;
147152
148
- mutex_lock(&indio_dev->mlock);
153
+ mutex_lock(&st->lock);
149154 ret = ad7303_write(st, chan->address, val);
150155 if (ret == 0)
151156 st->dac_cache[chan->channel] = val;
152
- mutex_unlock(&indio_dev->mlock);
157
+ mutex_unlock(&st->lock);
153158 break;
154159 default:
155160 ret = -EINVAL;
....@@ -200,7 +205,6 @@
200205 const struct spi_device_id *id = spi_get_device_id(spi);
201206 struct iio_dev *indio_dev;
202207 struct ad7303_state *st;
203
- bool ext_ref;
204208 int ret;
205209
206210 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
....@@ -212,6 +216,8 @@
212216
213217 st->spi = spi;
214218
219
+ mutex_init(&st->lock);
220
+
215221 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
216222 if (IS_ERR(st->vdd_reg))
217223 return PTR_ERR(st->vdd_reg);
....@@ -220,24 +226,15 @@
220226 if (ret)
221227 return ret;
222228
223
- if (spi->dev.of_node) {
224
- ext_ref = of_property_read_bool(spi->dev.of_node,
225
- "REF-supply");
226
- } else {
227
- struct ad7303_platform_data *pdata = spi->dev.platform_data;
228
- if (pdata && pdata->use_external_ref)
229
- ext_ref = true;
230
- else
231
- ext_ref = false;
229
+ st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF");
230
+ if (IS_ERR(st->vref_reg)) {
231
+ ret = PTR_ERR(st->vref_reg);
232
+ if (ret != -ENODEV)
233
+ goto err_disable_vdd_reg;
234
+ st->vref_reg = NULL;
232235 }
233236
234
- if (ext_ref) {
235
- st->vref_reg = devm_regulator_get(&spi->dev, "REF");
236
- if (IS_ERR(st->vref_reg)) {
237
- ret = PTR_ERR(st->vref_reg);
238
- goto err_disable_vdd_reg;
239
- }
240
-
237
+ if (st->vref_reg) {
241238 ret = regulator_enable(st->vref_reg);
242239 if (ret)
243240 goto err_disable_vdd_reg;
....@@ -245,7 +242,6 @@
245242 st->config |= AD7303_CFG_EXTERNAL_VREF;
246243 }
247244
248
- indio_dev->dev.parent = &spi->dev;
249245 indio_dev->name = id->name;
250246 indio_dev->info = &ad7303_info;
251247 indio_dev->modes = INDIO_DIRECT_MODE;
....@@ -295,7 +291,7 @@
295291 static struct spi_driver ad7303_driver = {
296292 .driver = {
297293 .name = "ad7303",
298
- .of_match_table = of_match_ptr(ad7303_spi_of_match),
294
+ .of_match_table = ad7303_spi_of_match,
299295 },
300296 .probe = ad7303_probe,
301297 .remove = ad7303_remove,