hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/iio/adc/imx7d_adc.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Freescale i.MX7D ADC driver
34 *
45 * Copyright (C) 2015 Freescale Semiconductor, Inc.
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.
106 */
117
128 #include <linux/clk.h>
....@@ -82,6 +78,7 @@
8278 #define IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT 0xf0000
8379
8480 #define IMX7D_ADC_TIMEOUT msecs_to_jiffies(100)
81
+#define IMX7D_ADC_INPUT_CLK 24000000
8582
8683 enum imx7d_adc_clk_pre_div {
8784 IMX7D_ADC_ANALOG_CLK_PRE_DIV_4,
....@@ -104,8 +101,6 @@
104101 enum imx7d_adc_average_num avg_num;
105102
106103 u32 core_time_unit; /* impact the sample rate */
107
-
108
- bool average_en;
109104 };
110105
111106 struct imx7d_adc {
....@@ -183,7 +178,6 @@
183178 info->adc_feature.clk_pre_div = IMX7D_ADC_ANALOG_CLK_PRE_DIV_4;
184179 info->adc_feature.avg_num = IMX7D_ADC_AVERAGE_NUM_32;
185180 info->adc_feature.core_time_unit = 1;
186
- info->adc_feature.average_en = true;
187181 }
188182
189183 static void imx7d_adc_sample_rate_set(struct imx7d_adc *info)
....@@ -244,9 +238,8 @@
244238
245239 /* the channel choose single conversion, and enable average mode */
246240 cfg1 |= (IMX7D_REG_ADC_CH_CFG1_CHANNEL_EN |
247
- IMX7D_REG_ADC_CH_CFG1_CHANNEL_SINGLE);
248
- if (info->adc_feature.average_en)
249
- cfg1 |= IMX7D_REG_ADC_CH_CFG1_CHANNEL_AVG_EN;
241
+ IMX7D_REG_ADC_CH_CFG1_CHANNEL_SINGLE |
242
+ IMX7D_REG_ADC_CH_CFG1_CHANNEL_AVG_EN);
250243
251244 /*
252245 * physical channel 0 chose logical channel A
....@@ -276,13 +269,11 @@
276269
277270 static u32 imx7d_adc_get_sample_rate(struct imx7d_adc *info)
278271 {
279
- /* input clock is always 24MHz */
280
- u32 input_clk = 24000000;
281272 u32 analogue_core_clk;
282273 u32 core_time_unit = info->adc_feature.core_time_unit;
283274 u32 tmp;
284275
285
- analogue_core_clk = input_clk / info->pre_div_num;
276
+ analogue_core_clk = IMX7D_ADC_INPUT_CLK / info->pre_div_num;
286277 tmp = (core_time_unit + 1) * 6;
287278
288279 return analogue_core_clk / tmp;
....@@ -388,8 +379,9 @@
388379 * timeout flags.
389380 */
390381 if (status & IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT) {
391
- pr_err("%s: ADC got conversion time out interrupt: 0x%08x\n",
392
- dev_name(info->dev), status);
382
+ dev_err(info->dev,
383
+ "ADC got conversion time out interrupt: 0x%08x\n",
384
+ status);
393385 status &= ~IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT;
394386 writel(status, info->regs + IMX7D_REG_ADC_INT_STATUS);
395387 }
....@@ -433,136 +425,7 @@
433425 writel(adc_cfg, info->regs + IMX7D_REG_ADC_ADC_CFG);
434426 }
435427
436
-static int imx7d_adc_probe(struct platform_device *pdev)
437
-{
438
- struct imx7d_adc *info;
439
- struct iio_dev *indio_dev;
440
- struct resource *mem;
441
- int irq;
442
- int ret;
443
-
444
- indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
445
- if (!indio_dev) {
446
- dev_err(&pdev->dev, "Failed allocating iio device\n");
447
- return -ENOMEM;
448
- }
449
-
450
- info = iio_priv(indio_dev);
451
- info->dev = &pdev->dev;
452
-
453
- mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
454
- info->regs = devm_ioremap_resource(&pdev->dev, mem);
455
- if (IS_ERR(info->regs)) {
456
- ret = PTR_ERR(info->regs);
457
- dev_err(&pdev->dev,
458
- "Failed to remap adc memory, err = %d\n", ret);
459
- return ret;
460
- }
461
-
462
- irq = platform_get_irq(pdev, 0);
463
- if (irq < 0) {
464
- dev_err(&pdev->dev, "No irq resource?\n");
465
- return irq;
466
- }
467
-
468
- info->clk = devm_clk_get(&pdev->dev, "adc");
469
- if (IS_ERR(info->clk)) {
470
- ret = PTR_ERR(info->clk);
471
- dev_err(&pdev->dev, "Failed getting clock, err = %d\n", ret);
472
- return ret;
473
- }
474
-
475
- info->vref = devm_regulator_get(&pdev->dev, "vref");
476
- if (IS_ERR(info->vref)) {
477
- ret = PTR_ERR(info->vref);
478
- dev_err(&pdev->dev,
479
- "Failed getting reference voltage, err = %d\n", ret);
480
- return ret;
481
- }
482
-
483
- ret = regulator_enable(info->vref);
484
- if (ret) {
485
- dev_err(&pdev->dev,
486
- "Can't enable adc reference top voltage, err = %d\n",
487
- ret);
488
- return ret;
489
- }
490
-
491
- platform_set_drvdata(pdev, indio_dev);
492
-
493
- init_completion(&info->completion);
494
-
495
- indio_dev->name = dev_name(&pdev->dev);
496
- indio_dev->dev.parent = &pdev->dev;
497
- indio_dev->info = &imx7d_adc_iio_info;
498
- indio_dev->modes = INDIO_DIRECT_MODE;
499
- indio_dev->channels = imx7d_adc_iio_channels;
500
- indio_dev->num_channels = ARRAY_SIZE(imx7d_adc_iio_channels);
501
-
502
- ret = clk_prepare_enable(info->clk);
503
- if (ret) {
504
- dev_err(&pdev->dev,
505
- "Could not prepare or enable the clock.\n");
506
- goto error_adc_clk_enable;
507
- }
508
-
509
- ret = devm_request_irq(info->dev, irq,
510
- imx7d_adc_isr, 0,
511
- dev_name(&pdev->dev), info);
512
- if (ret < 0) {
513
- dev_err(&pdev->dev, "Failed requesting irq, irq = %d\n", irq);
514
- goto error_iio_device_register;
515
- }
516
-
517
- imx7d_adc_feature_config(info);
518
- imx7d_adc_hw_init(info);
519
-
520
- ret = iio_device_register(indio_dev);
521
- if (ret) {
522
- imx7d_adc_power_down(info);
523
- dev_err(&pdev->dev, "Couldn't register the device.\n");
524
- goto error_iio_device_register;
525
- }
526
-
527
- return 0;
528
-
529
-error_iio_device_register:
530
- clk_disable_unprepare(info->clk);
531
-error_adc_clk_enable:
532
- regulator_disable(info->vref);
533
-
534
- return ret;
535
-}
536
-
537
-static int imx7d_adc_remove(struct platform_device *pdev)
538
-{
539
- struct iio_dev *indio_dev = platform_get_drvdata(pdev);
540
- struct imx7d_adc *info = iio_priv(indio_dev);
541
-
542
- iio_device_unregister(indio_dev);
543
-
544
- imx7d_adc_power_down(info);
545
-
546
- clk_disable_unprepare(info->clk);
547
- regulator_disable(info->vref);
548
-
549
- return 0;
550
-}
551
-
552
-static int __maybe_unused imx7d_adc_suspend(struct device *dev)
553
-{
554
- struct iio_dev *indio_dev = dev_get_drvdata(dev);
555
- struct imx7d_adc *info = iio_priv(indio_dev);
556
-
557
- imx7d_adc_power_down(info);
558
-
559
- clk_disable_unprepare(info->clk);
560
- regulator_disable(info->vref);
561
-
562
- return 0;
563
-}
564
-
565
-static int __maybe_unused imx7d_adc_resume(struct device *dev)
428
+static int imx7d_adc_enable(struct device *dev)
566429 {
567430 struct iio_dev *indio_dev = dev_get_drvdata(dev);
568431 struct imx7d_adc *info = iio_priv(indio_dev);
....@@ -589,11 +452,104 @@
589452 return 0;
590453 }
591454
592
-static SIMPLE_DEV_PM_OPS(imx7d_adc_pm_ops, imx7d_adc_suspend, imx7d_adc_resume);
455
+static int imx7d_adc_disable(struct device *dev)
456
+{
457
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
458
+ struct imx7d_adc *info = iio_priv(indio_dev);
459
+
460
+ imx7d_adc_power_down(info);
461
+
462
+ clk_disable_unprepare(info->clk);
463
+ regulator_disable(info->vref);
464
+
465
+ return 0;
466
+}
467
+
468
+static void __imx7d_adc_disable(void *data)
469
+{
470
+ imx7d_adc_disable(data);
471
+}
472
+
473
+static int imx7d_adc_probe(struct platform_device *pdev)
474
+{
475
+ struct imx7d_adc *info;
476
+ struct iio_dev *indio_dev;
477
+ struct device *dev = &pdev->dev;
478
+ int irq;
479
+ int ret;
480
+
481
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
482
+ if (!indio_dev) {
483
+ dev_err(&pdev->dev, "Failed allocating iio device\n");
484
+ return -ENOMEM;
485
+ }
486
+
487
+ info = iio_priv(indio_dev);
488
+ info->dev = dev;
489
+
490
+ info->regs = devm_platform_ioremap_resource(pdev, 0);
491
+ if (IS_ERR(info->regs))
492
+ return PTR_ERR(info->regs);
493
+
494
+ irq = platform_get_irq(pdev, 0);
495
+ if (irq < 0)
496
+ return irq;
497
+
498
+ info->clk = devm_clk_get(dev, "adc");
499
+ if (IS_ERR(info->clk)) {
500
+ ret = PTR_ERR(info->clk);
501
+ dev_err(dev, "Failed getting clock, err = %d\n", ret);
502
+ return ret;
503
+ }
504
+
505
+ info->vref = devm_regulator_get(dev, "vref");
506
+ if (IS_ERR(info->vref)) {
507
+ ret = PTR_ERR(info->vref);
508
+ dev_err(dev,
509
+ "Failed getting reference voltage, err = %d\n", ret);
510
+ return ret;
511
+ }
512
+
513
+ platform_set_drvdata(pdev, indio_dev);
514
+
515
+ init_completion(&info->completion);
516
+
517
+ indio_dev->name = dev_name(dev);
518
+ indio_dev->info = &imx7d_adc_iio_info;
519
+ indio_dev->modes = INDIO_DIRECT_MODE;
520
+ indio_dev->channels = imx7d_adc_iio_channels;
521
+ indio_dev->num_channels = ARRAY_SIZE(imx7d_adc_iio_channels);
522
+
523
+ ret = devm_request_irq(dev, irq, imx7d_adc_isr, 0, dev_name(dev), info);
524
+ if (ret < 0) {
525
+ dev_err(dev, "Failed requesting irq, irq = %d\n", irq);
526
+ return ret;
527
+ }
528
+
529
+ imx7d_adc_feature_config(info);
530
+
531
+ ret = imx7d_adc_enable(&indio_dev->dev);
532
+ if (ret)
533
+ return ret;
534
+
535
+ ret = devm_add_action_or_reset(dev, __imx7d_adc_disable,
536
+ &indio_dev->dev);
537
+ if (ret)
538
+ return ret;
539
+
540
+ ret = devm_iio_device_register(dev, indio_dev);
541
+ if (ret) {
542
+ dev_err(&pdev->dev, "Couldn't register the device.\n");
543
+ return ret;
544
+ }
545
+
546
+ return 0;
547
+}
548
+
549
+static SIMPLE_DEV_PM_OPS(imx7d_adc_pm_ops, imx7d_adc_disable, imx7d_adc_enable);
593550
594551 static struct platform_driver imx7d_adc_driver = {
595552 .probe = imx7d_adc_probe,
596
- .remove = imx7d_adc_remove,
597553 .driver = {
598554 .name = "imx7d_adc",
599555 .of_match_table = imx7d_adc_match,