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