forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/iio/dac/ad5446.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * AD5446 SPI DAC driver
34 *
45 * Copyright 2010 Analog Devices Inc.
5
- *
6
- * Licensed under the GPL-2 or later.
76 */
87
98 #include <linux/interrupt.h>
....@@ -18,9 +17,12 @@
1817 #include <linux/regulator/consumer.h>
1918 #include <linux/err.h>
2019 #include <linux/module.h>
20
+#include <linux/mod_devicetable.h>
2121
2222 #include <linux/iio/iio.h>
2323 #include <linux/iio/sysfs.h>
24
+
25
+#include <asm/unaligned.h>
2426
2527 #define MODE_PWRDWN_1k 0x1
2628 #define MODE_PWRDWN_100k 0x2
....@@ -28,10 +30,14 @@
2830
2931 /**
3032 * struct ad5446_state - driver instance specific data
31
- * @spi: spi_device
33
+ * @dev: this device
3234 * @chip_info: chip model specific constants, available modes etc
3335 * @reg: supply regulator
3436 * @vref_mv: actual reference voltage used
37
+ * @cached_val: store/retrieve values during power down
38
+ * @pwr_down_mode: power down mode (1k, 100k or tristate)
39
+ * @pwr_down: true if the device is in power down
40
+ * @lock: lock to protect the data buffer during write ops
3541 */
3642
3743 struct ad5446_state {
....@@ -42,6 +48,7 @@
4248 unsigned cached_val;
4349 unsigned pwr_down_mode;
4450 unsigned pwr_down;
51
+ struct mutex lock;
4552 };
4653
4754 /**
....@@ -111,7 +118,7 @@
111118 if (ret)
112119 return ret;
113120
114
- mutex_lock(&indio_dev->mlock);
121
+ mutex_lock(&st->lock);
115122 st->pwr_down = powerdown;
116123
117124 if (st->pwr_down) {
....@@ -122,7 +129,7 @@
122129 }
123130
124131 ret = st->chip_info->write(st, val);
125
- mutex_unlock(&indio_dev->mlock);
132
+ mutex_unlock(&st->lock);
126133
127134 return ret ? ret : len;
128135 }
....@@ -171,7 +178,7 @@
171178
172179 switch (m) {
173180 case IIO_CHAN_INFO_RAW:
174
- *val = st->cached_val;
181
+ *val = st->cached_val >> chan->scan_type.shift;
175182 return IIO_VAL_INT;
176183 case IIO_CHAN_INFO_SCALE:
177184 *val = st->vref_mv;
....@@ -196,11 +203,11 @@
196203 return -EINVAL;
197204
198205 val <<= chan->scan_type.shift;
199
- mutex_lock(&indio_dev->mlock);
206
+ mutex_lock(&st->lock);
200207 st->cached_val = val;
201208 if (!st->pwr_down)
202209 ret = st->chip_info->write(st, val);
203
- mutex_unlock(&indio_dev->mlock);
210
+ mutex_unlock(&st->lock);
204211 break;
205212 default:
206213 ret = -EINVAL;
....@@ -247,13 +254,13 @@
247254 st->reg = reg;
248255 st->dev = dev;
249256
250
- /* Establish that the iio_dev is a child of the device */
251
- indio_dev->dev.parent = dev;
252257 indio_dev->name = name;
253258 indio_dev->info = &ad5446_info;
254259 indio_dev->modes = INDIO_DIRECT_MODE;
255260 indio_dev->channels = &st->chip_info->channel;
256261 indio_dev->num_channels = 1;
262
+
263
+ mutex_init(&st->lock);
257264
258265 st->pwr_down_mode = MODE_PWRDWN_1k;
259266
....@@ -303,14 +310,12 @@
303310 struct spi_device *spi = to_spi_device(st->dev);
304311 uint8_t data[3];
305312
306
- data[0] = (val >> 16) & 0xFF;
307
- data[1] = (val >> 8) & 0xFF;
308
- data[2] = val & 0xFF;
313
+ put_unaligned_be24(val, &data[0]);
309314
310315 return spi_write(spi, data, sizeof(data));
311316 }
312317
313
-/**
318
+/*
314319 * ad5446_supported_spi_device_ids:
315320 * The AD5620/40/60 parts are available in different fixed internal reference
316321 * voltage options. The actual part numbers may look differently
....@@ -474,13 +479,11 @@
474479 };
475480 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
476481
477
-#ifdef CONFIG_OF
478482 static const struct of_device_id ad5446_of_ids[] = {
479483 { .compatible = "ti,dac7512" },
480484 { }
481485 };
482486 MODULE_DEVICE_TABLE(of, ad5446_of_ids);
483
-#endif
484487
485488 static int ad5446_spi_probe(struct spi_device *spi)
486489 {
....@@ -498,7 +501,7 @@
498501 static struct spi_driver ad5446_spi_driver = {
499502 .driver = {
500503 .name = "ad5446",
501
- .of_match_table = of_match_ptr(ad5446_of_ids),
504
+ .of_match_table = ad5446_of_ids,
502505 },
503506 .probe = ad5446_spi_probe,
504507 .remove = ad5446_spi_remove,
....@@ -539,7 +542,7 @@
539542 return 0;
540543 }
541544
542
-/**
545
+/*
543546 * ad5446_supported_i2c_device_ids:
544547 * The AD5620/40/60 parts are available in different fixed internal reference
545548 * voltage options. The actual part numbers may look differently
....@@ -641,6 +644,6 @@
641644 }
642645 module_exit(ad5446_exit);
643646
644
-MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
647
+MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
645648 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
646649 MODULE_LICENSE("GPL v2");