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