forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/iio/humidity/dht11.c
....@@ -1,17 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * DHT11/DHT22 bit banging GPIO driver
34 *
45 * Copyright (c) Harald Geyer <harald@ccbib.org>
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
156 */
167
178 #include <linux/err.h>
....@@ -31,8 +22,7 @@
3122 #include <linux/completion.h>
3223 #include <linux/mutex.h>
3324 #include <linux/delay.h>
34
-#include <linux/gpio.h>
35
-#include <linux/of_gpio.h>
25
+#include <linux/gpio/consumer.h>
3626 #include <linux/timekeeping.h>
3727
3828 #include <linux/iio/iio.h>
....@@ -81,7 +71,7 @@
8171 struct dht11 {
8272 struct device *dev;
8373
84
- int gpio;
74
+ struct gpio_desc *gpiod;
8575 int irq;
8676
8777 struct completion completion;
....@@ -158,7 +148,7 @@
158148 return -EIO;
159149 }
160150
161
- dht11->timestamp = ktime_get_boot_ns();
151
+ dht11->timestamp = ktime_get_boottime_ns();
162152 if (hum_int < 4) { /* DHT22: 100000 = (3*256+232)*100 */
163153 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
164154 ((temp_int & 0x80) ? -100 : 100);
....@@ -184,11 +174,10 @@
184174 struct iio_dev *iio = data;
185175 struct dht11 *dht11 = iio_priv(iio);
186176
187
- /* TODO: Consider making the handler safe for IRQ sharing */
188177 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
189
- dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
178
+ dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
190179 dht11->edges[dht11->num_edges++].value =
191
- gpio_get_value(dht11->gpio);
180
+ gpiod_get_value(dht11->gpiod);
192181
193182 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
194183 complete(&dht11->completion);
....@@ -205,7 +194,7 @@
205194 int ret, timeres, offset;
206195
207196 mutex_lock(&dht11->lock);
208
- if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
197
+ if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
209198 timeres = ktime_get_resolution_ns();
210199 dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
211200 if (timeres > DHT11_MIN_TIMERES) {
....@@ -226,12 +215,12 @@
226215 reinit_completion(&dht11->completion);
227216
228217 dht11->num_edges = 0;
229
- ret = gpio_direction_output(dht11->gpio, 0);
218
+ ret = gpiod_direction_output(dht11->gpiod, 0);
230219 if (ret)
231220 goto err;
232221 usleep_range(DHT11_START_TRANSMISSION_MIN,
233222 DHT11_START_TRANSMISSION_MAX);
234
- ret = gpio_direction_input(dht11->gpio);
223
+ ret = gpiod_direction_input(dht11->gpiod);
235224 if (ret)
236225 goto err;
237226
....@@ -303,10 +292,8 @@
303292 static int dht11_probe(struct platform_device *pdev)
304293 {
305294 struct device *dev = &pdev->dev;
306
- struct device_node *node = dev->of_node;
307295 struct dht11 *dht11;
308296 struct iio_dev *iio;
309
- int ret;
310297
311298 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
312299 if (!iio) {
....@@ -316,22 +303,17 @@
316303
317304 dht11 = iio_priv(iio);
318305 dht11->dev = dev;
306
+ dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
307
+ if (IS_ERR(dht11->gpiod))
308
+ return PTR_ERR(dht11->gpiod);
319309
320
- ret = of_get_gpio(node, 0);
321
- if (ret < 0)
322
- return ret;
323
- dht11->gpio = ret;
324
- ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
325
- if (ret)
326
- return ret;
327
-
328
- dht11->irq = gpio_to_irq(dht11->gpio);
310
+ dht11->irq = gpiod_to_irq(dht11->gpiod);
329311 if (dht11->irq < 0) {
330
- dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
312
+ dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
331313 return -EINVAL;
332314 }
333315
334
- dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
316
+ dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
335317 dht11->num_edges = -1;
336318
337319 platform_set_drvdata(pdev, iio);
....@@ -339,7 +321,6 @@
339321 init_completion(&dht11->completion);
340322 mutex_init(&dht11->lock);
341323 iio->name = pdev->name;
342
- iio->dev.parent = &pdev->dev;
343324 iio->info = &dht11_iio_info;
344325 iio->modes = INDIO_DIRECT_MODE;
345326 iio->channels = dht11_chan_spec;