Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp331517ybz; Fri, 17 Apr 2020 01:54:03 -0700 (PDT) X-Google-Smtp-Source: APiQypLP3/ct0AqRW0rKbQ4q6zNj/zKfYHvOP/XWrxceY8mICslaGKuRNQlV0spWtcuwiX9IbwxB X-Received: by 2002:a50:e8cc:: with SMTP id l12mr2005714edn.204.1587113643237; Fri, 17 Apr 2020 01:54:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587113643; cv=none; d=google.com; s=arc-20160816; b=cn2XIopvY4bWA4ANB0w2ucIBYvP/SQwow/+qCutSXPyjVtmEhlc/yiMybTdcvhnISC pY3Xk1RmKluoAGX4B7t9m4UZG6K/dOK3GA7eznU/lTbS39DTi1LneYQrp3cgkwcvUvlR jzcsw0mMDyTXv0wSn+VerR9GHByI4ZStpMKrNREU0NncuSPiUY3bNlLKJuLvtCT+wcYX PBOduF8ENfPMFXQ70vF0t8+fP44TtBr0wTlQDaTVqHs8gLOzBSl1tc7tw2w6x1l9lQrA kje/DzRTmv0Xml6jF2wyHsKQOjv1m/ENToqw9vbexbbG8ztlS7cD4uRD+U9WGoEIXa3e ENqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=tIMSggY5t3bJxxo9RkdopxY8+yMNt9VADm3loRiXMow=; b=p21SQlhO0/frACNiECQsJoaxSVI3zLfqM/lQCb6aGmSrj5nz9RdC+O+vRTi8K+CpCR FihqYbNWRclgcLeR2VOhjeSjgmvweWxOLQLvp8V0vwQilpv8lBINc1CNbyVwmXsUPKww 5SboYFH1ri8Vq8wbO7VteDu6z02CTO8NkvpiPtVhLysyHDr/jsdAvoDrFfVlJjKIpPBG ouIg5nMloeCZEnt6FjmpNc1DGeb2DSY6GHFcChxE9IDApNkZkfq5K/tAG1nP4pRkSypQ WGlQc4UxDUcljhiye+iebB7dMvN/ZPnGifbLsG2SzHN7/cMFQfwi3vmUerKSbLXYqAUK gKVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="X6Cx0Jy/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc9si10168199ejb.503.2020.04.17.01.53.40; Fri, 17 Apr 2020 01:54:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="X6Cx0Jy/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730070AbgDQIvN (ORCPT + 99 others); Fri, 17 Apr 2020 04:51:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729886AbgDQIuY (ORCPT ); Fri, 17 Apr 2020 04:50:24 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D686AC061A0F; Fri, 17 Apr 2020 01:50:23 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id i10so2088427wrv.10; Fri, 17 Apr 2020 01:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tIMSggY5t3bJxxo9RkdopxY8+yMNt9VADm3loRiXMow=; b=X6Cx0Jy/wDAy5sJwQZlRnnraW5fqmGvCXGd21b0z2i2Z6/gIHin9+UAo9KKADsxein TMp9CcFIxw/6eNna2gi6QF+/r19I6UnFZ/psHu81Tf0iGjac4rp2yPGka8aNaX6xnNwo TnrzHTGFPS3h6Af3eBzmQ5J/qlsI/sMkU7siIoPSq4L8SJl06tSNjZNPRp5ko6ZH58DL 65AYwNuXvGqKWfcs9ABcugB8Tch2LiSRVUbKb5TbzfKeAWG2yvK3QFwMU/a3hm2hdaMh yWhXZxqDhaQGjfUn+YBhpPwrkqhsSs8i2nb19rrrOehvch6HoTPI2NF5MWo/txHGJ5JR 5ONQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tIMSggY5t3bJxxo9RkdopxY8+yMNt9VADm3loRiXMow=; b=s0GCfuh5rCEA7IeT4XeGPTQcdewphP9+XjxYhNmg8tf7RlGd0L4wMbuZS/kjOKPmaP 9OEkxtrcdL1/8o3gkym+TPVMXGB8MJ4ib6OfCeZyjyfTDt+6e0R7wWqBWQRqSae2eob/ 8oTGVZNmmShW63HOCYRUirBMQ0yFCVHATidyY/9yrubP8tnCb8xqqWZ1VloBda61Xzsw vlefrHR31KZqaKeL0MU9MSQ+BM04NgUapPhwEyOYFuPthBCHnbmijSk9KbWm7+SeiNRb 34Rsg5CgMhZ94CPGjSiTLS0M2xYUsnfq13+a2ZTCmSwszr4og+Q0eDxMvmos1w02L7s3 mLZA== X-Gm-Message-State: AGi0PuZUTAZ80ygUo5eMmyJKo/mq/SFk7VE1gD002865RaA4MCFuKWfL SjRZpqXSnUKLXZQeJPJo2C8= X-Received: by 2002:adf:afc6:: with SMTP id y6mr2532579wrd.74.1587113422542; Fri, 17 Apr 2020 01:50:22 -0700 (PDT) Received: from localhost.localdomain (p5B3F7443.dip0.t-ipconnect.de. [91.63.116.67]) by smtp.gmail.com with ESMTPSA id l5sm6807527wmi.22.2020.04.17.01.50.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Apr 2020 01:50:21 -0700 (PDT) From: Saravanan Sekar To: lee.jones@linaro.org, andy.shevchenko@gmail.com, robh+dt@kernel.org, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, sre@kernel.org Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-pm@vger.kernel.org, Saravanan Sekar Subject: [PATCH v10 3/6] iio: adc: mp2629: Add support for mp2629 ADC driver Date: Fri, 17 Apr 2020 10:50:00 +0200 Message-Id: <20200417085003.6124-4-sravanhome@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200417085003.6124-1-sravanhome@gmail.com> References: <20200417085003.6124-1-sravanhome@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for 8-bit resolution ADC readings for input power supply and battery charging measurement. Provides voltage, current readings to mp2629 power supply driver. Signed-off-by: Saravanan Sekar Reviewed-by: Andy Shevchenko --- drivers/iio/adc/Kconfig | 10 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/mp2629_adc.c | 208 +++++++++++++++++++++++++++++++++++ include/linux/mfd/mp2629.h | 9 ++ 4 files changed, 228 insertions(+) create mode 100644 drivers/iio/adc/mp2629_adc.c diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 82e33082958c..ef0c0cd31855 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -680,6 +680,16 @@ config MESON_SARADC To compile this driver as a module, choose M here: the module will be called meson_saradc. +config MP2629_ADC + tristate "Monolithic MP2629 ADC driver" + depends on MFD_MP2629 + help + Say yes to have support for battery charger IC MP2629 ADC device + accessed over I2C. + + This driver provides ADC conversion of system, input power supply + and battery voltage & current information. + config NAU7802 tristate "Nuvoton NAU7802 ADC driver" depends on I2C diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 919228900df9..f14416c245a6 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_MCP3911) += mcp3911.o obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o obj-$(CONFIG_MESON_SARADC) += meson_saradc.o +obj-$(CONFIG_MP2629_ADC) += mp2629_adc.o obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o obj-$(CONFIG_NAU7802) += nau7802.o obj-$(CONFIG_NPCM_ADC) += npcm_adc.o diff --git a/drivers/iio/adc/mp2629_adc.c b/drivers/iio/adc/mp2629_adc.c new file mode 100644 index 000000000000..faf389f32729 --- /dev/null +++ b/drivers/iio/adc/mp2629_adc.c @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * MP2629 Driver for ADC + * + * Copyright 2020 Monolithic Power Systems, Inc + * + * Author: Saravanan Sekar + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define MP2629_REG_ADC_CTRL 0x03 +#define MP2629_REG_BATT_VOLT 0x0e +#define MP2629_REG_SYSTEM_VOLT 0x0f +#define MP2629_REG_INPUT_VOLT 0x11 +#define MP2629_REG_BATT_CURRENT 0x12 +#define MP2629_REG_INPUT_CURRENT 0x13 + +#define MP2629_ADC_START BIT(7) +#define MP2629_ADC_CONTINUOUS BIT(6) + +#define MP2629_MAP(_mp, _mpc) IIO_MAP(#_mp, "mp2629_charger", "mp2629-"_mpc) + +#define MP2629_ADC_CHAN(_ch, _type) { \ + .type = _type, \ + .indexed = 1, \ + .datasheet_name = #_ch, \ + .channel = MP2629_ ## _ch, \ + .address = MP2629_REG_ ## _ch, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ +} + +struct mp2629_adc { + struct regmap *regmap; + struct device *dev; +}; + +static struct iio_chan_spec mp2629_channels[] = { + MP2629_ADC_CHAN(BATT_VOLT, IIO_VOLTAGE), + MP2629_ADC_CHAN(SYSTEM_VOLT, IIO_VOLTAGE), + MP2629_ADC_CHAN(INPUT_VOLT, IIO_VOLTAGE), + MP2629_ADC_CHAN(BATT_CURRENT, IIO_CURRENT), + MP2629_ADC_CHAN(INPUT_CURRENT, IIO_CURRENT) +}; + +static struct iio_map mp2629_adc_maps[] = { + MP2629_MAP(BATT_VOLT, "batt-volt"), + MP2629_MAP(SYSTEM_VOLT, "system-volt"), + MP2629_MAP(INPUT_VOLT, "input-volt"), + MP2629_MAP(BATT_CURRENT, "batt-current"), + MP2629_MAP(INPUT_CURRENT, "input-current") +}; + +static int mp2629_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct mp2629_adc *info = iio_priv(indio_dev); + unsigned int rval; + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret = regmap_read(info->regmap, chan->address, &rval); + if (ret) + return ret; + + if (chan->address == MP2629_INPUT_VOLT) + rval &= GENMASK(6, 0); + *val = rval; + return IIO_VAL_INT; + + case IIO_CHAN_INFO_SCALE: + switch (chan->channel) { + case MP2629_BATT_VOLT: + case MP2629_SYSTEM_VOLT: + *val = 20; + return IIO_VAL_INT; + + case MP2629_INPUT_VOLT: + *val = 60; + return IIO_VAL_INT; + + case MP2629_BATT_CURRENT: + *val = 175; + *val2 = 10; + return IIO_VAL_FRACTIONAL; + + case MP2629_INPUT_CURRENT: + *val = 133; + *val2 = 10; + return IIO_VAL_FRACTIONAL; + + default: + return -EINVAL; + } + + default: + return -EINVAL; + } +} + +static const struct iio_info mp2629_adc_info = { + .read_raw = &mp2629_read_raw, +}; + +static int mp2629_adc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct mp2629_info *ddata = dev_get_drvdata(dev->parent); + struct mp2629_adc *info; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*info)); + if (!indio_dev) + return -ENOMEM; + + info = iio_priv(indio_dev); + info->regmap = ddata->regmap; + info->dev = dev; + platform_set_drvdata(pdev, indio_dev); + + ret = regmap_update_bits(ddata->regmap, MP2629_REG_ADC_CTRL, + MP2629_ADC_START | MP2629_ADC_CONTINUOUS, + MP2629_ADC_START | MP2629_ADC_CONTINUOUS); + if (ret) { + dev_err(dev, "adc enable fail: %d\n", ret); + return ret; + } + + ret = iio_map_array_register(indio_dev, mp2629_adc_maps); + if (ret) { + dev_err(dev, "IIO maps register fail: %d\n", ret); + goto fail_disable; + } + + indio_dev->name = "mp2629-adc"; + indio_dev->dev.parent = dev; + indio_dev->channels = mp2629_channels; + indio_dev->num_channels = ARRAY_SIZE(mp2629_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->info = &mp2629_adc_info; + + ret = iio_device_register(indio_dev); + if (ret) { + dev_err(dev, "IIO device register fail: %d\n", ret); + goto fail_map_unregister; + } + + return 0; + +fail_map_unregister: + iio_map_array_unregister(indio_dev); + +fail_disable: + regmap_update_bits(ddata->regmap, MP2629_REG_ADC_CTRL, + MP2629_ADC_CONTINUOUS, 0); + regmap_update_bits(ddata->regmap, MP2629_REG_ADC_CTRL, + MP2629_ADC_START, 0); + + return ret; +} + +static int mp2629_adc_remove(struct platform_device *pdev) +{ + struct iio_dev *indio_dev = platform_get_drvdata(pdev); + struct mp2629_adc *info = iio_priv(indio_dev); + + iio_device_unregister(indio_dev); + + iio_map_array_unregister(indio_dev); + + regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL, + MP2629_ADC_CONTINUOUS, 0); + regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL, + MP2629_ADC_START, 0); + + return 0; +} + +static const struct of_device_id mp2629_adc_of_match[] = { + { .compatible = "mps,mp2629_adc"}, + {} +}; +MODULE_DEVICE_TABLE(of, mp2629_adc_of_match); + +static struct platform_driver mp2629_adc_driver = { + .driver = { + .name = "mp2629_adc", + .of_match_table = mp2629_adc_of_match, + }, + .probe = mp2629_adc_probe, + .remove = mp2629_adc_remove, +}; +module_platform_driver(mp2629_adc_driver); + +MODULE_AUTHOR("Saravanan Sekar "); +MODULE_DESCRIPTION("MP2629 ADC driver"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/mfd/mp2629.h b/include/linux/mfd/mp2629.h index 06985e41fc6f..83ebfab41691 100644 --- a/include/linux/mfd/mp2629.h +++ b/include/linux/mfd/mp2629.h @@ -16,4 +16,13 @@ struct mp2629_info { struct regmap *regmap; }; +enum mp2629_adc_chan { + MP2629_BATT_VOLT, + MP2629_SYSTEM_VOLT, + MP2629_INPUT_VOLT, + MP2629_BATT_CURRENT, + MP2629_INPUT_CURRENT, + MP2629_ADC_CHAN_END +}; + #endif -- 2.17.1