linux/drivers/iio/adc/ep93xx_adc.c
<<
>>
Prefs
   1/*
   2 * Driver for ADC module on the Cirrus Logic EP93xx series of SoCs
   3 *
   4 * Copyright (C) 2015 Alexander Sverdlin
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 * The driver uses polling to get the conversion status. According to EP93xx
  11 * datasheets, reading ADCResult register starts the conversion, but user is also
  12 * responsible for ensuring that delay between adjacent conversion triggers is
  13 * long enough so that maximum allowed conversion rate is not exceeded. This
  14 * basically renders IRQ mode unusable.
  15 */
  16
  17#include <linux/clk.h>
  18#include <linux/delay.h>
  19#include <linux/device.h>
  20#include <linux/err.h>
  21#include <linux/iio/iio.h>
  22#include <linux/io.h>
  23#include <linux/irqflags.h>
  24#include <linux/module.h>
  25#include <linux/mutex.h>
  26#include <linux/platform_device.h>
  27
  28/*
  29 * This code could benefit from real HR Timers, but jiffy granularity would
  30 * lower ADC conversion rate down to CONFIG_HZ, so we fallback to busy wait
  31 * in such case.
  32 *
  33 * HR Timers-based version loads CPU only up to 10% during back to back ADC
  34 * conversion, while busy wait-based version consumes whole CPU power.
  35 */
  36#ifdef CONFIG_HIGH_RES_TIMERS
  37#define ep93xx_adc_delay(usmin, usmax) usleep_range(usmin, usmax)
  38#else
  39#define ep93xx_adc_delay(usmin, usmax) udelay(usmin)
  40#endif
  41
  42#define EP93XX_ADC_RESULT       0x08
  43#define   EP93XX_ADC_SDR        BIT(31)
  44#define EP93XX_ADC_SWITCH       0x18
  45#define EP93XX_ADC_SW_LOCK      0x20
  46
  47struct ep93xx_adc_priv {
  48        struct clk *clk;
  49        void __iomem *base;
  50        int lastch;
  51        struct mutex lock;
  52};
  53
  54#define EP93XX_ADC_CH(index, dname, swcfg) {                    \
  55        .type = IIO_VOLTAGE,                                    \
  56        .indexed = 1,                                           \
  57        .channel = index,                                       \
  58        .address = swcfg,                                       \
  59        .datasheet_name = dname,                                \
  60        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
  61        .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) |   \
  62                                   BIT(IIO_CHAN_INFO_OFFSET),   \
  63}
  64
  65/*
  66 * Numbering scheme for channels 0..4 is defined in EP9301 and EP9302 datasheets.
  67 * EP9307, EP9312 and EP9312 have 3 channels more (total 8), but the numbering is
  68 * not defined. So the last three are numbered randomly, let's say.
  69 */
  70static const struct iio_chan_spec ep93xx_adc_channels[8] = {
  71        EP93XX_ADC_CH(0, "YM",  0x608),
  72        EP93XX_ADC_CH(1, "SXP", 0x680),
  73        EP93XX_ADC_CH(2, "SXM", 0x640),
  74        EP93XX_ADC_CH(3, "SYP", 0x620),
  75        EP93XX_ADC_CH(4, "SYM", 0x610),
  76        EP93XX_ADC_CH(5, "XP",  0x601),
  77        EP93XX_ADC_CH(6, "XM",  0x602),
  78        EP93XX_ADC_CH(7, "YP",  0x604),
  79};
  80
  81static int ep93xx_read_raw(struct iio_dev *iiodev,
  82                           struct iio_chan_spec const *channel, int *value,
  83                           int *shift, long mask)
  84{
  85        struct ep93xx_adc_priv *priv = iio_priv(iiodev);
  86        unsigned long timeout;
  87        int ret;
  88
  89        switch (mask) {
  90        case IIO_CHAN_INFO_RAW:
  91                mutex_lock(&priv->lock);
  92                if (priv->lastch != channel->channel) {
  93                        priv->lastch = channel->channel;
  94                        /*
  95                         * Switch register is software-locked, unlocking must be
  96                         * immediately followed by write
  97                         */
  98                        local_irq_disable();
  99                        writel_relaxed(0xAA, priv->base + EP93XX_ADC_SW_LOCK);
 100                        writel_relaxed(channel->address,
 101                                       priv->base + EP93XX_ADC_SWITCH);
 102                        local_irq_enable();
 103                        /*
 104                         * Settling delay depends on module clock and could be
 105                         * 2ms or 500us
 106                         */
 107                        ep93xx_adc_delay(2000, 2000);
 108                }
 109                /* Start the conversion, eventually discarding old result */
 110                readl_relaxed(priv->base + EP93XX_ADC_RESULT);
 111                /* Ensure maximum conversion rate is not exceeded */
 112                ep93xx_adc_delay(DIV_ROUND_UP(1000000, 925),
 113                                 DIV_ROUND_UP(1000000, 925));
 114                /* At this point conversion must be completed, but anyway... */
 115                ret = IIO_VAL_INT;
 116                timeout = jiffies + msecs_to_jiffies(1) + 1;
 117                while (1) {
 118                        u32 t;
 119
 120                        t = readl_relaxed(priv->base + EP93XX_ADC_RESULT);
 121                        if (t & EP93XX_ADC_SDR) {
 122                                *value = sign_extend32(t, 15);
 123                                break;
 124                        }
 125
 126                        if (time_after(jiffies, timeout)) {
 127                                dev_err(&iiodev->dev, "Conversion timeout\n");
 128                                ret = -ETIMEDOUT;
 129                                break;
 130                        }
 131
 132                        cpu_relax();
 133                }
 134                mutex_unlock(&priv->lock);
 135                return ret;
 136
 137        case IIO_CHAN_INFO_OFFSET:
 138                /* According to datasheet, range is -25000..25000 */
 139                *value = 25000;
 140                return IIO_VAL_INT;
 141
 142        case IIO_CHAN_INFO_SCALE:
 143                /* Typical supply voltage is 3.3v */
 144                *value = (1ULL << 32) * 3300 / 50000;
 145                *shift = 32;
 146                return IIO_VAL_FRACTIONAL_LOG2;
 147        }
 148
 149        return -EINVAL;
 150}
 151
 152static const struct iio_info ep93xx_adc_info = {
 153        .driver_module = THIS_MODULE,
 154        .read_raw = ep93xx_read_raw,
 155};
 156
 157static int ep93xx_adc_probe(struct platform_device *pdev)
 158{
 159        int ret;
 160        struct iio_dev *iiodev;
 161        struct ep93xx_adc_priv *priv;
 162        struct clk *pclk;
 163        struct resource *res;
 164
 165        iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv));
 166        if (!iiodev)
 167                return -ENOMEM;
 168        priv = iio_priv(iiodev);
 169
 170        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 171        if (!res) {
 172                dev_err(&pdev->dev, "Cannot obtain memory resource\n");
 173                return -ENXIO;
 174        }
 175        priv->base = devm_ioremap_resource(&pdev->dev, res);
 176        if (IS_ERR(priv->base)) {
 177                dev_err(&pdev->dev, "Cannot map memory resource\n");
 178                return PTR_ERR(priv->base);
 179        }
 180
 181        iiodev->dev.parent = &pdev->dev;
 182        iiodev->name = dev_name(&pdev->dev);
 183        iiodev->modes = INDIO_DIRECT_MODE;
 184        iiodev->info = &ep93xx_adc_info;
 185        iiodev->num_channels = ARRAY_SIZE(ep93xx_adc_channels);
 186        iiodev->channels = ep93xx_adc_channels;
 187
 188        priv->lastch = -1;
 189        mutex_init(&priv->lock);
 190
 191        platform_set_drvdata(pdev, iiodev);
 192
 193        priv->clk = devm_clk_get(&pdev->dev, NULL);
 194        if (IS_ERR(priv->clk)) {
 195                dev_err(&pdev->dev, "Cannot obtain clock\n");
 196                return PTR_ERR(priv->clk);
 197        }
 198
 199        pclk = clk_get_parent(priv->clk);
 200        if (!pclk) {
 201                dev_warn(&pdev->dev, "Cannot obtain parent clock\n");
 202        } else {
 203                /*
 204                 * This is actually a place for improvement:
 205                 * EP93xx ADC supports two clock divisors -- 4 and 16,
 206                 * resulting in conversion rates 3750 and 925 samples per second
 207                 * with 500us or 2ms settling time respectively.
 208                 * One might find this interesting enough to be configurable.
 209                 */
 210                ret = clk_set_rate(priv->clk, clk_get_rate(pclk) / 16);
 211                if (ret)
 212                        dev_warn(&pdev->dev, "Cannot set clock rate\n");
 213                /*
 214                 * We can tolerate rate setting failure because the module should
 215                 * work in any case.
 216                 */
 217        }
 218
 219        ret = clk_enable(priv->clk);
 220        if (ret) {
 221                dev_err(&pdev->dev, "Cannot enable clock\n");
 222                return ret;
 223        }
 224
 225        ret = iio_device_register(iiodev);
 226        if (ret)
 227                clk_disable(priv->clk);
 228
 229        return ret;
 230}
 231
 232static int ep93xx_adc_remove(struct platform_device *pdev)
 233{
 234        struct iio_dev *iiodev = platform_get_drvdata(pdev);
 235        struct ep93xx_adc_priv *priv = iio_priv(iiodev);
 236
 237        iio_device_unregister(iiodev);
 238        clk_disable(priv->clk);
 239
 240        return 0;
 241}
 242
 243static struct platform_driver ep93xx_adc_driver = {
 244        .driver = {
 245                .name = "ep93xx-adc",
 246        },
 247        .probe = ep93xx_adc_probe,
 248        .remove = ep93xx_adc_remove,
 249};
 250module_platform_driver(ep93xx_adc_driver);
 251
 252MODULE_AUTHOR("Alexander Sverdlin <alexander.sverdlin@gmail.com>");
 253MODULE_DESCRIPTION("Cirrus Logic EP93XX ADC driver");
 254MODULE_LICENSE("GPL");
 255MODULE_ALIAS("platform:ep93xx-adc");
 256