forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/staging/iio/cdc/ad7150.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /*
23 * AD7150 capacitive sensor driver supporting AD7150/1/6
34 *
45 * Copyright 2010-2011 Analog Devices Inc.
5
- *
6
- * Licensed under the GPL-2 or later.
76 */
87
98 #include <linux/bitfield.h>
....@@ -46,6 +45,9 @@
4645 #define AD7150_SN1 21
4746 #define AD7150_SN0 22
4847 #define AD7150_ID 23
48
+
49
+/* AD7150 masks */
50
+#define AD7150_THRESHTYPE_MSK GENMASK(6, 5)
4951
5052 /**
5153 * struct ad7150_chip_info - instance specific chip data
....@@ -103,18 +105,19 @@
103105 {
104106 int ret;
105107 struct ad7150_chip_info *chip = iio_priv(indio_dev);
108
+ int channel = chan->channel;
106109
107110 switch (mask) {
108111 case IIO_CHAN_INFO_RAW:
109112 ret = i2c_smbus_read_word_data(chip->client,
110
- ad7150_addresses[chan->channel][0]);
113
+ ad7150_addresses[channel][0]);
111114 if (ret < 0)
112115 return ret;
113116 *val = swab16(ret);
114117 return IIO_VAL_INT;
115118 case IIO_CHAN_INFO_AVERAGE_RAW:
116119 ret = i2c_smbus_read_word_data(chip->client,
117
- ad7150_addresses[chan->channel][1]);
120
+ ad7150_addresses[channel][1]);
118121 if (ret < 0)
119122 return ret;
120123 *val = swab16(ret);
....@@ -138,7 +141,7 @@
138141 if (ret < 0)
139142 return ret;
140143
141
- threshtype = (ret >> 5) & 0x03;
144
+ threshtype = FIELD_GET(AD7150_THRESHTYPE_MSK, ret);
142145
143146 /*check if threshold mode is fixed or adaptive*/
144147 thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
....@@ -162,7 +165,8 @@
162165 return -EINVAL;
163166 }
164167
165
-/* lock should be held */
168
+/* state_lock should be held to ensure consistent state*/
169
+
166170 static int ad7150_write_event_params(struct iio_dev *indio_dev,
167171 unsigned int chan,
168172 enum iio_event_type type,
....@@ -185,8 +189,8 @@
185189 case IIO_EV_TYPE_THRESH:
186190 value = chip->threshold[rising][chan];
187191 return i2c_smbus_write_word_data(chip->client,
188
- ad7150_addresses[chan][3],
189
- swab16(value));
192
+ ad7150_addresses[chan][3],
193
+ swab16(value));
190194 case IIO_EV_TYPE_MAG_ADAPTIVE:
191195 sens = chip->mag_sensitivity[rising][chan];
192196 timeout = chip->mag_timeout[rising][chan];
....@@ -201,16 +205,11 @@
201205 ret = i2c_smbus_write_byte_data(chip->client,
202206 ad7150_addresses[chan][4],
203207 sens);
204
- if (ret < 0)
208
+ if (ret)
205209 return ret;
206
-
207
- ret = i2c_smbus_write_byte_data(chip->client,
210
+ return i2c_smbus_write_byte_data(chip->client,
208211 ad7150_addresses[chan][5],
209212 timeout);
210
- if (ret < 0)
211
- return ret;
212
-
213
- return 0;
214213 }
215214
216215 static int ad7150_write_event_config(struct iio_dev *indio_dev,
....@@ -353,8 +352,8 @@
353352
354353 /* use the event code for consistency reasons */
355354 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
356
- int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
357
- == IIO_EV_DIR_RISING);
355
+ int rising = (IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
356
+ == IIO_EV_DIR_RISING) ? 1 : 0;
358357
359358 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
360359 case IIO_EV_TYPE_MAG_ADAPTIVE:
....@@ -468,29 +467,20 @@
468467 },
469468 };
470469
471
-static const struct iio_chan_spec ad7150_channels[] = {
472
- {
473
- .type = IIO_CAPACITANCE,
474
- .indexed = 1,
475
- .channel = 0,
476
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
477
- BIT(IIO_CHAN_INFO_AVERAGE_RAW),
478
- .event_spec = ad7150_events,
479
- .num_event_specs = ARRAY_SIZE(ad7150_events),
480
- }, {
481
- .type = IIO_CAPACITANCE,
482
- .indexed = 1,
483
- .channel = 1,
484
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
485
- BIT(IIO_CHAN_INFO_AVERAGE_RAW),
486
- .event_spec = ad7150_events,
487
- .num_event_specs = ARRAY_SIZE(ad7150_events),
488
- },
489
-};
470
+#define AD7150_CAPACITANCE_CHAN(_chan) { \
471
+ .type = IIO_CAPACITANCE, \
472
+ .indexed = 1, \
473
+ .channel = _chan, \
474
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
475
+ BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
476
+ .event_spec = ad7150_events, \
477
+ .num_event_specs = ARRAY_SIZE(ad7150_events), \
478
+ }
490479
491
-/*
492
- * threshold events
493
- */
480
+static const struct iio_chan_spec ad7150_channels[] = {
481
+ AD7150_CAPACITANCE_CHAN(0),
482
+ AD7150_CAPACITANCE_CHAN(1)
483
+};
494484
495485 static irqreturn_t ad7150_event_handler(int irq, void *private)
496486 {
....@@ -580,10 +570,6 @@
580570 .write_event_value = &ad7150_write_event_value,
581571 };
582572
583
-/*
584
- * device probe and remove
585
- */
586
-
587573 static int ad7150_probe(struct i2c_client *client,
588574 const struct i2c_device_id *id)
589575 {
....@@ -604,8 +590,6 @@
604590 indio_dev->name = id->name;
605591 indio_dev->channels = ad7150_channels;
606592 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
607
- /* Establish that the iio_dev is a child of the i2c device */
608
- indio_dev->dev.parent = &client->dev;
609593
610594 indio_dev->info = &ad7150_info;
611595