From: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Zhiyong Tao <zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
knaack.h-Mmb7MZpHnFY@public.gmane.org,
lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org,
pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org
Cc: srv_heupstream-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
liguo.zhang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
yingjoe.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
eddie.huang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
erin.lo-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
dawei.chien-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org,
matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org,
s.hauer-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org,
devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org,
linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Subject: Re: [PATCH v7 2/3] iio: adc: mt2701: Add Mediatek auxadc driver for mt2701.
Date: Sun, 21 Aug 2016 19:56:26 +0100 [thread overview]
Message-ID: <44e898e7-6f33-f69c-1f26-79fb592c06d5@kernel.org> (raw)
In-Reply-To: <1471504297-26947-3-git-send-email-zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
On 18/08/16 08:11, Zhiyong Tao wrote:
> Add Mediatek auxadc driver based on iio.
> It will register a device in iio and support iio.
> So thermal can read auxadc channel to sample data by iio device.
> It is tested successfully on mt2701 platform.
> Mt8173 and mt6577 platforms are not tested.
> But the expectation is compatible.
>
> Signed-off-by: Zhiyong Tao <zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
Nice little driver. Applied to the togreg branch of iio.git.
Will be pushed out as testing for the autobuilders to play with it.
Thanks,
Jonathan
> ---
> drivers/iio/adc/Kconfig | 13 ++
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/mt6577_auxadc.c | 291 +++++++++++++++++++++++++++++++++++++++
> 3 files changed, 305 insertions(+)
> create mode 100644 drivers/iio/adc/mt6577_auxadc.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 1de31bd..1936152 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -317,6 +317,19 @@ config MCP3422
> This driver can also be built as a module. If so, the module will be
> called mcp3422.
>
> +config MEDIATEK_MT6577_AUXADC
> + tristate "MediaTek AUXADC driver"
> + depends on ARCH_MEDIATEK || COMPILE_TEST
> + depends on HAS_IOMEM
> + help
> + Say yes here to enable support for MediaTek mt65xx AUXADC.
> +
> + The driver supports immediate mode operation to read from one of sixteen
> + channels (external or internal).
> +
> + This driver can also be built as a module. If so, the module will be
> + called mt6577_auxadc.
> +
> config MEN_Z188_ADC
> tristate "MEN 16z188 ADC IP Core support"
> depends on MCB
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index 0ba0d50..d528e2b 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -31,6 +31,7 @@ obj-$(CONFIG_MAX1027) += max1027.o
> obj-$(CONFIG_MAX1363) += max1363.o
> obj-$(CONFIG_MCP320X) += mcp320x.o
> obj-$(CONFIG_MCP3422) += mcp3422.o
> +obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o
> obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o
> obj-$(CONFIG_MXS_LRADC) += mxs-lradc.o
> obj-$(CONFIG_NAU7802) += nau7802.o
> diff --git a/drivers/iio/adc/mt6577_auxadc.c b/drivers/iio/adc/mt6577_auxadc.c
> new file mode 100644
> index 0000000..2d104c8
> --- /dev/null
> +++ b/drivers/iio/adc/mt6577_auxadc.c
> @@ -0,0 +1,291 @@
> +/*
> + * Copyright (c) 2016 MediaTek Inc.
> + * Author: Zhiyong Tao <zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@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.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/iopoll.h>
> +#include <linux/io.h>
> +#include <linux/iio/iio.h>
> +
> +/* Register definitions */
> +#define MT6577_AUXADC_CON0 0x00
> +#define MT6577_AUXADC_CON1 0x04
> +#define MT6577_AUXADC_CON2 0x10
> +#define MT6577_AUXADC_STA BIT(0)
> +
> +#define MT6577_AUXADC_DAT0 0x14
> +#define MT6577_AUXADC_RDY0 BIT(12)
> +
> +#define MT6577_AUXADC_MISC 0x94
> +#define MT6577_AUXADC_PDN_EN BIT(14)
> +
> +#define MT6577_AUXADC_DAT_MASK 0xfff
> +#define MT6577_AUXADC_SLEEP_US 1000
> +#define MT6577_AUXADC_TIMEOUT_US 10000
> +#define MT6577_AUXADC_POWER_READY_MS 1
> +#define MT6577_AUXADC_SAMPLE_READY_US 25
> +
> +struct mt6577_auxadc_device {
> + void __iomem *reg_base;
> + struct clk *adc_clk;
> + struct mutex lock;
> +};
> +
> +#define MT6577_AUXADC_CHANNEL(idx) { \
> + .type = IIO_VOLTAGE, \
> + .indexed = 1, \
> + .channel = (idx), \
> + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
> +}
> +
> +static const struct iio_chan_spec mt6577_auxadc_iio_channels[] = {
> + MT6577_AUXADC_CHANNEL(0),
> + MT6577_AUXADC_CHANNEL(1),
> + MT6577_AUXADC_CHANNEL(2),
> + MT6577_AUXADC_CHANNEL(3),
> + MT6577_AUXADC_CHANNEL(4),
> + MT6577_AUXADC_CHANNEL(5),
> + MT6577_AUXADC_CHANNEL(6),
> + MT6577_AUXADC_CHANNEL(7),
> + MT6577_AUXADC_CHANNEL(8),
> + MT6577_AUXADC_CHANNEL(9),
> + MT6577_AUXADC_CHANNEL(10),
> + MT6577_AUXADC_CHANNEL(11),
> + MT6577_AUXADC_CHANNEL(12),
> + MT6577_AUXADC_CHANNEL(13),
> + MT6577_AUXADC_CHANNEL(14),
> + MT6577_AUXADC_CHANNEL(15),
> +};
> +
> +static inline void mt6577_auxadc_mod_reg(void __iomem *reg,
> + u32 or_mask, u32 and_mask)
> +{
> + u32 val;
> +
> + val = readl(reg);
> + val |= or_mask;
> + val &= ~and_mask;
> + writel(val, reg);
> +}
> +
> +static int mt6577_auxadc_read(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan)
> +{
> + u32 val;
> + void __iomem *reg_channel;
> + int ret;
> + struct mt6577_auxadc_device *adc_dev = iio_priv(indio_dev);
> +
> + reg_channel = adc_dev->reg_base + MT6577_AUXADC_DAT0 +
> + chan->channel * 0x04;
> +
> + mutex_lock(&adc_dev->lock);
> +
> + mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_CON1,
> + 0, 1 << chan->channel);
> +
> + /* read channel and make sure old ready bit == 0 */
> + ret = readl_poll_timeout(reg_channel, val,
> + ((val & MT6577_AUXADC_RDY0) == 0),
> + MT6577_AUXADC_SLEEP_US,
> + MT6577_AUXADC_TIMEOUT_US);
> + if (ret < 0) {
> + dev_err(indio_dev->dev.parent,
> + "wait for channel[%d] ready bit clear time out\n",
> + chan->channel);
> + goto err_timeout;
> + }
> +
> + /* set bit to trigger sample */
> + mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_CON1,
> + 1 << chan->channel, 0);
> +
> + /* we must delay here for hardware sample channel data */
> + udelay(MT6577_AUXADC_SAMPLE_READY_US);
> +
> + /* check MTK_AUXADC_CON2 if auxadc is idle */
> + ret = readl_poll_timeout(adc_dev->reg_base + MT6577_AUXADC_CON2, val,
> + ((val & MT6577_AUXADC_STA) == 0),
> + MT6577_AUXADC_SLEEP_US,
> + MT6577_AUXADC_TIMEOUT_US);
> + if (ret < 0) {
> + dev_err(indio_dev->dev.parent,
> + "wait for auxadc idle time out\n");
> + goto err_timeout;
> + }
> +
> + /* read channel and make sure ready bit == 1 */
> + ret = readl_poll_timeout(reg_channel, val,
> + ((val & MT6577_AUXADC_RDY0) != 0),
> + MT6577_AUXADC_SLEEP_US,
> + MT6577_AUXADC_TIMEOUT_US);
> + if (ret < 0) {
> + dev_err(indio_dev->dev.parent,
> + "wait for channel[%d] data ready time out\n",
> + chan->channel);
> + goto err_timeout;
> + }
> +
> + /* read data */
> + val = readl(reg_channel) & MT6577_AUXADC_DAT_MASK;
> +
> + mutex_unlock(&adc_dev->lock);
> +
> + return val;
> +
> +err_timeout:
> +
> + mutex_unlock(&adc_dev->lock);
> +
> + return -ETIMEDOUT;
> +}
> +
> +static int mt6577_auxadc_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val,
> + int *val2,
> + long info)
> +{
> + switch (info) {
> + case IIO_CHAN_INFO_PROCESSED:
> + *val = mt6577_auxadc_read(indio_dev, chan);
> + if (*val < 0) {
> + dev_err(indio_dev->dev.parent,
> + "failed to sample data on channel[%d]\n",
> + chan->channel);
> + return *val;
> + }
> + return IIO_VAL_INT;
> +
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static const struct iio_info mt6577_auxadc_info = {
> + .driver_module = THIS_MODULE,
> + .read_raw = &mt6577_auxadc_read_raw,
> +};
> +
> +static int mt6577_auxadc_probe(struct platform_device *pdev)
> +{
> + struct mt6577_auxadc_device *adc_dev;
> + unsigned long adc_clk_rate;
> + struct resource *res;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + adc_dev = iio_priv(indio_dev);
> + indio_dev->dev.parent = &pdev->dev;
> + indio_dev->name = dev_name(&pdev->dev);
> + indio_dev->info = &mt6577_auxadc_info;
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + indio_dev->channels = mt6577_auxadc_iio_channels;
> + indio_dev->num_channels = ARRAY_SIZE(mt6577_auxadc_iio_channels);
> +
> + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + adc_dev->reg_base = devm_ioremap_resource(&pdev->dev, res);
> + if (IS_ERR(adc_dev->reg_base)) {
> + dev_err(&pdev->dev, "failed to get auxadc base address\n");
> + return PTR_ERR(adc_dev->reg_base);
> + }
> +
> + adc_dev->adc_clk = devm_clk_get(&pdev->dev, "main");
> + if (IS_ERR(adc_dev->adc_clk)) {
> + dev_err(&pdev->dev, "failed to get auxadc clock\n");
> + return PTR_ERR(adc_dev->adc_clk);
> + }
> +
> + ret = clk_prepare_enable(adc_dev->adc_clk);
> + if (ret) {
> + dev_err(&pdev->dev, "failed to enable auxadc clock\n");
> + return ret;
> + }
> +
> + adc_clk_rate = clk_get_rate(adc_dev->adc_clk);
> + if (!adc_clk_rate) {
> + ret = -EINVAL;
> + dev_err(&pdev->dev, "null clock rate\n");
> + goto err_disable_clk;
> + }
> +
> + mutex_init(&adc_dev->lock);
> +
> + mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
> + MT6577_AUXADC_PDN_EN, 0);
> + mdelay(MT6577_AUXADC_POWER_READY_MS);
> +
> + platform_set_drvdata(pdev, indio_dev);
> +
> + ret = iio_device_register(indio_dev);
> + if (ret < 0) {
> + dev_err(&pdev->dev, "failed to register iio device\n");
> + goto err_power_off;
> + }
> +
> + return 0;
> +
> +err_power_off:
> + mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
> + 0, MT6577_AUXADC_PDN_EN);
> +err_disable_clk:
> + clk_disable_unprepare(adc_dev->adc_clk);
> + return ret;
> +}
> +
> +static int mt6577_auxadc_remove(struct platform_device *pdev)
> +{
> + struct iio_dev *indio_dev = platform_get_drvdata(pdev);
> + struct mt6577_auxadc_device *adc_dev = iio_priv(indio_dev);
> +
> + iio_device_unregister(indio_dev);
> +
> + mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
> + 0, MT6577_AUXADC_PDN_EN);
> +
> + clk_disable_unprepare(adc_dev->adc_clk);
> +
> + return 0;
> +}
> +
> +static const struct of_device_id mt6577_auxadc_of_match[] = {
> + { .compatible = "mediatek,mt2701-auxadc", },
> + { .compatible = "mediatek,mt8173-auxadc", },
> + { }
> +};
> +MODULE_DEVICE_TABLE(of, mt6577_auxadc_of_match);
> +
> +static struct platform_driver mt6577_auxadc_driver = {
> + .driver = {
> + .name = "mt6577-auxadc",
> + .of_match_table = mt6577_auxadc_of_match,
> + },
> + .probe = mt6577_auxadc_probe,
> + .remove = mt6577_auxadc_remove,
> +};
> +module_platform_driver(mt6577_auxadc_driver);
> +
> +MODULE_AUTHOR("Zhiyong Tao <zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>");
> +MODULE_DESCRIPTION("MTK AUXADC Device Driver");
> +MODULE_LICENSE("GPL v2");
>
next prev parent reply other threads:[~2016-08-21 18:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-18 7:11 [PATCH v7 0/3] AUXADC: Mediatek auxadc driver Zhiyong Tao
2016-08-18 7:11 ` [PATCH v7 1/3] dt-bindings: auxadc: Add binding document for Mediatek auxadc Zhiyong Tao
[not found] ` <1471504297-26947-2-git-send-email-zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
2016-08-21 18:54 ` Jonathan Cameron
[not found] ` <1471504297-26947-1-git-send-email-zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
2016-08-18 7:11 ` [PATCH v7 2/3] iio: adc: mt2701: Add Mediatek auxadc driver for mt2701 Zhiyong Tao
[not found] ` <1471504297-26947-3-git-send-email-zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
2016-08-21 18:56 ` Jonathan Cameron [this message]
2016-08-18 7:11 ` [PATCH v7 3/3] arm: dts: mt2701: Add auxadc node Zhiyong Tao
[not found] ` <1471504297-26947-4-git-send-email-zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
2016-08-21 18:58 ` Jonathan Cameron
[not found] ` <c238236c-2445-7802-6f2e-2f4776778c36-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2016-08-22 6:22 ` zhiyong tao
2016-08-23 0:51 ` kbuild test robot
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=44e898e7-6f33-f69c-1f26-79fb592c06d5@kernel.org \
--to=jic23-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=dawei.chien-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=eddie.huang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=erin.lo-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=knaack.h-Mmb7MZpHnFY@public.gmane.org \
--cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
--cc=liguo.zhang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
--cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
--cc=matthias.bgg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org \
--cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=s.hauer-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org \
--cc=srv_heupstream-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=yingjoe.chen-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
--cc=zhiyong.tao-NuS5LvNUpcJWk0Htik3J/w@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).