1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 28614206aSAlexander Sverdlin /* 38614206aSAlexander Sverdlin * Driver for ADC module on the Cirrus Logic EP93xx series of SoCs 48614206aSAlexander Sverdlin * 58614206aSAlexander Sverdlin * Copyright (C) 2015 Alexander Sverdlin 68614206aSAlexander Sverdlin * 78614206aSAlexander Sverdlin * The driver uses polling to get the conversion status. According to EP93xx 88614206aSAlexander Sverdlin * datasheets, reading ADCResult register starts the conversion, but user is also 98614206aSAlexander Sverdlin * responsible for ensuring that delay between adjacent conversion triggers is 108614206aSAlexander Sverdlin * long enough so that maximum allowed conversion rate is not exceeded. This 118614206aSAlexander Sverdlin * basically renders IRQ mode unusable. 128614206aSAlexander Sverdlin */ 138614206aSAlexander Sverdlin 148614206aSAlexander Sverdlin #include <linux/clk.h> 158614206aSAlexander Sverdlin #include <linux/delay.h> 168614206aSAlexander Sverdlin #include <linux/device.h> 178614206aSAlexander Sverdlin #include <linux/err.h> 188614206aSAlexander Sverdlin #include <linux/iio/iio.h> 198614206aSAlexander Sverdlin #include <linux/io.h> 208614206aSAlexander Sverdlin #include <linux/irqflags.h> 218614206aSAlexander Sverdlin #include <linux/module.h> 228614206aSAlexander Sverdlin #include <linux/mutex.h> 238614206aSAlexander Sverdlin #include <linux/platform_device.h> 248614206aSAlexander Sverdlin 258614206aSAlexander Sverdlin /* 268614206aSAlexander Sverdlin * This code could benefit from real HR Timers, but jiffy granularity would 278614206aSAlexander Sverdlin * lower ADC conversion rate down to CONFIG_HZ, so we fallback to busy wait 288614206aSAlexander Sverdlin * in such case. 298614206aSAlexander Sverdlin * 308614206aSAlexander Sverdlin * HR Timers-based version loads CPU only up to 10% during back to back ADC 318614206aSAlexander Sverdlin * conversion, while busy wait-based version consumes whole CPU power. 328614206aSAlexander Sverdlin */ 338614206aSAlexander Sverdlin #ifdef CONFIG_HIGH_RES_TIMERS 348614206aSAlexander Sverdlin #define ep93xx_adc_delay(usmin, usmax) usleep_range(usmin, usmax) 358614206aSAlexander Sverdlin #else 368614206aSAlexander Sverdlin #define ep93xx_adc_delay(usmin, usmax) udelay(usmin) 378614206aSAlexander Sverdlin #endif 388614206aSAlexander Sverdlin 398614206aSAlexander Sverdlin #define EP93XX_ADC_RESULT 0x08 408614206aSAlexander Sverdlin #define EP93XX_ADC_SDR BIT(31) 418614206aSAlexander Sverdlin #define EP93XX_ADC_SWITCH 0x18 428614206aSAlexander Sverdlin #define EP93XX_ADC_SW_LOCK 0x20 438614206aSAlexander Sverdlin 448614206aSAlexander Sverdlin struct ep93xx_adc_priv { 458614206aSAlexander Sverdlin struct clk *clk; 468614206aSAlexander Sverdlin void __iomem *base; 478614206aSAlexander Sverdlin int lastch; 488614206aSAlexander Sverdlin struct mutex lock; 498614206aSAlexander Sverdlin }; 508614206aSAlexander Sverdlin 518614206aSAlexander Sverdlin #define EP93XX_ADC_CH(index, dname, swcfg) { \ 528614206aSAlexander Sverdlin .type = IIO_VOLTAGE, \ 538614206aSAlexander Sverdlin .indexed = 1, \ 548614206aSAlexander Sverdlin .channel = index, \ 558614206aSAlexander Sverdlin .address = swcfg, \ 568614206aSAlexander Sverdlin .datasheet_name = dname, \ 578614206aSAlexander Sverdlin .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 588614206aSAlexander Sverdlin .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) | \ 598614206aSAlexander Sverdlin BIT(IIO_CHAN_INFO_OFFSET), \ 608614206aSAlexander Sverdlin } 618614206aSAlexander Sverdlin 628614206aSAlexander Sverdlin /* 638614206aSAlexander Sverdlin * Numbering scheme for channels 0..4 is defined in EP9301 and EP9302 datasheets. 648614206aSAlexander Sverdlin * EP9307, EP9312 and EP9312 have 3 channels more (total 8), but the numbering is 658614206aSAlexander Sverdlin * not defined. So the last three are numbered randomly, let's say. 668614206aSAlexander Sverdlin */ 678614206aSAlexander Sverdlin static const struct iio_chan_spec ep93xx_adc_channels[8] = { 688614206aSAlexander Sverdlin EP93XX_ADC_CH(0, "YM", 0x608), 698614206aSAlexander Sverdlin EP93XX_ADC_CH(1, "SXP", 0x680), 708614206aSAlexander Sverdlin EP93XX_ADC_CH(2, "SXM", 0x640), 718614206aSAlexander Sverdlin EP93XX_ADC_CH(3, "SYP", 0x620), 728614206aSAlexander Sverdlin EP93XX_ADC_CH(4, "SYM", 0x610), 738614206aSAlexander Sverdlin EP93XX_ADC_CH(5, "XP", 0x601), 748614206aSAlexander Sverdlin EP93XX_ADC_CH(6, "XM", 0x602), 758614206aSAlexander Sverdlin EP93XX_ADC_CH(7, "YP", 0x604), 768614206aSAlexander Sverdlin }; 778614206aSAlexander Sverdlin 788614206aSAlexander Sverdlin static int ep93xx_read_raw(struct iio_dev *iiodev, 798614206aSAlexander Sverdlin struct iio_chan_spec const *channel, int *value, 808614206aSAlexander Sverdlin int *shift, long mask) 818614206aSAlexander Sverdlin { 828614206aSAlexander Sverdlin struct ep93xx_adc_priv *priv = iio_priv(iiodev); 838614206aSAlexander Sverdlin unsigned long timeout; 848614206aSAlexander Sverdlin int ret; 858614206aSAlexander Sverdlin 868614206aSAlexander Sverdlin switch (mask) { 878614206aSAlexander Sverdlin case IIO_CHAN_INFO_RAW: 888614206aSAlexander Sverdlin mutex_lock(&priv->lock); 898614206aSAlexander Sverdlin if (priv->lastch != channel->channel) { 908614206aSAlexander Sverdlin priv->lastch = channel->channel; 918614206aSAlexander Sverdlin /* 928614206aSAlexander Sverdlin * Switch register is software-locked, unlocking must be 938614206aSAlexander Sverdlin * immediately followed by write 948614206aSAlexander Sverdlin */ 958614206aSAlexander Sverdlin local_irq_disable(); 968614206aSAlexander Sverdlin writel_relaxed(0xAA, priv->base + EP93XX_ADC_SW_LOCK); 978614206aSAlexander Sverdlin writel_relaxed(channel->address, 988614206aSAlexander Sverdlin priv->base + EP93XX_ADC_SWITCH); 998614206aSAlexander Sverdlin local_irq_enable(); 1008614206aSAlexander Sverdlin /* 1018614206aSAlexander Sverdlin * Settling delay depends on module clock and could be 1028614206aSAlexander Sverdlin * 2ms or 500us 1038614206aSAlexander Sverdlin */ 1048614206aSAlexander Sverdlin ep93xx_adc_delay(2000, 2000); 1058614206aSAlexander Sverdlin } 1068614206aSAlexander Sverdlin /* Start the conversion, eventually discarding old result */ 1078614206aSAlexander Sverdlin readl_relaxed(priv->base + EP93XX_ADC_RESULT); 1088614206aSAlexander Sverdlin /* Ensure maximum conversion rate is not exceeded */ 1098614206aSAlexander Sverdlin ep93xx_adc_delay(DIV_ROUND_UP(1000000, 925), 1108614206aSAlexander Sverdlin DIV_ROUND_UP(1000000, 925)); 1118614206aSAlexander Sverdlin /* At this point conversion must be completed, but anyway... */ 1128614206aSAlexander Sverdlin ret = IIO_VAL_INT; 1138614206aSAlexander Sverdlin timeout = jiffies + msecs_to_jiffies(1) + 1; 1148614206aSAlexander Sverdlin while (1) { 1158614206aSAlexander Sverdlin u32 t; 1168614206aSAlexander Sverdlin 1178614206aSAlexander Sverdlin t = readl_relaxed(priv->base + EP93XX_ADC_RESULT); 1188614206aSAlexander Sverdlin if (t & EP93XX_ADC_SDR) { 1198614206aSAlexander Sverdlin *value = sign_extend32(t, 15); 1208614206aSAlexander Sverdlin break; 1218614206aSAlexander Sverdlin } 1228614206aSAlexander Sverdlin 1238614206aSAlexander Sverdlin if (time_after(jiffies, timeout)) { 1248614206aSAlexander Sverdlin dev_err(&iiodev->dev, "Conversion timeout\n"); 1258614206aSAlexander Sverdlin ret = -ETIMEDOUT; 1268614206aSAlexander Sverdlin break; 1278614206aSAlexander Sverdlin } 1288614206aSAlexander Sverdlin 1298614206aSAlexander Sverdlin cpu_relax(); 1308614206aSAlexander Sverdlin } 1318614206aSAlexander Sverdlin mutex_unlock(&priv->lock); 1328614206aSAlexander Sverdlin return ret; 1338614206aSAlexander Sverdlin 1348614206aSAlexander Sverdlin case IIO_CHAN_INFO_OFFSET: 1358614206aSAlexander Sverdlin /* According to datasheet, range is -25000..25000 */ 1368614206aSAlexander Sverdlin *value = 25000; 1378614206aSAlexander Sverdlin return IIO_VAL_INT; 1388614206aSAlexander Sverdlin 1398614206aSAlexander Sverdlin case IIO_CHAN_INFO_SCALE: 1408614206aSAlexander Sverdlin /* Typical supply voltage is 3.3v */ 1418614206aSAlexander Sverdlin *value = (1ULL << 32) * 3300 / 50000; 1428614206aSAlexander Sverdlin *shift = 32; 1438614206aSAlexander Sverdlin return IIO_VAL_FRACTIONAL_LOG2; 1448614206aSAlexander Sverdlin } 1458614206aSAlexander Sverdlin 1468614206aSAlexander Sverdlin return -EINVAL; 1478614206aSAlexander Sverdlin } 1488614206aSAlexander Sverdlin 1498614206aSAlexander Sverdlin static const struct iio_info ep93xx_adc_info = { 1508614206aSAlexander Sverdlin .read_raw = ep93xx_read_raw, 1518614206aSAlexander Sverdlin }; 1528614206aSAlexander Sverdlin 1538614206aSAlexander Sverdlin static int ep93xx_adc_probe(struct platform_device *pdev) 1548614206aSAlexander Sverdlin { 1558614206aSAlexander Sverdlin int ret; 1568614206aSAlexander Sverdlin struct iio_dev *iiodev; 1578614206aSAlexander Sverdlin struct ep93xx_adc_priv *priv; 1588614206aSAlexander Sverdlin struct clk *pclk; 1598614206aSAlexander Sverdlin struct resource *res; 1608614206aSAlexander Sverdlin 1618614206aSAlexander Sverdlin iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv)); 1628614206aSAlexander Sverdlin if (!iiodev) 1638614206aSAlexander Sverdlin return -ENOMEM; 1648614206aSAlexander Sverdlin priv = iio_priv(iiodev); 1658614206aSAlexander Sverdlin 1668614206aSAlexander Sverdlin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1678614206aSAlexander Sverdlin priv->base = devm_ioremap_resource(&pdev->dev, res); 1688614206aSAlexander Sverdlin if (IS_ERR(priv->base)) { 1698614206aSAlexander Sverdlin dev_err(&pdev->dev, "Cannot map memory resource\n"); 1708614206aSAlexander Sverdlin return PTR_ERR(priv->base); 1718614206aSAlexander Sverdlin } 1728614206aSAlexander Sverdlin 1738614206aSAlexander Sverdlin iiodev->dev.parent = &pdev->dev; 1748614206aSAlexander Sverdlin iiodev->name = dev_name(&pdev->dev); 1758614206aSAlexander Sverdlin iiodev->modes = INDIO_DIRECT_MODE; 1768614206aSAlexander Sverdlin iiodev->info = &ep93xx_adc_info; 1778614206aSAlexander Sverdlin iiodev->num_channels = ARRAY_SIZE(ep93xx_adc_channels); 1788614206aSAlexander Sverdlin iiodev->channels = ep93xx_adc_channels; 1798614206aSAlexander Sverdlin 1808614206aSAlexander Sverdlin priv->lastch = -1; 1818614206aSAlexander Sverdlin mutex_init(&priv->lock); 1828614206aSAlexander Sverdlin 1838614206aSAlexander Sverdlin platform_set_drvdata(pdev, iiodev); 1848614206aSAlexander Sverdlin 1858614206aSAlexander Sverdlin priv->clk = devm_clk_get(&pdev->dev, NULL); 1868614206aSAlexander Sverdlin if (IS_ERR(priv->clk)) { 1878614206aSAlexander Sverdlin dev_err(&pdev->dev, "Cannot obtain clock\n"); 1888614206aSAlexander Sverdlin return PTR_ERR(priv->clk); 1898614206aSAlexander Sverdlin } 1908614206aSAlexander Sverdlin 1918614206aSAlexander Sverdlin pclk = clk_get_parent(priv->clk); 1928614206aSAlexander Sverdlin if (!pclk) { 1938614206aSAlexander Sverdlin dev_warn(&pdev->dev, "Cannot obtain parent clock\n"); 1948614206aSAlexander Sverdlin } else { 1958614206aSAlexander Sverdlin /* 1968614206aSAlexander Sverdlin * This is actually a place for improvement: 1978614206aSAlexander Sverdlin * EP93xx ADC supports two clock divisors -- 4 and 16, 1988614206aSAlexander Sverdlin * resulting in conversion rates 3750 and 925 samples per second 1998614206aSAlexander Sverdlin * with 500us or 2ms settling time respectively. 2008614206aSAlexander Sverdlin * One might find this interesting enough to be configurable. 2018614206aSAlexander Sverdlin */ 2028614206aSAlexander Sverdlin ret = clk_set_rate(priv->clk, clk_get_rate(pclk) / 16); 2038614206aSAlexander Sverdlin if (ret) 2048614206aSAlexander Sverdlin dev_warn(&pdev->dev, "Cannot set clock rate\n"); 2058614206aSAlexander Sverdlin /* 2068614206aSAlexander Sverdlin * We can tolerate rate setting failure because the module should 2078614206aSAlexander Sverdlin * work in any case. 2088614206aSAlexander Sverdlin */ 2098614206aSAlexander Sverdlin } 2108614206aSAlexander Sverdlin 2118614206aSAlexander Sverdlin ret = clk_enable(priv->clk); 2128614206aSAlexander Sverdlin if (ret) { 2138614206aSAlexander Sverdlin dev_err(&pdev->dev, "Cannot enable clock\n"); 2148614206aSAlexander Sverdlin return ret; 2158614206aSAlexander Sverdlin } 2168614206aSAlexander Sverdlin 2178614206aSAlexander Sverdlin ret = iio_device_register(iiodev); 2188614206aSAlexander Sverdlin if (ret) 2198614206aSAlexander Sverdlin clk_disable(priv->clk); 2208614206aSAlexander Sverdlin 2218614206aSAlexander Sverdlin return ret; 2228614206aSAlexander Sverdlin } 2238614206aSAlexander Sverdlin 2248614206aSAlexander Sverdlin static int ep93xx_adc_remove(struct platform_device *pdev) 2258614206aSAlexander Sverdlin { 2268614206aSAlexander Sverdlin struct iio_dev *iiodev = platform_get_drvdata(pdev); 2278614206aSAlexander Sverdlin struct ep93xx_adc_priv *priv = iio_priv(iiodev); 2288614206aSAlexander Sverdlin 2298614206aSAlexander Sverdlin iio_device_unregister(iiodev); 2308614206aSAlexander Sverdlin clk_disable(priv->clk); 2318614206aSAlexander Sverdlin 2328614206aSAlexander Sverdlin return 0; 2338614206aSAlexander Sverdlin } 2348614206aSAlexander Sverdlin 2358614206aSAlexander Sverdlin static struct platform_driver ep93xx_adc_driver = { 2368614206aSAlexander Sverdlin .driver = { 2378614206aSAlexander Sverdlin .name = "ep93xx-adc", 2388614206aSAlexander Sverdlin }, 2398614206aSAlexander Sverdlin .probe = ep93xx_adc_probe, 2408614206aSAlexander Sverdlin .remove = ep93xx_adc_remove, 2418614206aSAlexander Sverdlin }; 2428614206aSAlexander Sverdlin module_platform_driver(ep93xx_adc_driver); 2438614206aSAlexander Sverdlin 2448614206aSAlexander Sverdlin MODULE_AUTHOR("Alexander Sverdlin <alexander.sverdlin@gmail.com>"); 2458614206aSAlexander Sverdlin MODULE_DESCRIPTION("Cirrus Logic EP93XX ADC driver"); 2468614206aSAlexander Sverdlin MODULE_LICENSE("GPL"); 2478614206aSAlexander Sverdlin MODULE_ALIAS("platform:ep93xx-adc"); 248