2019-02-08 23:09:42 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-02-15 01:44:56 +07:00
|
|
|
/*
|
|
|
|
* lpc32xx_adc.c - Support for ADC in LPC32XX
|
|
|
|
*
|
|
|
|
* 3-channel, 10-bit ADC
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011, 2012 Roland Stigge <stigge@antcom.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/completion.h>
|
2019-03-15 16:52:28 +07:00
|
|
|
#include <linux/err.h>
|
2012-04-25 21:54:58 +07:00
|
|
|
#include <linux/iio/iio.h>
|
2019-03-15 16:52:28 +07:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
2019-03-15 16:52:30 +07:00
|
|
|
#include <linux/regulator/consumer.h>
|
2012-02-15 01:44:56 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LPC32XX registers definitions
|
|
|
|
*/
|
2017-02-05 20:06:58 +07:00
|
|
|
#define LPC32XXAD_SELECT(x) ((x) + 0x04)
|
|
|
|
#define LPC32XXAD_CTRL(x) ((x) + 0x08)
|
|
|
|
#define LPC32XXAD_VALUE(x) ((x) + 0x48)
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:58 +07:00
|
|
|
/* Bit definitions for LPC32XXAD_SELECT: */
|
|
|
|
/* constant, always write this value! */
|
|
|
|
#define LPC32XXAD_REFm 0x00000200
|
|
|
|
/* constant, always write this value! */
|
|
|
|
#define LPC32XXAD_REFp 0x00000080
|
|
|
|
/* multiple of this is the channel number: 0, 1, 2 */
|
|
|
|
#define LPC32XXAD_IN 0x00000010
|
|
|
|
/* constant, always write this value! */
|
|
|
|
#define LPC32XXAD_INTERNAL 0x00000004
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:58 +07:00
|
|
|
/* Bit definitions for LPC32XXAD_CTRL: */
|
|
|
|
#define LPC32XXAD_STROBE 0x00000002
|
|
|
|
#define LPC32XXAD_PDN_CTRL 0x00000004
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:58 +07:00
|
|
|
/* Bit definitions for LPC32XXAD_VALUE: */
|
|
|
|
#define LPC32XXAD_VALUE_MASK 0x000003FF
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:58 +07:00
|
|
|
#define LPC32XXAD_NAME "lpc32xx-adc"
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
struct lpc32xx_adc_state {
|
2012-02-15 01:44:56 +07:00
|
|
|
void __iomem *adc_base;
|
|
|
|
struct clk *clk;
|
|
|
|
struct completion completion;
|
2019-03-15 16:52:30 +07:00
|
|
|
struct regulator *vref;
|
2012-02-15 01:44:56 +07:00
|
|
|
|
|
|
|
u32 value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int lpc32xx_read_raw(struct iio_dev *indio_dev,
|
2015-10-15 01:14:13 +07:00
|
|
|
struct iio_chan_spec const *chan,
|
|
|
|
int *val,
|
|
|
|
int *val2,
|
|
|
|
long mask)
|
2012-02-15 01:44:56 +07:00
|
|
|
{
|
2017-02-05 20:06:59 +07:00
|
|
|
struct lpc32xx_adc_state *st = iio_priv(indio_dev);
|
2017-05-30 18:05:27 +07:00
|
|
|
int ret;
|
2019-03-15 16:52:30 +07:00
|
|
|
|
|
|
|
switch (mask) {
|
|
|
|
case IIO_CHAN_INFO_RAW:
|
2012-02-15 01:44:56 +07:00
|
|
|
mutex_lock(&indio_dev->mlock);
|
2017-05-30 18:05:27 +07:00
|
|
|
ret = clk_prepare_enable(st->clk);
|
|
|
|
if (ret) {
|
|
|
|
mutex_unlock(&indio_dev->mlock);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-15 01:44:56 +07:00
|
|
|
/* Measurement setup */
|
2017-02-05 20:06:58 +07:00
|
|
|
__raw_writel(LPC32XXAD_INTERNAL | (chan->address) |
|
|
|
|
LPC32XXAD_REFp | LPC32XXAD_REFm,
|
2017-02-05 20:06:59 +07:00
|
|
|
LPC32XXAD_SELECT(st->adc_base));
|
2012-02-15 01:44:56 +07:00
|
|
|
/* Trigger conversion */
|
2017-02-05 20:06:58 +07:00
|
|
|
__raw_writel(LPC32XXAD_PDN_CTRL | LPC32XXAD_STROBE,
|
2017-02-05 20:06:59 +07:00
|
|
|
LPC32XXAD_CTRL(st->adc_base));
|
|
|
|
wait_for_completion(&st->completion); /* set by ISR */
|
|
|
|
clk_disable_unprepare(st->clk);
|
|
|
|
*val = st->value;
|
2012-02-15 01:44:56 +07:00
|
|
|
mutex_unlock(&indio_dev->mlock);
|
|
|
|
|
|
|
|
return IIO_VAL_INT;
|
|
|
|
|
2019-03-15 16:52:30 +07:00
|
|
|
case IIO_CHAN_INFO_SCALE:
|
|
|
|
*val = regulator_get_voltage(st->vref) / 1000;
|
|
|
|
*val2 = 10;
|
|
|
|
|
|
|
|
return IIO_VAL_FRACTIONAL_LOG2;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct iio_info lpc32xx_adc_iio_info = {
|
|
|
|
.read_raw = &lpc32xx_read_raw,
|
|
|
|
};
|
|
|
|
|
2019-03-15 16:52:30 +07:00
|
|
|
#define LPC32XX_ADC_CHANNEL_BASE(_index) \
|
2012-04-15 23:41:18 +07:00
|
|
|
.type = IIO_VOLTAGE, \
|
|
|
|
.indexed = 1, \
|
|
|
|
.channel = _index, \
|
2013-03-05 04:10:17 +07:00
|
|
|
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
|
2017-02-05 20:06:58 +07:00
|
|
|
.address = LPC32XXAD_IN * _index, \
|
2019-03-15 16:52:30 +07:00
|
|
|
.scan_index = _index,
|
|
|
|
|
|
|
|
#define LPC32XX_ADC_CHANNEL(_index) { \
|
|
|
|
LPC32XX_ADC_CHANNEL_BASE(_index) \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LPC32XX_ADC_SCALE_CHANNEL(_index) { \
|
|
|
|
LPC32XX_ADC_CHANNEL_BASE(_index) \
|
|
|
|
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
2012-08-09 14:51:00 +07:00
|
|
|
static const struct iio_chan_spec lpc32xx_adc_iio_channels[] = {
|
2012-02-15 01:44:56 +07:00
|
|
|
LPC32XX_ADC_CHANNEL(0),
|
|
|
|
LPC32XX_ADC_CHANNEL(1),
|
|
|
|
LPC32XX_ADC_CHANNEL(2),
|
|
|
|
};
|
|
|
|
|
2019-03-15 16:52:30 +07:00
|
|
|
static const struct iio_chan_spec lpc32xx_adc_iio_scale_channels[] = {
|
|
|
|
LPC32XX_ADC_SCALE_CHANNEL(0),
|
|
|
|
LPC32XX_ADC_SCALE_CHANNEL(1),
|
|
|
|
LPC32XX_ADC_SCALE_CHANNEL(2),
|
|
|
|
};
|
|
|
|
|
2012-02-15 01:44:56 +07:00
|
|
|
static irqreturn_t lpc32xx_adc_isr(int irq, void *dev_id)
|
|
|
|
{
|
2017-02-05 20:06:59 +07:00
|
|
|
struct lpc32xx_adc_state *st = dev_id;
|
2012-02-15 01:44:56 +07:00
|
|
|
|
|
|
|
/* Read value and clear irq */
|
2017-02-05 20:06:59 +07:00
|
|
|
st->value = __raw_readl(LPC32XXAD_VALUE(st->adc_base)) &
|
|
|
|
LPC32XXAD_VALUE_MASK;
|
|
|
|
complete(&st->completion);
|
2012-02-15 01:44:56 +07:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-11-20 01:21:57 +07:00
|
|
|
static int lpc32xx_adc_probe(struct platform_device *pdev)
|
2012-02-15 01:44:56 +07:00
|
|
|
{
|
2017-02-05 20:06:59 +07:00
|
|
|
struct lpc32xx_adc_state *st = NULL;
|
2012-02-15 01:44:56 +07:00
|
|
|
struct resource *res;
|
|
|
|
int retval = -ENODEV;
|
|
|
|
struct iio_dev *iodev = NULL;
|
|
|
|
int irq;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (!res) {
|
|
|
|
dev_err(&pdev->dev, "failed to get platform I/O memory\n");
|
2015-08-30 15:12:57 +07:00
|
|
|
return -ENXIO;
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
iodev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
|
2013-09-01 00:12:00 +07:00
|
|
|
if (!iodev)
|
|
|
|
return -ENOMEM;
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
st = iio_priv(iodev);
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
st->adc_base = devm_ioremap(&pdev->dev, res->start,
|
|
|
|
resource_size(res));
|
|
|
|
if (!st->adc_base) {
|
2012-02-15 01:44:56 +07:00
|
|
|
dev_err(&pdev->dev, "failed mapping memory\n");
|
2013-09-01 00:12:00 +07:00
|
|
|
return -EBUSY;
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
st->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(st->clk)) {
|
2012-02-15 01:44:56 +07:00
|
|
|
dev_err(&pdev->dev, "failed getting clock\n");
|
2017-02-05 20:06:59 +07:00
|
|
|
return PTR_ERR(st->clk);
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
2013-10-17 03:45:00 +07:00
|
|
|
if (irq <= 0) {
|
2012-02-15 01:44:56 +07:00
|
|
|
dev_err(&pdev->dev, "failed getting interrupt resource\n");
|
2015-08-30 15:12:57 +07:00
|
|
|
return -ENXIO;
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
2013-09-01 00:12:00 +07:00
|
|
|
retval = devm_request_irq(&pdev->dev, irq, lpc32xx_adc_isr, 0,
|
2017-02-05 20:06:59 +07:00
|
|
|
LPC32XXAD_NAME, st);
|
2012-02-15 01:44:56 +07:00
|
|
|
if (retval < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed requesting interrupt\n");
|
2013-09-01 00:12:00 +07:00
|
|
|
return retval;
|
2012-02-15 01:44:56 +07:00
|
|
|
}
|
|
|
|
|
2019-03-15 16:52:30 +07:00
|
|
|
st->vref = devm_regulator_get(&pdev->dev, "vref");
|
|
|
|
if (IS_ERR(st->vref)) {
|
|
|
|
iodev->channels = lpc32xx_adc_iio_channels;
|
|
|
|
dev_info(&pdev->dev,
|
|
|
|
"Missing vref regulator: No scaling available\n");
|
|
|
|
} else {
|
|
|
|
iodev->channels = lpc32xx_adc_iio_scale_channels;
|
|
|
|
}
|
|
|
|
|
2012-02-15 01:44:56 +07:00
|
|
|
platform_set_drvdata(pdev, iodev);
|
|
|
|
|
2017-02-05 20:06:59 +07:00
|
|
|
init_completion(&st->completion);
|
2012-02-15 01:44:56 +07:00
|
|
|
|
2017-02-05 20:06:58 +07:00
|
|
|
iodev->name = LPC32XXAD_NAME;
|
2012-02-15 01:44:56 +07:00
|
|
|
iodev->dev.parent = &pdev->dev;
|
|
|
|
iodev->info = &lpc32xx_adc_iio_info;
|
|
|
|
iodev->modes = INDIO_DIRECT_MODE;
|
|
|
|
iodev->num_channels = ARRAY_SIZE(lpc32xx_adc_iio_channels);
|
|
|
|
|
2013-10-29 18:39:00 +07:00
|
|
|
retval = devm_iio_device_register(&pdev->dev, iodev);
|
2012-02-15 01:44:56 +07:00
|
|
|
if (retval)
|
2013-09-01 00:12:00 +07:00
|
|
|
return retval;
|
2012-02-15 01:44:56 +07:00
|
|
|
|
|
|
|
dev_info(&pdev->dev, "LPC32XX ADC driver loaded, IRQ %d\n", irq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-21 15:10:16 +07:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id lpc32xx_adc_match[] = {
|
|
|
|
{ .compatible = "nxp,lpc3220-adc" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, lpc32xx_adc_match);
|
|
|
|
#endif
|
|
|
|
|
2012-02-15 01:44:56 +07:00
|
|
|
static struct platform_driver lpc32xx_adc_driver = {
|
|
|
|
.probe = lpc32xx_adc_probe,
|
|
|
|
.driver = {
|
2017-02-05 20:06:58 +07:00
|
|
|
.name = LPC32XXAD_NAME,
|
2012-04-21 15:10:16 +07:00
|
|
|
.of_match_table = of_match_ptr(lpc32xx_adc_match),
|
2012-02-15 01:44:56 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(lpc32xx_adc_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
|
|
|
|
MODULE_DESCRIPTION("LPC32XX ADC driver");
|
|
|
|
MODULE_LICENSE("GPL");
|