forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/drivers/iio/adc/ltc2497.c
....@@ -1,34 +1,24 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC
34 *
45 * Copyright (C) 2017 Analog Devices Inc.
56 *
6
- * Licensed under the GPL-2.
7
- *
87 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf
98 */
109
11
-#include <linux/delay.h>
1210 #include <linux/i2c.h>
1311 #include <linux/iio/iio.h>
1412 #include <linux/iio/driver.h>
15
-#include <linux/iio/sysfs.h>
1613 #include <linux/module.h>
17
-#include <linux/of.h>
18
-#include <linux/regulator/consumer.h>
14
+#include <linux/mod_devicetable.h>
1915
20
-#define LTC2497_ENABLE 0xA0
21
-#define LTC2497_SGL BIT(4)
22
-#define LTC2497_DIFF 0
23
-#define LTC2497_SIGN BIT(3)
24
-#define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE
25
-#define LTC2497_CONVERSION_TIME_MS 150ULL
16
+#include "ltc2497.h"
2617
27
-struct ltc2497_st {
18
+struct ltc2497_driverdata {
19
+ /* this must be the first member */
20
+ struct ltc2497core_driverdata common_ddata;
2821 struct i2c_client *client;
29
- struct regulator *ref;
30
- ktime_t time_prev;
31
- u8 addr_prev;
3222 /*
3323 * DMA (thus cache coherency maintenance) requires the
3424 * transfer buffers to live in their own cache lines.
....@@ -36,232 +26,72 @@
3626 __be32 buf ____cacheline_aligned;
3727 };
3828
39
-static int ltc2497_wait_conv(struct ltc2497_st *st)
29
+static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
30
+ u8 address, int *val)
4031 {
41
- s64 time_elapsed;
42
-
43
- time_elapsed = ktime_ms_delta(ktime_get(), st->time_prev);
44
-
45
- if (time_elapsed < LTC2497_CONVERSION_TIME_MS) {
46
- /* delay if conversion time not passed
47
- * since last read or write
48
- */
49
- if (msleep_interruptible(
50
- LTC2497_CONVERSION_TIME_MS - time_elapsed))
51
- return -ERESTARTSYS;
52
-
53
- return 0;
54
- }
55
-
56
- if (time_elapsed - LTC2497_CONVERSION_TIME_MS <= 0) {
57
- /* We're in automatic mode -
58
- * so the last reading is stil not outdated
59
- */
60
- return 0;
61
- }
62
-
63
- return 1;
64
-}
65
-
66
-static int ltc2497_read(struct ltc2497_st *st, u8 address, int *val)
67
-{
68
- struct i2c_client *client = st->client;
32
+ struct ltc2497_driverdata *st =
33
+ container_of(ddata, struct ltc2497_driverdata, common_ddata);
6934 int ret;
7035
71
- ret = ltc2497_wait_conv(st);
72
- if (ret < 0)
73
- return ret;
74
-
75
- if (ret || st->addr_prev != address) {
76
- ret = i2c_smbus_write_byte(st->client,
77
- LTC2497_ENABLE | address);
78
- if (ret < 0)
36
+ if (val) {
37
+ ret = i2c_master_recv(st->client, (char *)&st->buf, 3);
38
+ if (ret < 0) {
39
+ dev_err(&st->client->dev, "i2c_master_recv failed\n");
7940 return ret;
80
- st->addr_prev = address;
81
- if (msleep_interruptible(LTC2497_CONVERSION_TIME_MS))
82
- return -ERESTARTSYS;
83
- }
84
- ret = i2c_master_recv(client, (char *)&st->buf, 3);
85
- if (ret < 0) {
86
- dev_err(&client->dev, "i2c_master_recv failed\n");
87
- return ret;
88
- }
89
- st->time_prev = ktime_get();
41
+ }
9042
91
- /* convert and shift the result,
92
- * and finally convert from offset binary to signed integer
93
- */
94
- *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
43
+ *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
9544
45
+ /*
46
+ * The part started a new conversion at the end of the above i2c
47
+ * transfer, so if the address didn't change since the last call
48
+ * everything is fine and we can return early.
49
+ * If not (which should only happen when some sort of bulk
50
+ * conversion is implemented) we have to program the new
51
+ * address. Note that this probably fails as the conversion that
52
+ * was triggered above is like not complete yet and the two
53
+ * operations have to be done in a single transfer.
54
+ */
55
+ if (ddata->addr_prev == address)
56
+ return 0;
57
+ }
58
+
59
+ ret = i2c_smbus_write_byte(st->client,
60
+ LTC2497_ENABLE | address);
61
+ if (ret)
62
+ dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
63
+ ERR_PTR(ret));
9664 return ret;
9765 }
98
-
99
-static int ltc2497_read_raw(struct iio_dev *indio_dev,
100
- struct iio_chan_spec const *chan,
101
- int *val, int *val2, long mask)
102
-{
103
- struct ltc2497_st *st = iio_priv(indio_dev);
104
- int ret;
105
-
106
- switch (mask) {
107
- case IIO_CHAN_INFO_RAW:
108
- mutex_lock(&indio_dev->mlock);
109
- ret = ltc2497_read(st, chan->address, val);
110
- mutex_unlock(&indio_dev->mlock);
111
- if (ret < 0)
112
- return ret;
113
-
114
- return IIO_VAL_INT;
115
-
116
- case IIO_CHAN_INFO_SCALE:
117
- ret = regulator_get_voltage(st->ref);
118
- if (ret < 0)
119
- return ret;
120
-
121
- *val = ret / 1000;
122
- *val2 = 17;
123
-
124
- return IIO_VAL_FRACTIONAL_LOG2;
125
-
126
- default:
127
- return -EINVAL;
128
- }
129
-}
130
-
131
-#define LTC2497_CHAN(_chan, _addr, _ds_name) { \
132
- .type = IIO_VOLTAGE, \
133
- .indexed = 1, \
134
- .channel = (_chan), \
135
- .address = (_addr | (_chan / 2) | ((_chan & 1) ? LTC2497_SIGN : 0)), \
136
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
137
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
138
- .datasheet_name = (_ds_name), \
139
-}
140
-
141
-#define LTC2497_CHAN_DIFF(_chan, _addr) { \
142
- .type = IIO_VOLTAGE, \
143
- .indexed = 1, \
144
- .channel = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 1 : 0), \
145
- .channel2 = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 0 : 1),\
146
- .address = (_addr | _chan), \
147
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
148
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
149
- .differential = 1, \
150
-}
151
-
152
-static const struct iio_chan_spec ltc2497_channel[] = {
153
- LTC2497_CHAN(0, LTC2497_SGL, "CH0"),
154
- LTC2497_CHAN(1, LTC2497_SGL, "CH1"),
155
- LTC2497_CHAN(2, LTC2497_SGL, "CH2"),
156
- LTC2497_CHAN(3, LTC2497_SGL, "CH3"),
157
- LTC2497_CHAN(4, LTC2497_SGL, "CH4"),
158
- LTC2497_CHAN(5, LTC2497_SGL, "CH5"),
159
- LTC2497_CHAN(6, LTC2497_SGL, "CH6"),
160
- LTC2497_CHAN(7, LTC2497_SGL, "CH7"),
161
- LTC2497_CHAN(8, LTC2497_SGL, "CH8"),
162
- LTC2497_CHAN(9, LTC2497_SGL, "CH9"),
163
- LTC2497_CHAN(10, LTC2497_SGL, "CH10"),
164
- LTC2497_CHAN(11, LTC2497_SGL, "CH11"),
165
- LTC2497_CHAN(12, LTC2497_SGL, "CH12"),
166
- LTC2497_CHAN(13, LTC2497_SGL, "CH13"),
167
- LTC2497_CHAN(14, LTC2497_SGL, "CH14"),
168
- LTC2497_CHAN(15, LTC2497_SGL, "CH15"),
169
- LTC2497_CHAN_DIFF(0, LTC2497_DIFF),
170
- LTC2497_CHAN_DIFF(1, LTC2497_DIFF),
171
- LTC2497_CHAN_DIFF(2, LTC2497_DIFF),
172
- LTC2497_CHAN_DIFF(3, LTC2497_DIFF),
173
- LTC2497_CHAN_DIFF(4, LTC2497_DIFF),
174
- LTC2497_CHAN_DIFF(5, LTC2497_DIFF),
175
- LTC2497_CHAN_DIFF(6, LTC2497_DIFF),
176
- LTC2497_CHAN_DIFF(7, LTC2497_DIFF),
177
- LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN),
178
- LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN),
179
- LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN),
180
- LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN),
181
- LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN),
182
- LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN),
183
- LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN),
184
- LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN),
185
-};
186
-
187
-static const struct iio_info ltc2497_info = {
188
- .read_raw = ltc2497_read_raw,
189
-};
19066
19167 static int ltc2497_probe(struct i2c_client *client,
19268 const struct i2c_device_id *id)
19369 {
19470 struct iio_dev *indio_dev;
195
- struct ltc2497_st *st;
196
- struct iio_map *plat_data;
197
- int ret;
71
+ struct ltc2497_driverdata *st;
72
+ struct device *dev = &client->dev;
19873
19974 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
20075 I2C_FUNC_SMBUS_WRITE_BYTE))
20176 return -EOPNOTSUPP;
20277
203
- indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
78
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
20479 if (!indio_dev)
20580 return -ENOMEM;
20681
20782 st = iio_priv(indio_dev);
20883 i2c_set_clientdata(client, indio_dev);
20984 st->client = client;
85
+ st->common_ddata.result_and_measure = ltc2497_result_and_measure;
21086
211
- indio_dev->dev.parent = &client->dev;
212
- indio_dev->name = id->name;
213
- indio_dev->info = &ltc2497_info;
214
- indio_dev->modes = INDIO_DIRECT_MODE;
215
- indio_dev->channels = ltc2497_channel;
216
- indio_dev->num_channels = ARRAY_SIZE(ltc2497_channel);
217
-
218
- st->ref = devm_regulator_get(&client->dev, "vref");
219
- if (IS_ERR(st->ref))
220
- return PTR_ERR(st->ref);
221
-
222
- ret = regulator_enable(st->ref);
223
- if (ret < 0)
224
- return ret;
225
-
226
- if (client->dev.platform_data) {
227
- plat_data = ((struct iio_map *)client->dev.platform_data);
228
- ret = iio_map_array_register(indio_dev, plat_data);
229
- if (ret) {
230
- dev_err(&indio_dev->dev, "iio map err: %d\n", ret);
231
- goto err_regulator_disable;
232
- }
233
- }
234
-
235
- ret = i2c_smbus_write_byte(st->client, LTC2497_CONFIG_DEFAULT);
236
- if (ret < 0)
237
- goto err_array_unregister;
238
-
239
- st->addr_prev = LTC2497_CONFIG_DEFAULT;
240
- st->time_prev = ktime_get();
241
-
242
- ret = iio_device_register(indio_dev);
243
- if (ret < 0)
244
- goto err_array_unregister;
245
-
246
- return 0;
247
-
248
-err_array_unregister:
249
- iio_map_array_unregister(indio_dev);
250
-
251
-err_regulator_disable:
252
- regulator_disable(st->ref);
253
-
254
- return ret;
87
+ return ltc2497core_probe(dev, indio_dev);
25588 }
25689
25790 static int ltc2497_remove(struct i2c_client *client)
25891 {
25992 struct iio_dev *indio_dev = i2c_get_clientdata(client);
260
- struct ltc2497_st *st = iio_priv(indio_dev);
26193
262
- iio_map_array_unregister(indio_dev);
263
- iio_device_unregister(indio_dev);
264
- regulator_disable(st->ref);
94
+ ltc2497core_remove(indio_dev);
26595
26696 return 0;
26797 }
....@@ -281,7 +111,7 @@
281111 static struct i2c_driver ltc2497_driver = {
282112 .driver = {
283113 .name = "ltc2497",
284
- .of_match_table = of_match_ptr(ltc2497_of_match),
114
+ .of_match_table = ltc2497_of_match,
285115 },
286116 .probe = ltc2497_probe,
287117 .remove = ltc2497_remove,