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> 17db6bcb8cSTomer 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; 27db6bcb8cSTomer Maimon struct reset_control *reset; 28*08dfc6f8SSergiu Cuciurean /* 29*08dfc6f8SSergiu Cuciurean * Lock to protect the device state during a potential concurrent 30*08dfc6f8SSergiu Cuciurean * read access from userspace. Reading a raw value requires a sequence 31*08dfc6f8SSergiu Cuciurean * of register writes, then a wait for a event and finally a register 32*08dfc6f8SSergiu Cuciurean * read, during which userspace could issue another read request. 33*08dfc6f8SSergiu Cuciurean * This lock protects a read access from ocurring before another one 34*08dfc6f8SSergiu Cuciurean * has finished. 35*08dfc6f8SSergiu Cuciurean */ 36*08dfc6f8SSergiu Cuciurean struct mutex lock; 379bf85fbcSTomer Maimon }; 389bf85fbcSTomer Maimon 399bf85fbcSTomer Maimon /* ADC registers */ 409bf85fbcSTomer Maimon #define NPCM_ADCCON 0x00 419bf85fbcSTomer Maimon #define NPCM_ADCDATA 0x04 429bf85fbcSTomer Maimon 439bf85fbcSTomer Maimon /* ADCCON Register Bits */ 449bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_EN BIT(21) 459bf85fbcSTomer Maimon #define NPCM_ADCCON_REFSEL BIT(19) 469bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_INT_ST BIT(18) 479bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_EN BIT(17) 489bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_RST BIT(16) 499bf85fbcSTomer Maimon #define NPCM_ADCCON_ADC_CONV BIT(13) 509bf85fbcSTomer Maimon 519bf85fbcSTomer Maimon #define NPCM_ADCCON_CH_MASK GENMASK(27, 24) 529bf85fbcSTomer Maimon #define NPCM_ADCCON_CH(x) ((x) << 24) 539bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_SHIFT 1 549bf85fbcSTomer Maimon #define NPCM_ADCCON_DIV_MASK GENMASK(8, 1) 559bf85fbcSTomer Maimon #define NPCM_ADC_DATA_MASK(x) ((x) & GENMASK(9, 0)) 569bf85fbcSTomer Maimon 579bf85fbcSTomer Maimon #define NPCM_ADC_ENABLE (NPCM_ADCCON_ADC_EN | NPCM_ADCCON_ADC_INT_EN) 589bf85fbcSTomer Maimon 599bf85fbcSTomer Maimon /* ADC General Definition */ 609bf85fbcSTomer Maimon #define NPCM_RESOLUTION_BITS 10 619bf85fbcSTomer Maimon #define NPCM_INT_VREF_MV 2000 629bf85fbcSTomer Maimon 639bf85fbcSTomer Maimon #define NPCM_ADC_CHAN(ch) { \ 649bf85fbcSTomer Maimon .type = IIO_VOLTAGE, \ 659bf85fbcSTomer Maimon .indexed = 1, \ 669bf85fbcSTomer Maimon .channel = ch, \ 679bf85fbcSTomer Maimon .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 689bf85fbcSTomer Maimon .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 699bf85fbcSTomer Maimon BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 709bf85fbcSTomer Maimon } 719bf85fbcSTomer Maimon 729bf85fbcSTomer Maimon static const struct iio_chan_spec npcm_adc_iio_channels[] = { 739bf85fbcSTomer Maimon NPCM_ADC_CHAN(0), 749bf85fbcSTomer Maimon NPCM_ADC_CHAN(1), 759bf85fbcSTomer Maimon NPCM_ADC_CHAN(2), 769bf85fbcSTomer Maimon NPCM_ADC_CHAN(3), 779bf85fbcSTomer Maimon NPCM_ADC_CHAN(4), 789bf85fbcSTomer Maimon NPCM_ADC_CHAN(5), 799bf85fbcSTomer Maimon NPCM_ADC_CHAN(6), 809bf85fbcSTomer Maimon NPCM_ADC_CHAN(7), 819bf85fbcSTomer Maimon }; 829bf85fbcSTomer Maimon 839bf85fbcSTomer Maimon static irqreturn_t npcm_adc_isr(int irq, void *data) 849bf85fbcSTomer Maimon { 859bf85fbcSTomer Maimon u32 regtemp; 869bf85fbcSTomer Maimon struct iio_dev *indio_dev = data; 879bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 889bf85fbcSTomer Maimon 899bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 909bf85fbcSTomer Maimon if (regtemp & NPCM_ADCCON_ADC_INT_ST) { 919bf85fbcSTomer Maimon iowrite32(regtemp, info->regs + NPCM_ADCCON); 929bf85fbcSTomer Maimon wake_up_interruptible(&info->wq); 939bf85fbcSTomer Maimon info->int_status = true; 949bf85fbcSTomer Maimon } 959bf85fbcSTomer Maimon 969bf85fbcSTomer Maimon return IRQ_HANDLED; 979bf85fbcSTomer Maimon } 989bf85fbcSTomer Maimon 999bf85fbcSTomer Maimon static int npcm_adc_read(struct npcm_adc *info, int *val, u8 channel) 1009bf85fbcSTomer Maimon { 1019bf85fbcSTomer Maimon int ret; 1029bf85fbcSTomer Maimon u32 regtemp; 1039bf85fbcSTomer Maimon 1049bf85fbcSTomer Maimon /* Select ADC channel */ 1059bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 1069bf85fbcSTomer Maimon regtemp &= ~NPCM_ADCCON_CH_MASK; 1079bf85fbcSTomer Maimon info->int_status = false; 1089bf85fbcSTomer Maimon iowrite32(regtemp | NPCM_ADCCON_CH(channel) | 1099bf85fbcSTomer Maimon NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON); 1109bf85fbcSTomer Maimon 1119bf85fbcSTomer Maimon ret = wait_event_interruptible_timeout(info->wq, info->int_status, 1129bf85fbcSTomer Maimon msecs_to_jiffies(10)); 1139bf85fbcSTomer Maimon if (ret == 0) { 1149bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 115db6bcb8cSTomer Maimon if (regtemp & NPCM_ADCCON_ADC_CONV) { 1169bf85fbcSTomer Maimon /* if conversion failed - reset ADC module */ 117db6bcb8cSTomer Maimon reset_control_assert(info->reset); 1189bf85fbcSTomer Maimon msleep(100); 119db6bcb8cSTomer Maimon reset_control_deassert(info->reset); 1209bf85fbcSTomer Maimon msleep(100); 1219bf85fbcSTomer Maimon 1229bf85fbcSTomer Maimon /* Enable ADC and start conversion module */ 1239bf85fbcSTomer Maimon iowrite32(NPCM_ADC_ENABLE | NPCM_ADCCON_ADC_CONV, 1249bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 1259bf85fbcSTomer Maimon dev_err(info->dev, "RESET ADC Complete\n"); 1269bf85fbcSTomer Maimon } 1279bf85fbcSTomer Maimon return -ETIMEDOUT; 1289bf85fbcSTomer Maimon } 1299bf85fbcSTomer Maimon if (ret < 0) 1309bf85fbcSTomer Maimon return ret; 1319bf85fbcSTomer Maimon 1329bf85fbcSTomer Maimon *val = NPCM_ADC_DATA_MASK(ioread32(info->regs + NPCM_ADCDATA)); 1339bf85fbcSTomer Maimon 1349bf85fbcSTomer Maimon return 0; 1359bf85fbcSTomer Maimon } 1369bf85fbcSTomer Maimon 1379bf85fbcSTomer Maimon static int npcm_adc_read_raw(struct iio_dev *indio_dev, 1389bf85fbcSTomer Maimon struct iio_chan_spec const *chan, int *val, 1399bf85fbcSTomer Maimon int *val2, long mask) 1409bf85fbcSTomer Maimon { 1419bf85fbcSTomer Maimon int ret; 1429bf85fbcSTomer Maimon int vref_uv; 1439bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 1449bf85fbcSTomer Maimon 1459bf85fbcSTomer Maimon switch (mask) { 1469bf85fbcSTomer Maimon case IIO_CHAN_INFO_RAW: 147*08dfc6f8SSergiu Cuciurean mutex_lock(&info->lock); 1489bf85fbcSTomer Maimon ret = npcm_adc_read(info, val, chan->channel); 149*08dfc6f8SSergiu Cuciurean mutex_unlock(&info->lock); 1509bf85fbcSTomer Maimon if (ret) { 1519bf85fbcSTomer Maimon dev_err(info->dev, "NPCM ADC read failed\n"); 1529bf85fbcSTomer Maimon return ret; 1539bf85fbcSTomer Maimon } 1549bf85fbcSTomer Maimon return IIO_VAL_INT; 1559bf85fbcSTomer Maimon case IIO_CHAN_INFO_SCALE: 1564e63ed6bSTomer Maimon if (!IS_ERR(info->vref)) { 1579bf85fbcSTomer Maimon vref_uv = regulator_get_voltage(info->vref); 1589bf85fbcSTomer Maimon *val = vref_uv / 1000; 1599bf85fbcSTomer Maimon } else { 1609bf85fbcSTomer Maimon *val = NPCM_INT_VREF_MV; 1619bf85fbcSTomer Maimon } 1629bf85fbcSTomer Maimon *val2 = NPCM_RESOLUTION_BITS; 1639bf85fbcSTomer Maimon return IIO_VAL_FRACTIONAL_LOG2; 1649bf85fbcSTomer Maimon case IIO_CHAN_INFO_SAMP_FREQ: 1659bf85fbcSTomer Maimon *val = info->adc_sample_hz; 1669bf85fbcSTomer Maimon return IIO_VAL_INT; 1679bf85fbcSTomer Maimon default: 1689bf85fbcSTomer Maimon return -EINVAL; 1699bf85fbcSTomer Maimon } 1709bf85fbcSTomer Maimon 1719bf85fbcSTomer Maimon return 0; 1729bf85fbcSTomer Maimon } 1739bf85fbcSTomer Maimon 1749bf85fbcSTomer Maimon static const struct iio_info npcm_adc_iio_info = { 1759bf85fbcSTomer Maimon .read_raw = &npcm_adc_read_raw, 1769bf85fbcSTomer Maimon }; 1779bf85fbcSTomer Maimon 1789bf85fbcSTomer Maimon static const struct of_device_id npcm_adc_match[] = { 1799bf85fbcSTomer Maimon { .compatible = "nuvoton,npcm750-adc", }, 1809bf85fbcSTomer Maimon { /* sentinel */ } 1819bf85fbcSTomer Maimon }; 1829bf85fbcSTomer Maimon MODULE_DEVICE_TABLE(of, npcm_adc_match); 1839bf85fbcSTomer Maimon 1849bf85fbcSTomer Maimon static int npcm_adc_probe(struct platform_device *pdev) 1859bf85fbcSTomer Maimon { 1869bf85fbcSTomer Maimon int ret; 1879bf85fbcSTomer Maimon int irq; 1889bf85fbcSTomer Maimon u32 div; 1899bf85fbcSTomer Maimon u32 reg_con; 1909bf85fbcSTomer Maimon struct npcm_adc *info; 1919bf85fbcSTomer Maimon struct iio_dev *indio_dev; 1929bf85fbcSTomer Maimon struct device *dev = &pdev->dev; 1939bf85fbcSTomer Maimon 1949bf85fbcSTomer Maimon indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info)); 1959bf85fbcSTomer Maimon if (!indio_dev) 1969bf85fbcSTomer Maimon return -ENOMEM; 1979bf85fbcSTomer Maimon info = iio_priv(indio_dev); 1989bf85fbcSTomer Maimon 199*08dfc6f8SSergiu Cuciurean mutex_init(&info->lock); 200*08dfc6f8SSergiu Cuciurean 2019bf85fbcSTomer Maimon info->dev = &pdev->dev; 2029bf85fbcSTomer Maimon 20308cf48c7SJonathan Cameron info->regs = devm_platform_ioremap_resource(pdev, 0); 2049bf85fbcSTomer Maimon if (IS_ERR(info->regs)) 2059bf85fbcSTomer Maimon return PTR_ERR(info->regs); 2069bf85fbcSTomer Maimon 207db6bcb8cSTomer Maimon info->reset = devm_reset_control_get(&pdev->dev, NULL); 208db6bcb8cSTomer Maimon if (IS_ERR(info->reset)) 209db6bcb8cSTomer Maimon return PTR_ERR(info->reset); 210db6bcb8cSTomer Maimon 2119bf85fbcSTomer Maimon info->adc_clk = devm_clk_get(&pdev->dev, NULL); 2129bf85fbcSTomer Maimon if (IS_ERR(info->adc_clk)) { 2139bf85fbcSTomer Maimon dev_warn(&pdev->dev, "ADC clock failed: can't read clk\n"); 2149bf85fbcSTomer Maimon return PTR_ERR(info->adc_clk); 2159bf85fbcSTomer Maimon } 2169bf85fbcSTomer Maimon 2179bf85fbcSTomer Maimon /* calculate ADC clock sample rate */ 2189bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2199bf85fbcSTomer Maimon div = reg_con & NPCM_ADCCON_DIV_MASK; 2209bf85fbcSTomer Maimon div = div >> NPCM_ADCCON_DIV_SHIFT; 2219bf85fbcSTomer Maimon info->adc_sample_hz = clk_get_rate(info->adc_clk) / ((div + 1) * 2); 2229bf85fbcSTomer Maimon 2239bf85fbcSTomer Maimon irq = platform_get_irq(pdev, 0); 2249bf85fbcSTomer Maimon if (irq <= 0) { 2259bf85fbcSTomer Maimon ret = -EINVAL; 2269bf85fbcSTomer Maimon goto err_disable_clk; 2279bf85fbcSTomer Maimon } 2289bf85fbcSTomer Maimon 2299bf85fbcSTomer Maimon ret = devm_request_irq(&pdev->dev, irq, npcm_adc_isr, 0, 2309bf85fbcSTomer Maimon "NPCM_ADC", indio_dev); 2319bf85fbcSTomer Maimon if (ret < 0) { 2329bf85fbcSTomer Maimon dev_err(dev, "failed requesting interrupt\n"); 2339bf85fbcSTomer Maimon goto err_disable_clk; 2349bf85fbcSTomer Maimon } 2359bf85fbcSTomer Maimon 2369bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2379bf85fbcSTomer Maimon info->vref = devm_regulator_get_optional(&pdev->dev, "vref"); 2389bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) { 2399bf85fbcSTomer Maimon ret = regulator_enable(info->vref); 2409bf85fbcSTomer Maimon if (ret) { 2419bf85fbcSTomer Maimon dev_err(&pdev->dev, "Can't enable ADC reference voltage\n"); 2429bf85fbcSTomer Maimon goto err_disable_clk; 2439bf85fbcSTomer Maimon } 2449bf85fbcSTomer Maimon 2459bf85fbcSTomer Maimon iowrite32(reg_con & ~NPCM_ADCCON_REFSEL, 2469bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 2479bf85fbcSTomer Maimon } else { 2489bf85fbcSTomer Maimon /* 2499bf85fbcSTomer Maimon * Any error which is not ENODEV indicates the regulator 2509bf85fbcSTomer Maimon * has been specified and so is a failure case. 2519bf85fbcSTomer Maimon */ 2529bf85fbcSTomer Maimon if (PTR_ERR(info->vref) != -ENODEV) { 2539bf85fbcSTomer Maimon ret = PTR_ERR(info->vref); 2549bf85fbcSTomer Maimon goto err_disable_clk; 2559bf85fbcSTomer Maimon } 2569bf85fbcSTomer Maimon 2579bf85fbcSTomer Maimon /* Use internal reference */ 2589bf85fbcSTomer Maimon iowrite32(reg_con | NPCM_ADCCON_REFSEL, 2599bf85fbcSTomer Maimon info->regs + NPCM_ADCCON); 2609bf85fbcSTomer Maimon } 2619bf85fbcSTomer Maimon 2629bf85fbcSTomer Maimon init_waitqueue_head(&info->wq); 2639bf85fbcSTomer Maimon 2649bf85fbcSTomer Maimon reg_con = ioread32(info->regs + NPCM_ADCCON); 2659bf85fbcSTomer Maimon reg_con |= NPCM_ADC_ENABLE; 2669bf85fbcSTomer Maimon 2679bf85fbcSTomer Maimon /* Enable the ADC Module */ 2689bf85fbcSTomer Maimon iowrite32(reg_con, info->regs + NPCM_ADCCON); 2699bf85fbcSTomer Maimon 2709bf85fbcSTomer Maimon /* Start ADC conversion */ 2719bf85fbcSTomer Maimon iowrite32(reg_con | NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON); 2729bf85fbcSTomer Maimon 2739bf85fbcSTomer Maimon platform_set_drvdata(pdev, indio_dev); 2749bf85fbcSTomer Maimon indio_dev->name = dev_name(&pdev->dev); 2759bf85fbcSTomer Maimon indio_dev->info = &npcm_adc_iio_info; 2769bf85fbcSTomer Maimon indio_dev->modes = INDIO_DIRECT_MODE; 2779bf85fbcSTomer Maimon indio_dev->channels = npcm_adc_iio_channels; 2789bf85fbcSTomer Maimon indio_dev->num_channels = ARRAY_SIZE(npcm_adc_iio_channels); 2799bf85fbcSTomer Maimon 2809bf85fbcSTomer Maimon ret = iio_device_register(indio_dev); 2819bf85fbcSTomer Maimon if (ret) { 2829bf85fbcSTomer Maimon dev_err(&pdev->dev, "Couldn't register the device.\n"); 2839bf85fbcSTomer Maimon goto err_iio_register; 2849bf85fbcSTomer Maimon } 2859bf85fbcSTomer Maimon 2869bf85fbcSTomer Maimon pr_info("NPCM ADC driver probed\n"); 2879bf85fbcSTomer Maimon 2889bf85fbcSTomer Maimon return 0; 2899bf85fbcSTomer Maimon 2909bf85fbcSTomer Maimon err_iio_register: 2919bf85fbcSTomer Maimon iowrite32(reg_con & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON); 2929bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) 2939bf85fbcSTomer Maimon regulator_disable(info->vref); 2949bf85fbcSTomer Maimon err_disable_clk: 2959bf85fbcSTomer Maimon clk_disable_unprepare(info->adc_clk); 2969bf85fbcSTomer Maimon 2979bf85fbcSTomer Maimon return ret; 2989bf85fbcSTomer Maimon } 2999bf85fbcSTomer Maimon 3009bf85fbcSTomer Maimon static int npcm_adc_remove(struct platform_device *pdev) 3019bf85fbcSTomer Maimon { 3029bf85fbcSTomer Maimon struct iio_dev *indio_dev = platform_get_drvdata(pdev); 3039bf85fbcSTomer Maimon struct npcm_adc *info = iio_priv(indio_dev); 3049bf85fbcSTomer Maimon u32 regtemp; 3059bf85fbcSTomer Maimon 3069bf85fbcSTomer Maimon iio_device_unregister(indio_dev); 3079bf85fbcSTomer Maimon 3089bf85fbcSTomer Maimon regtemp = ioread32(info->regs + NPCM_ADCCON); 3099bf85fbcSTomer Maimon iowrite32(regtemp & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON); 3109bf85fbcSTomer Maimon if (!IS_ERR(info->vref)) 3119bf85fbcSTomer Maimon regulator_disable(info->vref); 3129bf85fbcSTomer Maimon clk_disable_unprepare(info->adc_clk); 3139bf85fbcSTomer Maimon 3149bf85fbcSTomer Maimon return 0; 3159bf85fbcSTomer Maimon } 3169bf85fbcSTomer Maimon 3179bf85fbcSTomer Maimon static struct platform_driver npcm_adc_driver = { 3189bf85fbcSTomer Maimon .probe = npcm_adc_probe, 3199bf85fbcSTomer Maimon .remove = npcm_adc_remove, 3209bf85fbcSTomer Maimon .driver = { 3219bf85fbcSTomer Maimon .name = "npcm_adc", 3229bf85fbcSTomer Maimon .of_match_table = npcm_adc_match, 3239bf85fbcSTomer Maimon }, 3249bf85fbcSTomer Maimon }; 3259bf85fbcSTomer Maimon 3269bf85fbcSTomer Maimon module_platform_driver(npcm_adc_driver); 3279bf85fbcSTomer Maimon 3289bf85fbcSTomer Maimon MODULE_DESCRIPTION("Nuvoton NPCM ADC Driver"); 3299bf85fbcSTomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); 3309bf85fbcSTomer Maimon MODULE_LICENSE("GPL v2"); 331