19bf85fbcSTomer Maimon // SPDX-License-Identifier: GPL-2.0 29bf85fbcSTomer Maimon // Copyright (c) 2019 Nuvoton Technology corporation. 39bf85fbcSTomer Maimon 49bf85fbcSTomer Maimon #include <linux/clk.h> 59bf85fbcSTomer Maimon #include <linux/device.h> 69bf85fbcSTomer Maimon #include <linux/mfd/syscon.h> 79bf85fbcSTomer Maimon #include <linux/io.h> 89bf85fbcSTomer Maimon #include <linux/iio/iio.h> 99bf85fbcSTomer Maimon #include <linux/interrupt.h> 109bf85fbcSTomer Maimon #include <linux/kernel.h> 119bf85fbcSTomer Maimon #include <linux/module.h> 129bf85fbcSTomer Maimon #include <linux/platform_device.h> 139bf85fbcSTomer Maimon #include <linux/regmap.h> 149bf85fbcSTomer Maimon #include <linux/regulator/consumer.h> 159bf85fbcSTomer Maimon #include <linux/spinlock.h> 169bf85fbcSTomer Maimon #include <linux/uaccess.h> 17*db6bcb8cSTomer Maimon #include <linux/reset.h> 189bf85fbcSTomer Maimon 199bf85fbcSTomer Maimon struct npcm_adc { 209bf85fbcSTomer Maimon bool int_status; 219bf85fbcSTomer Maimon u32 adc_sample_hz; 229bf85fbcSTomer Maimon struct device *dev; 239bf85fbcSTomer Maimon void __iomem *regs; 249bf85fbcSTomer Maimon struct clk *adc_clk; 259bf85fbcSTomer Maimon wait_queue_head_t wq; 269bf85fbcSTomer Maimon struct regulator *vref; 27*db6bcb8cSTomer Maimon struct reset_control *reset; 289bf85fbcSTomer Maimon }; 299bf85fbcSTomer Maimon 309bf85fbcSTomer Maimon /* ADC registers */ 319bf85fbcSTomer Maimon #define NPCM_ADCCON 0x00 329bf85fbcSTomer Maimon #define NPCM_ADCDATA 0x04 339bf85fbcSTomer Maimon 349bf85fbcSTomer Maimon /* ADCCON Register Bits */ 359bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_EN BIT(21) 369bf85fbcSTomer Maimon #define NPCM_ADCCON_REFSEL BIT(19) 379bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_ST BIT(18) 389bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_EN BIT(17) 399bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_RST BIT(16) 409bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_CONV BIT(13) 419bf85fbcSTomer Maimon 429bf85fbcSTomer Maimon #define NPCM_ADCCON_CH_MASK GENMASK(27, 24) 439bf85fbcSTomer Maimon #define NPCM_ADCCON_CH(x) ((x) << 24) 449bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_SHIFT 1 459bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_MASK GENMASK(8, 1) 469bf85fbcSTomer Maimon #define NPCM_ADC_DATA_MASK(x) ((x) & GENMASK(9, 0)) 479bf85fbcSTomer Maimon 489bf85fbcSTomer Maimon #define NPCM_ADC_ENABLE (NPCM_ADCCON_ADC_EN | NPCM_ADCCON_ADC_INT_EN) 499bf85fbcSTomer Maimon 509bf85fbcSTomer Maimon /* ADC General Definition */ 519bf85fbcSTomer Maimon #define NPCM_RESOLUTION_BITS 10 529bf85fbcSTomer Maimon #define NPCM_INT_VREF_MV 2000 539bf85fbcSTomer Maimon 549bf85fbcSTomer Maimon #define NPCM_ADC_CHAN(ch) { \ 559bf85fbcSTomer Maimon .type = IIO_VOLTAGE, \ 569bf85fbcSTomer Maimon .indexed = 1, \ 579bf85fbcSTomer Maimon .channel = ch, \ 589bf85fbcSTomer Maimon .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 599bf85fbcSTomer Maimon .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 609bf85fbcSTomer Maimon BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 619bf85fbcSTomer Maimon } 629bf85fbcSTomer Maimon 639bf85fbcSTomer Maimon static const struct iio_chan_spec npcm_adc_iio_channels[] = { 649bf85fbcSTomer Maimon NPCM_ADC_CHAN(0), 659bf85fbcSTomer Maimon NPCM_ADC_CHAN(1), 669bf85fbcSTomer Maimon NPCM_ADC_CHAN(2), 679bf85fbcSTomer Maimon NPCM_ADC_CHAN(3), 689bf85fbcSTomer Maimon NPCM_ADC_CHAN(4), 699bf85fbcSTomer Maimon NPCM_ADC_CHAN(5), 709bf85fbcSTomer Maimon NPCM_ADC_CHAN(6), 719bf85fbcSTomer Maimon NPCM_ADC_CHAN(7), 729bf85fbcSTomer Maimon }; 739bf85fbcSTomer Maimon 749bf85fbcSTomer Maimon static irqreturn_t npcm_adc_isr(int irq, void *data) 759bf85fbcSTomer Maimon { 769bf85fbcSTomer Maimon u32 regtemp; 779bf85fbcSTomer Maimon struct iio_dev *indio_dev = data; 789bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 799bf85fbcSTomer Maimon 809bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 819bf85fbcSTomer Maimon if (regtemp & NPCM_ADCCON_ADC_INT_ST) { 829bf85fbcSTomer Maimon iowrite32(regtemp, info->regs + NPCM_ADCCON); 839bf85fbcSTomer Maimon wake_up_interruptible(&info->wq); 849bf85fbcSTomer Maimon info->int_status = true; 859bf85fbcSTomer Maimon } 869bf85fbcSTomer Maimon 879bf85fbcSTomer Maimon return IRQ_HANDLED; 889bf85fbcSTomer Maimon } 899bf85fbcSTomer Maimon 909bf85fbcSTomer Maimon static int npcm_adc_read(struct npcm_adc *info, int *val, u8 channel) 919bf85fbcSTomer Maimon { 929bf85fbcSTomer Maimon int ret; 939bf85fbcSTomer Maimon u32 regtemp; 949bf85fbcSTomer Maimon 959bf85fbcSTomer Maimon /* Select ADC channel */ 969bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 979bf85fbcSTomer Maimon regtemp &= ~NPCM_ADCCON_CH_MASK; 989bf85fbcSTomer Maimon info->int_status = false; 999bf85fbcSTomer Maimon iowrite32(regtemp | NPCM_ADCCON_CH(channel) | 1009bf85fbcSTomer Maimon NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON); 1019bf85fbcSTomer Maimon 1029bf85fbcSTomer Maimon ret = wait_event_interruptible_timeout(info->wq, info->int_status, 1039bf85fbcSTomer Maimon msecs_to_jiffies(10)); 1049bf85fbcSTomer Maimon if (ret == 0) { 1059bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 106*db6bcb8cSTomer Maimon if (regtemp & NPCM_ADCCON_ADC_CONV) { 1079bf85fbcSTomer Maimon /* if conversion failed - reset ADC module */ 108*db6bcb8cSTomer Maimon reset_control_assert(info->reset); 1099bf85fbcSTomer Maimon msleep(100); 110*db6bcb8cSTomer Maimon reset_control_deassert(info->reset); 1119bf85fbcSTomer Maimon msleep(100); 1129bf85fbcSTomer Maimon 1139bf85fbcSTomer Maimon /* Enable ADC and start conversion module */ 1149bf85fbcSTomer Maimon iowrite32(NPCM_ADC_ENABLE | NPCM_ADCCON_ADC_CONV, 1159bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 1169bf85fbcSTomer Maimon dev_err(info->dev, "RESET ADC Complete\n"); 1179bf85fbcSTomer Maimon } 1189bf85fbcSTomer Maimon return -ETIMEDOUT; 1199bf85fbcSTomer Maimon } 1209bf85fbcSTomer Maimon if (ret < 0) 1219bf85fbcSTomer Maimon return ret; 1229bf85fbcSTomer Maimon 1239bf85fbcSTomer Maimon *val = NPCM_ADC_DATA_MASK(ioread32(info->regs + NPCM_ADCDATA)); 1249bf85fbcSTomer Maimon 1259bf85fbcSTomer Maimon return 0; 1269bf85fbcSTomer Maimon } 1279bf85fbcSTomer Maimon 1289bf85fbcSTomer Maimon static int npcm_adc_read_raw(struct iio_dev *indio_dev, 1299bf85fbcSTomer Maimon struct iio_chan_spec const *chan, int *val, 1309bf85fbcSTomer Maimon int *val2, long mask) 1319bf85fbcSTomer Maimon { 1329bf85fbcSTomer Maimon int ret; 1339bf85fbcSTomer Maimon int vref_uv; 1349bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 1359bf85fbcSTomer Maimon 1369bf85fbcSTomer Maimon switch (mask) { 1379bf85fbcSTomer Maimon case IIO_CHAN_INFO_RAW: 1389bf85fbcSTomer Maimon mutex_lock(&indio_dev->mlock); 1399bf85fbcSTomer Maimon ret = npcm_adc_read(info, val, chan->channel); 1409bf85fbcSTomer Maimon mutex_unlock(&indio_dev->mlock); 1419bf85fbcSTomer Maimon if (ret) { 1429bf85fbcSTomer Maimon dev_err(info->dev, "NPCM ADC read failed\n"); 1439bf85fbcSTomer Maimon return ret; 1449bf85fbcSTomer Maimon } 1459bf85fbcSTomer Maimon return IIO_VAL_INT; 1469bf85fbcSTomer Maimon case IIO_CHAN_INFO_SCALE: 1474e63ed6bSTomer Maimon if (!IS_ERR(info->vref)) { 1489bf85fbcSTomer Maimon vref_uv = regulator_get_voltage(info->vref); 1499bf85fbcSTomer Maimon *val = vref_uv / 1000; 1509bf85fbcSTomer Maimon } else { 1519bf85fbcSTomer Maimon *val = NPCM_INT_VREF_MV; 1529bf85fbcSTomer Maimon } 1539bf85fbcSTomer Maimon *val2 = NPCM_RESOLUTION_BITS; 1549bf85fbcSTomer Maimon return IIO_VAL_FRACTIONAL_LOG2; 1559bf85fbcSTomer Maimon case IIO_CHAN_INFO_SAMP_FREQ: 1569bf85fbcSTomer Maimon *val = info->adc_sample_hz; 1579bf85fbcSTomer Maimon return IIO_VAL_INT; 1589bf85fbcSTomer Maimon default: 1599bf85fbcSTomer Maimon return -EINVAL; 1609bf85fbcSTomer Maimon } 1619bf85fbcSTomer Maimon 1629bf85fbcSTomer Maimon return 0; 1639bf85fbcSTomer Maimon } 1649bf85fbcSTomer Maimon 1659bf85fbcSTomer Maimon static const struct iio_info npcm_adc_iio_info = { 1669bf85fbcSTomer Maimon .read_raw = &npcm_adc_read_raw, 1679bf85fbcSTomer Maimon }; 1689bf85fbcSTomer Maimon 1699bf85fbcSTomer Maimon static const struct of_device_id npcm_adc_match[] = { 1709bf85fbcSTomer Maimon { .compatible = "nuvoton,npcm750-adc", }, 1719bf85fbcSTomer Maimon { /* sentinel */ } 1729bf85fbcSTomer Maimon }; 1739bf85fbcSTomer Maimon MODULE_DEVICE_TABLE(of, npcm_adc_match); 1749bf85fbcSTomer Maimon 1759bf85fbcSTomer Maimon static int npcm_adc_probe(struct platform_device *pdev) 1769bf85fbcSTomer Maimon { 1779bf85fbcSTomer Maimon int ret; 1789bf85fbcSTomer Maimon int irq; 1799bf85fbcSTomer Maimon u32 div; 1809bf85fbcSTomer Maimon u32 reg_con; 1819bf85fbcSTomer Maimon struct npcm_adc *info; 1829bf85fbcSTomer Maimon struct iio_dev *indio_dev; 1839bf85fbcSTomer Maimon struct device *dev = &pdev->dev; 1849bf85fbcSTomer Maimon 1859bf85fbcSTomer Maimon indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info)); 1869bf85fbcSTomer Maimon if (!indio_dev) 1879bf85fbcSTomer Maimon return -ENOMEM; 1889bf85fbcSTomer Maimon info = iio_priv(indio_dev); 1899bf85fbcSTomer Maimon 1909bf85fbcSTomer Maimon info->dev = &pdev->dev; 1919bf85fbcSTomer Maimon 19208cf48c7SJonathan Cameron info->regs = devm_platform_ioremap_resource(pdev, 0); 1939bf85fbcSTomer Maimon if (IS_ERR(info->regs)) 1949bf85fbcSTomer Maimon return PTR_ERR(info->regs); 1959bf85fbcSTomer Maimon 196*db6bcb8cSTomer Maimon info->reset = devm_reset_control_get(&pdev->dev, NULL); 197*db6bcb8cSTomer Maimon if (IS_ERR(info->reset)) 198*db6bcb8cSTomer Maimon return PTR_ERR(info->reset); 199*db6bcb8cSTomer Maimon 2009bf85fbcSTomer Maimon info->adc_clk = devm_clk_get(&pdev->dev, NULL); 2019bf85fbcSTomer Maimon if (IS_ERR(info->adc_clk)) { 2029bf85fbcSTomer Maimon dev_warn(&pdev->dev, "ADC clock failed: can't read clk\n"); 2039bf85fbcSTomer Maimon return PTR_ERR(info->adc_clk); 2049bf85fbcSTomer Maimon } 2059bf85fbcSTomer Maimon 2069bf85fbcSTomer Maimon /* calculate ADC clock sample rate */ 2079bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2089bf85fbcSTomer Maimon div = reg_con & NPCM_ADCCON_DIV_MASK; 2099bf85fbcSTomer Maimon div = div >> NPCM_ADCCON_DIV_SHIFT; 2109bf85fbcSTomer Maimon info->adc_sample_hz = clk_get_rate(info->adc_clk) / ((div + 1) * 2); 2119bf85fbcSTomer Maimon 2129bf85fbcSTomer Maimon irq = platform_get_irq(pdev, 0); 2139bf85fbcSTomer Maimon if (irq <= 0) { 2149bf85fbcSTomer Maimon ret = -EINVAL; 2159bf85fbcSTomer Maimon goto err_disable_clk; 2169bf85fbcSTomer Maimon } 2179bf85fbcSTomer Maimon 2189bf85fbcSTomer Maimon ret = devm_request_irq(&pdev->dev, irq, npcm_adc_isr, 0, 2199bf85fbcSTomer Maimon "NPCM_ADC", indio_dev); 2209bf85fbcSTomer Maimon if (ret < 0) { 2219bf85fbcSTomer Maimon dev_err(dev, "failed requesting interrupt\n"); 2229bf85fbcSTomer Maimon goto err_disable_clk; 2239bf85fbcSTomer Maimon } 2249bf85fbcSTomer Maimon 2259bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2269bf85fbcSTomer Maimon info->vref = devm_regulator_get_optional(&pdev->dev, "vref"); 2279bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) { 2289bf85fbcSTomer Maimon ret = regulator_enable(info->vref); 2299bf85fbcSTomer Maimon if (ret) { 2309bf85fbcSTomer Maimon dev_err(&pdev->dev, "Can't enable ADC reference voltage\n"); 2319bf85fbcSTomer Maimon goto err_disable_clk; 2329bf85fbcSTomer Maimon } 2339bf85fbcSTomer Maimon 2349bf85fbcSTomer Maimon iowrite32(reg_con & ~NPCM_ADCCON_REFSEL, 2359bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 2369bf85fbcSTomer Maimon } else { 2379bf85fbcSTomer Maimon /* 2389bf85fbcSTomer Maimon * Any error which is not ENODEV indicates the regulator 2399bf85fbcSTomer Maimon * has been specified and so is a failure case. 2409bf85fbcSTomer Maimon */ 2419bf85fbcSTomer Maimon if (PTR_ERR(info->vref) != -ENODEV) { 2429bf85fbcSTomer Maimon ret = PTR_ERR(info->vref); 2439bf85fbcSTomer Maimon goto err_disable_clk; 2449bf85fbcSTomer Maimon } 2459bf85fbcSTomer Maimon 2469bf85fbcSTomer Maimon /* Use internal reference */ 2479bf85fbcSTomer Maimon iowrite32(reg_con | NPCM_ADCCON_REFSEL, 2489bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 2499bf85fbcSTomer Maimon } 2509bf85fbcSTomer Maimon 2519bf85fbcSTomer Maimon init_waitqueue_head(&info->wq); 2529bf85fbcSTomer Maimon 2539bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2549bf85fbcSTomer Maimon reg_con |= NPCM_ADC_ENABLE; 2559bf85fbcSTomer Maimon 2569bf85fbcSTomer Maimon /* Enable the ADC Module */ 2579bf85fbcSTomer Maimon iowrite32(reg_con, info->regs + NPCM_ADCCON); 2589bf85fbcSTomer Maimon 2599bf85fbcSTomer Maimon /* Start ADC conversion */ 2609bf85fbcSTomer Maimon iowrite32(reg_con | NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON); 2619bf85fbcSTomer Maimon 2629bf85fbcSTomer Maimon platform_set_drvdata(pdev, indio_dev); 2639bf85fbcSTomer Maimon indio_dev->name = dev_name(&pdev->dev); 2649bf85fbcSTomer Maimon indio_dev->dev.parent = &pdev->dev; 2659bf85fbcSTomer Maimon indio_dev->info = &npcm_adc_iio_info; 2669bf85fbcSTomer Maimon indio_dev->modes = INDIO_DIRECT_MODE; 2679bf85fbcSTomer Maimon indio_dev->channels = npcm_adc_iio_channels; 2689bf85fbcSTomer Maimon indio_dev->num_channels = ARRAY_SIZE(npcm_adc_iio_channels); 2699bf85fbcSTomer Maimon 2709bf85fbcSTomer Maimon ret = iio_device_register(indio_dev); 2719bf85fbcSTomer Maimon if (ret) { 2729bf85fbcSTomer Maimon dev_err(&pdev->dev, "Couldn't register the device.\n"); 2739bf85fbcSTomer Maimon goto err_iio_register; 2749bf85fbcSTomer Maimon } 2759bf85fbcSTomer Maimon 2769bf85fbcSTomer Maimon pr_info("NPCM ADC driver probed\n"); 2779bf85fbcSTomer Maimon 2789bf85fbcSTomer Maimon return 0; 2799bf85fbcSTomer Maimon 2809bf85fbcSTomer Maimon err_iio_register: 2819bf85fbcSTomer Maimon iowrite32(reg_con & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON); 2829bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) 2839bf85fbcSTomer Maimon regulator_disable(info->vref); 2849bf85fbcSTomer Maimon err_disable_clk: 2859bf85fbcSTomer Maimon clk_disable_unprepare(info->adc_clk); 2869bf85fbcSTomer Maimon 2879bf85fbcSTomer Maimon return ret; 2889bf85fbcSTomer Maimon } 2899bf85fbcSTomer Maimon 2909bf85fbcSTomer Maimon static int npcm_adc_remove(struct platform_device *pdev) 2919bf85fbcSTomer Maimon { 2929bf85fbcSTomer Maimon struct iio_dev *indio_dev = platform_get_drvdata(pdev); 2939bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 2949bf85fbcSTomer Maimon u32 regtemp; 2959bf85fbcSTomer Maimon 2969bf85fbcSTomer Maimon iio_device_unregister(indio_dev); 2979bf85fbcSTomer Maimon 2989bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 2999bf85fbcSTomer Maimon iowrite32(regtemp & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON); 3009bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) 3019bf85fbcSTomer Maimon regulator_disable(info->vref); 3029bf85fbcSTomer Maimon clk_disable_unprepare(info->adc_clk); 3039bf85fbcSTomer Maimon 3049bf85fbcSTomer Maimon return 0; 3059bf85fbcSTomer Maimon } 3069bf85fbcSTomer Maimon 3079bf85fbcSTomer Maimon static struct platform_driver npcm_adc_driver = { 3089bf85fbcSTomer Maimon .probe = npcm_adc_probe, 3099bf85fbcSTomer Maimon .remove = npcm_adc_remove, 3109bf85fbcSTomer Maimon .driver = { 3119bf85fbcSTomer Maimon .name = "npcm_adc", 3129bf85fbcSTomer Maimon .of_match_table = npcm_adc_match, 3139bf85fbcSTomer Maimon }, 3149bf85fbcSTomer Maimon }; 3159bf85fbcSTomer Maimon 3169bf85fbcSTomer Maimon module_platform_driver(npcm_adc_driver); 3179bf85fbcSTomer Maimon 3189bf85fbcSTomer Maimon MODULE_DESCRIPTION("Nuvoton NPCM ADC Driver"); 3199bf85fbcSTomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); 3209bf85fbcSTomer Maimon MODULE_LICENSE("GPL v2"); 321