forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/iio/adc/rockchip_saradc.c
....@@ -1,19 +1,11 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Rockchip Successive Approximation Register (SAR) A/D Converter
34 * Copyright (C) 2014 ROCKCHIP, Inc.
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License as published by
7
- * the Free Software Foundation; either version 2 of the License, or
8
- * (at your option) any later version.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
145 */
156
167 #include <linux/module.h>
8
+#include <linux/mutex.h>
179 #include <linux/platform_device.h>
1810 #include <linux/interrupt.h>
1911 #include <linux/io.h>
....@@ -77,11 +69,14 @@
7769 struct clk *clk;
7870 struct completion completion;
7971 struct regulator *vref;
72
+ /* lock to protect against multiple access to the device */
73
+ struct mutex lock;
8074 int uv_vref;
8175 struct reset_control *reset;
8276 const struct rockchip_saradc_data *data;
8377 u16 last_val;
8478 const struct iio_chan_spec *last_chan;
79
+ struct notifier_block nb;
8580 bool suspended;
8681 #ifdef CONFIG_ROCKCHIP_SARADC_TEST_CHN
8782 bool test;
....@@ -197,22 +192,22 @@
197192 #endif
198193 switch (mask) {
199194 case IIO_CHAN_INFO_RAW:
200
- mutex_lock(&indio_dev->mlock);
195
+ mutex_lock(&info->lock);
201196
202197 if (info->suspended) {
203
- mutex_unlock(&indio_dev->mlock);
198
+ mutex_unlock(&info->lock);
204199 return -EBUSY;
205200 }
206201
207202 ret = rockchip_saradc_conversion(info, chan);
208203 if (ret) {
209204 rockchip_saradc_power_down(info);
210
- mutex_unlock(&indio_dev->mlock);
205
+ mutex_unlock(&info->lock);
211206 return ret;
212207 }
213208
214209 *val = info->last_val;
215
- mutex_unlock(&indio_dev->mlock);
210
+ mutex_unlock(&info->lock);
216211 return IIO_VAL_INT;
217212 case IIO_CHAN_INFO_SCALE:
218213 /* It is a dummy regulator */
....@@ -336,6 +331,25 @@
336331 .read = rockchip_saradc_read_v2,
337332 };
338333
334
+static const struct iio_chan_spec rockchip_rk3562_saradc_iio_channels[] = {
335
+ SARADC_CHANNEL(0, "adc0", 10),
336
+ SARADC_CHANNEL(1, "adc1", 10),
337
+ SARADC_CHANNEL(2, "adc2", 10),
338
+ SARADC_CHANNEL(3, "adc3", 10),
339
+ SARADC_CHANNEL(4, "adc4", 10),
340
+ SARADC_CHANNEL(5, "adc5", 10),
341
+ SARADC_CHANNEL(6, "adc6", 10),
342
+ SARADC_CHANNEL(7, "adc7", 10),
343
+};
344
+
345
+static const struct rockchip_saradc_data rk3562_saradc_data = {
346
+ .channels = rockchip_rk3562_saradc_iio_channels,
347
+ .num_channels = ARRAY_SIZE(rockchip_rk3562_saradc_iio_channels),
348
+ .clk_rate = 1000000,
349
+ .start = rockchip_saradc_start_v2,
350
+ .read = rockchip_saradc_read_v2,
351
+};
352
+
339353 static const struct iio_chan_spec rockchip_rk3568_saradc_iio_channels[] = {
340354 SARADC_CHANNEL(0, "adc0", 10),
341355 SARADC_CHANNEL(1, "adc1", 10),
....@@ -402,6 +416,9 @@
402416 .compatible = "rockchip,rk3528-saradc",
403417 .data = &rk3528_saradc_data,
404418 }, {
419
+ .compatible = "rockchip,rk3562-saradc",
420
+ .data = &rk3562_saradc_data,
421
+ }, {
405422 .compatible = "rockchip,rk3568-saradc",
406423 .data = &rk3568_saradc_data,
407424 }, {
....@@ -415,7 +432,7 @@
415432 };
416433 MODULE_DEVICE_TABLE(of, rockchip_saradc_match);
417434
418
-/**
435
+/*
419436 * Reset SARADC Controller.
420437 */
421438 static void rockchip_saradc_reset_controller(struct reset_control *reset)
....@@ -462,7 +479,7 @@
462479 int ret;
463480 int i, j = 0;
464481
465
- mutex_lock(&i_dev->mlock);
482
+ mutex_lock(&info->lock);
466483
467484 for_each_set_bit(i, i_dev->active_scan_mask, i_dev->masklength) {
468485 const struct iio_chan_spec *chan = &i_dev->channels[i];
....@@ -479,11 +496,31 @@
479496
480497 iio_push_to_buffers_with_timestamp(i_dev, &data, iio_get_time_ns(i_dev));
481498 out:
482
- mutex_unlock(&i_dev->mlock);
499
+ mutex_unlock(&info->lock);
483500
484501 iio_trigger_notify_done(i_dev->trig);
485502
486503 return IRQ_HANDLED;
504
+}
505
+
506
+static int rockchip_saradc_volt_notify(struct notifier_block *nb,
507
+ unsigned long event,
508
+ void *data)
509
+{
510
+ struct rockchip_saradc *info =
511
+ container_of(nb, struct rockchip_saradc, nb);
512
+
513
+ if (event & REGULATOR_EVENT_VOLTAGE_CHANGE)
514
+ info->uv_vref = (unsigned long)data;
515
+
516
+ return NOTIFY_OK;
517
+}
518
+
519
+static void rockchip_saradc_regulator_unreg_notifier(void *data)
520
+{
521
+ struct rockchip_saradc *info = data;
522
+
523
+ regulator_unregister_notifier(info->vref, &info->nb);
487524 }
488525
489526 #ifdef CONFIG_ROCKCHIP_SARADC_TEST_CHN
....@@ -510,7 +547,7 @@
510547 return size;
511548 }
512549
513
- if (!info->test && val < SARADC_CTRL_CHN_MASK) {
550
+ if (!info->test && val <= SARADC_CTRL_CHN_MASK) {
514551 info->test = true;
515552 info->chn = val;
516553 mod_delayed_work(info->wq, &info->work, msecs_to_jiffies(100));
....@@ -612,10 +649,8 @@
612649 init_completion(&info->completion);
613650
614651 irq = platform_get_irq(pdev, 0);
615
- if (irq < 0) {
616
- dev_err(&pdev->dev, "no irq resource?\n");
652
+ if (irq < 0)
617653 return irq;
618
- }
619654
620655 ret = devm_request_irq(&pdev->dev, irq, rockchip_saradc_isr,
621656 0, dev_name(&pdev->dev), info);
....@@ -669,12 +704,13 @@
669704 return ret;
670705 }
671706
672
- info->uv_vref = regulator_get_voltage(info->vref);
673
- if (info->uv_vref < 0) {
707
+ ret = regulator_get_voltage(info->vref);
708
+ if (ret < 0) {
674709 dev_err(&pdev->dev, "failed to get voltage\n");
675
- ret = info->uv_vref;
676710 return ret;
677711 }
712
+
713
+ info->uv_vref = ret;
678714
679715 ret = clk_prepare_enable(info->pclk);
680716 if (ret < 0) {
....@@ -705,8 +741,6 @@
705741 platform_set_drvdata(pdev, indio_dev);
706742
707743 indio_dev->name = dev_name(&pdev->dev);
708
- indio_dev->dev.parent = &pdev->dev;
709
- indio_dev->dev.of_node = pdev->dev.of_node;
710744 indio_dev->info = &rockchip_saradc_iio_info;
711745 indio_dev->modes = INDIO_DIRECT_MODE;
712746
....@@ -715,6 +749,17 @@
715749 ret = devm_iio_triggered_buffer_setup(&indio_dev->dev, indio_dev, NULL,
716750 rockchip_saradc_trigger_handler,
717751 NULL);
752
+ if (ret)
753
+ return ret;
754
+
755
+ info->nb.notifier_call = rockchip_saradc_volt_notify;
756
+ ret = regulator_register_notifier(info->vref, &info->nb);
757
+ if (ret)
758
+ return ret;
759
+
760
+ ret = devm_add_action_or_reset(&pdev->dev,
761
+ rockchip_saradc_regulator_unreg_notifier,
762
+ info);
718763 if (ret)
719764 return ret;
720765
....@@ -742,6 +787,8 @@
742787 return ret;
743788 }
744789 #endif
790
+ mutex_init(&info->lock);
791
+
745792 return devm_iio_device_register(&pdev->dev, indio_dev);
746793 }
747794
....@@ -752,14 +799,14 @@
752799 struct rockchip_saradc *info = iio_priv(indio_dev);
753800
754801 /* Avoid reading saradc when suspending */
755
- mutex_lock(&indio_dev->mlock);
802
+ mutex_lock(&info->lock);
756803
757804 clk_disable_unprepare(info->clk);
758805 clk_disable_unprepare(info->pclk);
759806 regulator_disable(info->vref);
760807
761808 info->suspended = true;
762
- mutex_unlock(&indio_dev->mlock);
809
+ mutex_unlock(&info->lock);
763810
764811 return 0;
765812 }
....@@ -800,21 +847,7 @@
800847 },
801848 };
802849
803
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
804
-static int __init rockchip_saradc_driver_init(void)
805
-{
806
- return platform_driver_register(&rockchip_saradc_driver);
807
-}
808
-fs_initcall(rockchip_saradc_driver_init);
809
-
810
-static void __exit rockchip_saradc_driver_exit(void)
811
-{
812
- platform_driver_unregister(&rockchip_saradc_driver);
813
-}
814
-module_exit(rockchip_saradc_driver_exit);
815
-#else
816850 module_platform_driver(rockchip_saradc_driver);
817
-#endif
818851
819852 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
820853 MODULE_DESCRIPTION("Rockchip SARADC driver");