From: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Joachim Eastwood <manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: knaack.h-Mmb7MZpHnFY@public.gmane.org,
lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org,
pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org,
linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH v3 3/4] iio: dac: add NXP LPC18xx DAC driver
Date: Sat, 12 Mar 2016 18:15:23 +0000 [thread overview]
Message-ID: <56E45CBB.2090808@kernel.org> (raw)
In-Reply-To: <1457785817-32328-4-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
On 12/03/16 12:30, Joachim Eastwood wrote:
> Add base support for the 10-bit DAC peripheral found
> on NXP LPC18xx/43xx SoCs.
>
> This is a minimal driver that does not support DMA or
> interrupts.
>
> User manual with register description can be found on:
> LPC18xx: www.nxp.com/documents/user_manual/UM10430.pdf
> LPC43xx: www.nxp.com/documents/user_manual/UM10503.pdf
>
> Signed-off-by: Joachim Eastwood <manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Applied.
> ---
> drivers/iio/dac/Kconfig | 10 ++
> drivers/iio/dac/Makefile | 1 +
> drivers/iio/dac/lpc18xx_dac.c | 210 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 221 insertions(+)
> create mode 100644 drivers/iio/dac/lpc18xx_dac.c
>
> diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
> index a995139f907c..210db81ca144 100644
> --- a/drivers/iio/dac/Kconfig
> +++ b/drivers/iio/dac/Kconfig
> @@ -154,6 +154,16 @@ config AD7303
> To compile this driver as module choose M here: the module will be called
> ad7303.
>
> +config LPC18XX_DAC
> + tristate "NXP LPC18xx DAC driver"
> + depends on ARCH_LPC18XX || COMPILE_TEST
> + depends on OF && HAS_IOMEM
> + help
> + Say yes here to build support for NXP LPC18XX DAC.
> +
> + To compile this driver as a module, choose M here: the module will be
> + called lpc18xx_dac.
> +
> config M62332
> tristate "Mitsubishi M62332 DAC driver"
> depends on I2C
> diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
> index 67b48429686d..420a15cdaa53 100644
> --- a/drivers/iio/dac/Makefile
> +++ b/drivers/iio/dac/Makefile
> @@ -17,6 +17,7 @@ obj-$(CONFIG_AD5764) += ad5764.o
> obj-$(CONFIG_AD5791) += ad5791.o
> obj-$(CONFIG_AD5686) += ad5686.o
> obj-$(CONFIG_AD7303) += ad7303.o
> +obj-$(CONFIG_LPC18XX_DAC) += lpc18xx_dac.o
> obj-$(CONFIG_M62332) += m62332.o
> obj-$(CONFIG_MAX517) += max517.o
> obj-$(CONFIG_MAX5821) += max5821.o
> diff --git a/drivers/iio/dac/lpc18xx_dac.c b/drivers/iio/dac/lpc18xx_dac.c
> new file mode 100644
> index 000000000000..55d1456a059d
> --- /dev/null
> +++ b/drivers/iio/dac/lpc18xx_dac.c
> @@ -0,0 +1,210 @@
> +/*
> + * IIO DAC driver for NXP LPC18xx DAC
> + *
> + * Copyright (C) 2016 Joachim Eastwood <manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * UNSUPPORTED hardware features:
> + * - Interrupts
> + * - DMA
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/err.h>
> +#include <linux/iio/iio.h>
> +#include <linux/iio/driver.h>
> +#include <linux/io.h>
> +#include <linux/iopoll.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/regulator/consumer.h>
> +
> +/* LPC18XX DAC registers and bits */
> +#define LPC18XX_DAC_CR 0x000
> +#define LPC18XX_DAC_CR_VALUE_SHIFT 6
> +#define LPC18XX_DAC_CR_VALUE_MASK 0x3ff
> +#define LPC18XX_DAC_CR_BIAS BIT(16)
> +#define LPC18XX_DAC_CTRL 0x004
> +#define LPC18XX_DAC_CTRL_DMA_ENA BIT(3)
> +
> +struct lpc18xx_dac {
> + struct regulator *vref;
> + void __iomem *base;
> + struct mutex lock;
> + struct clk *clk;
> +};
> +
> +static const struct iio_chan_spec lpc18xx_dac_iio_channels[] = {
> + {
> + .type = IIO_VOLTAGE,
> + .output = 1,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE),
> + },
> +};
> +
> +static int lpc18xx_dac_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct lpc18xx_dac *dac = iio_priv(indio_dev);
> + u32 reg;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + reg = readl(dac->base + LPC18XX_DAC_CR);
> + *val = reg >> LPC18XX_DAC_CR_VALUE_SHIFT;
> + *val &= LPC18XX_DAC_CR_VALUE_MASK;
> +
> + return IIO_VAL_INT;
> +
> + case IIO_CHAN_INFO_SCALE:
> + *val = regulator_get_voltage(dac->vref) / 1000;
> + *val2 = 10;
> +
> + return IIO_VAL_FRACTIONAL_LOG2;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static int lpc18xx_dac_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct lpc18xx_dac *dac = iio_priv(indio_dev);
> + u32 reg;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + if (val < 0 || val > LPC18XX_DAC_CR_VALUE_MASK)
> + return -EINVAL;
> +
> + reg = LPC18XX_DAC_CR_BIAS;
> + reg |= val << LPC18XX_DAC_CR_VALUE_SHIFT;
> +
> + mutex_lock(&dac->lock);
> + writel(reg, dac->base + LPC18XX_DAC_CR);
> + writel(LPC18XX_DAC_CTRL_DMA_ENA, dac->base + LPC18XX_DAC_CTRL);
> + mutex_unlock(&dac->lock);
> +
> + return 0;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static const struct iio_info lpc18xx_dac_info = {
> + .read_raw = lpc18xx_dac_read_raw,
> + .write_raw = lpc18xx_dac_write_raw,
> + .driver_module = THIS_MODULE,
> +};
> +
> +static int lpc18xx_dac_probe(struct platform_device *pdev)
> +{
> + struct iio_dev *indio_dev;
> + struct lpc18xx_dac *dac;
> + struct resource *res;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*dac));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + platform_set_drvdata(pdev, indio_dev);
> + dac = iio_priv(indio_dev);
> + mutex_init(&dac->lock);
> +
> + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + dac->base = devm_ioremap_resource(&pdev->dev, res);
> + if (IS_ERR(dac->base))
> + return PTR_ERR(dac->base);
> +
> + dac->clk = devm_clk_get(&pdev->dev, NULL);
> + if (IS_ERR(dac->clk)) {
> + dev_err(&pdev->dev, "error getting clock\n");
> + return PTR_ERR(dac->clk);
> + }
> +
> + dac->vref = devm_regulator_get(&pdev->dev, "vref");
> + if (IS_ERR(dac->vref)) {
> + dev_err(&pdev->dev, "error getting regulator\n");
> + return PTR_ERR(dac->vref);
> + }
> +
> + indio_dev->name = dev_name(&pdev->dev);
> + indio_dev->dev.parent = &pdev->dev;
> + indio_dev->info = &lpc18xx_dac_info;
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + indio_dev->channels = lpc18xx_dac_iio_channels;
> + indio_dev->num_channels = ARRAY_SIZE(lpc18xx_dac_iio_channels);
> +
> + ret = regulator_enable(dac->vref);
> + if (ret) {
> + dev_err(&pdev->dev, "unable to enable regulator\n");
> + return ret;
> + }
> +
> + ret = clk_prepare_enable(dac->clk);
> + if (ret) {
> + dev_err(&pdev->dev, "unable to enable clock\n");
> + goto dis_reg;
> + }
> +
> + writel(0, dac->base + LPC18XX_DAC_CTRL);
> + writel(0, dac->base + LPC18XX_DAC_CR);
> +
> + ret = iio_device_register(indio_dev);
> + if (ret) {
> + dev_err(&pdev->dev, "unable to register device\n");
> + goto dis_clk;
> + }
> +
> + return 0;
> +
> +dis_clk:
> + clk_disable_unprepare(dac->clk);
> +dis_reg:
> + regulator_disable(dac->vref);
> + return ret;
> +}
> +
> +static int lpc18xx_dac_remove(struct platform_device *pdev)
> +{
> + struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> + struct lpc18xx_dac *dac = iio_priv(indio_dev);
> +
> + iio_device_unregister(indio_dev);
> +
> + writel(0, dac->base + LPC18XX_DAC_CTRL);
> + clk_disable_unprepare(dac->clk);
> + regulator_disable(dac->vref);
> +
> + return 0;
> +}
> +
> +static const struct of_device_id lpc18xx_dac_match[] = {
> + { .compatible = "nxp,lpc1850-dac" },
> + { /* sentinel */ }
> +};
> +MODULE_DEVICE_TABLE(of, lpc18xx_dac_match);
> +
> +static struct platform_driver lpc18xx_dac_driver = {
> + .probe = lpc18xx_dac_probe,
> + .remove = lpc18xx_dac_remove,
> + .driver = {
> + .name = "lpc18xx-dac",
> + .of_match_table = lpc18xx_dac_match,
> + },
> +};
> +module_platform_driver(lpc18xx_dac_driver);
> +
> +MODULE_DESCRIPTION("LPC18xx DAC driver");
> +MODULE_AUTHOR("Joachim Eastwood <manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>");
> +MODULE_LICENSE("GPL v2");
>
next prev parent reply other threads:[~2016-03-12 18:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-12 12:30 [PATCH v3 0/4] Support for analog peripherals on LPC18xx familiy Joachim Eastwood
[not found] ` <1457785817-32328-1-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-03-12 12:30 ` [PATCH v3 1/4] iio: adc: add NXP LPC18xx ADC driver Joachim Eastwood
[not found] ` <1457785817-32328-2-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-03-12 18:14 ` Jonathan Cameron
2016-03-12 12:30 ` [PATCH v3 2/4] dt: document NXP LPC1850 ADC driver bindings Joachim Eastwood
[not found] ` <1457785817-32328-3-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-03-12 18:15 ` Jonathan Cameron
2016-03-12 12:30 ` [PATCH v3 3/4] iio: dac: add NXP LPC18xx DAC driver Joachim Eastwood
[not found] ` <1457785817-32328-4-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-03-12 18:15 ` Jonathan Cameron [this message]
2016-03-12 12:30 ` [PATCH v3 4/4] dt: document NXP LPC1850 DAC driver bindings Joachim Eastwood
[not found] ` <1457785817-32328-5-git-send-email-manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-03-12 18:17 ` Jonathan Cameron
[not found] ` <56E45D45.4050605-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2016-03-12 19:06 ` Joachim Eastwood
2016-03-18 20:12 ` Rob Herring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=56E45CBB.2090808@kernel.org \
--to=jic23-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=knaack.h-Mmb7MZpHnFY@public.gmane.org \
--cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
--cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=manabian-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org \
--cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).