| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Freescale i.MX7D ADC driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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. |
|---|
| 10 | 6 | */ |
|---|
| 11 | 7 | |
|---|
| 12 | 8 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 82 | 78 | #define IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT 0xf0000 |
|---|
| 83 | 79 | |
|---|
| 84 | 80 | #define IMX7D_ADC_TIMEOUT msecs_to_jiffies(100) |
|---|
| 81 | +#define IMX7D_ADC_INPUT_CLK 24000000 |
|---|
| 85 | 82 | |
|---|
| 86 | 83 | enum imx7d_adc_clk_pre_div { |
|---|
| 87 | 84 | IMX7D_ADC_ANALOG_CLK_PRE_DIV_4, |
|---|
| .. | .. |
|---|
| 104 | 101 | enum imx7d_adc_average_num avg_num; |
|---|
| 105 | 102 | |
|---|
| 106 | 103 | u32 core_time_unit; /* impact the sample rate */ |
|---|
| 107 | | - |
|---|
| 108 | | - bool average_en; |
|---|
| 109 | 104 | }; |
|---|
| 110 | 105 | |
|---|
| 111 | 106 | struct imx7d_adc { |
|---|
| .. | .. |
|---|
| 183 | 178 | info->adc_feature.clk_pre_div = IMX7D_ADC_ANALOG_CLK_PRE_DIV_4; |
|---|
| 184 | 179 | info->adc_feature.avg_num = IMX7D_ADC_AVERAGE_NUM_32; |
|---|
| 185 | 180 | info->adc_feature.core_time_unit = 1; |
|---|
| 186 | | - info->adc_feature.average_en = true; |
|---|
| 187 | 181 | } |
|---|
| 188 | 182 | |
|---|
| 189 | 183 | static void imx7d_adc_sample_rate_set(struct imx7d_adc *info) |
|---|
| .. | .. |
|---|
| 244 | 238 | |
|---|
| 245 | 239 | /* the channel choose single conversion, and enable average mode */ |
|---|
| 246 | 240 | 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); |
|---|
| 250 | 243 | |
|---|
| 251 | 244 | /* |
|---|
| 252 | 245 | * physical channel 0 chose logical channel A |
|---|
| .. | .. |
|---|
| 276 | 269 | |
|---|
| 277 | 270 | static u32 imx7d_adc_get_sample_rate(struct imx7d_adc *info) |
|---|
| 278 | 271 | { |
|---|
| 279 | | - /* input clock is always 24MHz */ |
|---|
| 280 | | - u32 input_clk = 24000000; |
|---|
| 281 | 272 | u32 analogue_core_clk; |
|---|
| 282 | 273 | u32 core_time_unit = info->adc_feature.core_time_unit; |
|---|
| 283 | 274 | u32 tmp; |
|---|
| 284 | 275 | |
|---|
| 285 | | - analogue_core_clk = input_clk / info->pre_div_num; |
|---|
| 276 | + analogue_core_clk = IMX7D_ADC_INPUT_CLK / info->pre_div_num; |
|---|
| 286 | 277 | tmp = (core_time_unit + 1) * 6; |
|---|
| 287 | 278 | |
|---|
| 288 | 279 | return analogue_core_clk / tmp; |
|---|
| .. | .. |
|---|
| 388 | 379 | * timeout flags. |
|---|
| 389 | 380 | */ |
|---|
| 390 | 381 | 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); |
|---|
| 393 | 385 | status &= ~IMX7D_REG_ADC_INT_STATUS_CHANNEL_CONV_TIME_OUT; |
|---|
| 394 | 386 | writel(status, info->regs + IMX7D_REG_ADC_INT_STATUS); |
|---|
| 395 | 387 | } |
|---|
| .. | .. |
|---|
| 433 | 425 | writel(adc_cfg, info->regs + IMX7D_REG_ADC_ADC_CFG); |
|---|
| 434 | 426 | } |
|---|
| 435 | 427 | |
|---|
| 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) |
|---|
| 566 | 429 | { |
|---|
| 567 | 430 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
|---|
| 568 | 431 | struct imx7d_adc *info = iio_priv(indio_dev); |
|---|
| .. | .. |
|---|
| 589 | 452 | return 0; |
|---|
| 590 | 453 | } |
|---|
| 591 | 454 | |
|---|
| 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); |
|---|
| 593 | 550 | |
|---|
| 594 | 551 | static struct platform_driver imx7d_adc_driver = { |
|---|
| 595 | 552 | .probe = imx7d_adc_probe, |
|---|
| 596 | | - .remove = imx7d_adc_remove, |
|---|
| 597 | 553 | .driver = { |
|---|
| 598 | 554 | .name = "imx7d_adc", |
|---|
| 599 | 555 | .of_match_table = imx7d_adc_match, |
|---|