Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1947456rwb; Sun, 2 Oct 2022 11:11:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5LVZiWNwbUU6cmAjDoEQcgLroiyDjth8KCUhb+GFkuaMPlJnWUy0TearcPS8Mwp8jpVqzh X-Received: by 2002:a17:906:58cc:b0:785:f030:19c2 with SMTP id e12-20020a17090658cc00b00785f03019c2mr13089822ejs.88.1664734280330; Sun, 02 Oct 2022 11:11:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664734280; cv=none; d=google.com; s=arc-20160816; b=MhX3miv4hdYniO67fqv+ztWC/k7FRbiPKUi4xBh1pvWjmrHa3vNws0oMe5hanZidrv E4aWftkt4vU8PWZVNsLuw76/h1Gq/Eiqv74aquyJq6ZImt5Q43Ns1ldxlL7zlDw1RVjQ yGLqnPJSeiVnc5fCkIbG0f0hHF2rs0Ybo8oZBO4utASca6Pg2J1lvQAFZBFkSTO1qBzB um6fAWTVzoJGaiX1PKrWjlsBy7nxN9wortq2cHssGBtE6ZJi7+HBMYtfUSExwBN8glGd eY/sNc2N1ot2G9pwg6kzN62FCo2A7nGQL7G1EESOeCtwemjRZgH7YIIef7piTNBUsMg0 SILQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=jsLkQ1URqYZV0RJspEC1Qkdxi+dgKYrcyjhEHzcg9bA=; b=02Um3U+yKGrBqVjPJPuy23smazK5VQYuM7HUjmuimuqUbv5QpANQJpwG87/kJI4bx9 nW2PpwCWUYAWY47UbT8Cg5stNZaM0buc2WbuBXQ3IQnxl5eOUGl9J9c19GxYyO3Yi1Pf S9nZwttPzxeaQvd1o852CijJ4vl9yXqAaQIE4vImi+xWWjR4Ijiyyu0u8g7BR1NfcWTN yTBL4FuMYpq7YOK0d2tnEBwgay1MhgIqbRIbuBB4CevOYePNqfM6L/cnZkISU7C94RNt Y5zqYA0zdccyDu80UWjb7pLr6AL/lvlhf2+krvI8lDj30nDX7+wF6ICYhisvN58b7SD/ xf9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AHrf1ahy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ef11-20020a05640228cb00b00458ea275c22si1635671edb.325.2022.10.02.11.10.53; Sun, 02 Oct 2022 11:11:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AHrf1ahy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbiJBRs1 (ORCPT + 99 others); Sun, 2 Oct 2022 13:48:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229681AbiJBRsZ (ORCPT ); Sun, 2 Oct 2022 13:48:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E84B436082; Sun, 2 Oct 2022 10:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664732903; x=1696268903; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=ApulsrstOsE6sQgCr69j7S7zz4j5JHA7E5+uPVmJbGY=; b=AHrf1ahy4e0FoCRX1kdgNrahA+or7H6eCy6UCavsTLi0fSqh06/uu3RN q6rbJLNiNZxlHsM3RT8144fnxwYiFByUInMOSQO9g4mLni45uhLLRytj0 ZuagzXropWnlTCP9/RqrHS17pxGDXwNTe+zLRBAGucci7ngkn1yqRFteq mrmuwxAFLC0rUPlUf0kK5ipdwLy9sui6qkSICVohjtPZIur8af5Mk+Qcg Zk/Q/YOzvjowtrxgNh+pOgqrWu8EohBsplh9ye1HvIUZLXOf7rO/biwxZ b3FfR4zNERhhiCNr7fyX3aAab0TgCA959ttyg4O+19jdWA8BW71JeVDHR w==; X-IronPort-AV: E=McAfee;i="6500,9779,10488"; a="388785888" X-IronPort-AV: E=Sophos;i="5.93,363,1654585200"; d="scan'208";a="388785888" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Oct 2022 10:48:23 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10488"; a="574401734" X-IronPort-AV: E=Sophos;i="5.93,363,1654585200"; d="scan'208";a="574401734" Received: from unknown (HELO rajath-NUC10i7FNH..) ([10.223.165.55]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Oct 2022 10:48:21 -0700 From: Rajat Khandelwal To: jic23@kernel.org, lars@metafoo.de Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, rajat.khandelwal@intel.com, Rajat Khandelwal Subject: [PATCH v3] iio: pressure: mpl115: Implementing low power mode by shutdown gpio Date: Mon, 3 Oct 2022 23:17:43 +0530 Message-Id: <20221003174743.2693435-1-rajat.khandelwal@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_12_24, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org MPL115 supports shutdown gpio which can be used to set the default state to low power mode. Power from all internal circuits and registers is removed. This is done by pulling the SHDN pin to low. This patch sets the default mode to low-power and only exits when a reading is required from the chip. This maximises power savings. According to spec., a wakeup time period of ~5 ms exists between waking up and actually communicating with the device. This is implemented using sleep delay. Signed-off-by: Rajat Khandelwal --- v3: Using runtime PM for low power mode and not forcing shutdown pin drivers/iio/pressure/mpl115.c | 61 ++++++++++++++++++++++++++++++- drivers/iio/pressure/mpl115.h | 5 +++ drivers/iio/pressure/mpl115_i2c.c | 1 + drivers/iio/pressure/mpl115_spi.c | 1 + 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/drivers/iio/pressure/mpl115.c b/drivers/iio/pressure/mpl115.c index 5bf5b9abe6f1..ec7527161844 100644 --- a/drivers/iio/pressure/mpl115.c +++ b/drivers/iio/pressure/mpl115.c @@ -4,12 +4,13 @@ * * Copyright (c) 2014 Peter Meerwald * - * TODO: shutdown pin + * TODO: synchronization with system suspend */ #include #include #include +#include #include "mpl115.h" @@ -27,6 +28,7 @@ struct mpl115_data { s16 a0; s16 b1, b2; s16 c12; + struct gpio_desc *shutdown; const struct mpl115_ops *ops; }; @@ -102,16 +104,24 @@ static int mpl115_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_PROCESSED: + pm_runtime_get_sync(data->dev); ret = mpl115_comp_pressure(data, val, val2); if (ret < 0) return ret; + pm_runtime_mark_last_busy(data->dev); + pm_runtime_put_autosuspend(data->dev); + return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_RAW: + pm_runtime_get_sync(data->dev); /* temperature -5.35 C / LSB, 472 LSB is 25 C */ ret = mpl115_read_temp(data); if (ret < 0) return ret; + pm_runtime_mark_last_busy(data->dev); + pm_runtime_put_autosuspend(data->dev); *val = ret >> 6; + return IIO_VAL_INT; case IIO_CHAN_INFO_OFFSET: *val = -605; @@ -168,6 +178,8 @@ int mpl115_probe(struct device *dev, const char *name, if (ret) return ret; + dev_set_drvdata(dev, indio_dev); + ret = data->ops->read(data->dev, MPL115_A0); if (ret < 0) return ret; @@ -185,10 +193,58 @@ int mpl115_probe(struct device *dev, const char *name, return ret; data->c12 = ret; + data->shutdown = devm_gpiod_get_optional(dev, "shutdown", + GPIOD_OUT_LOW); + if (IS_ERR(data->shutdown)) + return dev_err_probe(dev, PTR_ERR(data->shutdown), + "cannot get shutdown gpio\n"); + + if (data->shutdown) { + /* Enable runtime PM */ + pm_runtime_get_noresume(dev); + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + + /* + * As the device takes 3 ms to come up with a fresh + * reading after power-on and 5 ms to actually power-on, + * do not shut it down unnecessarily. Set autosuspend to + * 200 ms. + */ + pm_runtime_set_autosuspend_delay(dev, 200); + pm_runtime_use_autosuspend(dev); + pm_runtime_put(dev); + + dev_dbg(dev, "low-power mode enabled"); + } else + dev_dbg(dev, "low-power mode disabled"); + return devm_iio_device_register(dev, indio_dev); } EXPORT_SYMBOL_NS_GPL(mpl115_probe, IIO_MPL115); +static int mpl115_runtime_suspend(struct device *dev) +{ + struct mpl115_data *data = iio_priv(dev_get_drvdata(dev)); + + gpiod_set_value(data->shutdown, 1); + + return 0; +} + +static int mpl115_runtime_resume(struct device *dev) +{ + struct mpl115_data *data = iio_priv(dev_get_drvdata(dev)); + + gpiod_set_value(data->shutdown, 0); + usleep_range(5000, 6000); + + return 0; +} + +DEFINE_RUNTIME_DEV_PM_OPS(mpl115_dev_pm_ops, mpl115_runtime_suspend, + mpl115_runtime_resume, NULL); + MODULE_AUTHOR("Peter Meerwald "); MODULE_DESCRIPTION("Freescale MPL115 pressure/temperature driver"); MODULE_LICENSE("GPL"); diff --git a/drivers/iio/pressure/mpl115.h b/drivers/iio/pressure/mpl115.h index 57d55eb8e661..78a0068a17bb 100644 --- a/drivers/iio/pressure/mpl115.h +++ b/drivers/iio/pressure/mpl115.h @@ -6,6 +6,8 @@ * Copyright (c) 2016 Akinobu Mita */ +#include + #ifndef _MPL115_H_ #define _MPL115_H_ @@ -18,4 +20,7 @@ struct mpl115_ops { int mpl115_probe(struct device *dev, const char *name, const struct mpl115_ops *ops); +/*PM ops */ +extern const struct dev_pm_ops mpl115_dev_pm_ops; + #endif diff --git a/drivers/iio/pressure/mpl115_i2c.c b/drivers/iio/pressure/mpl115_i2c.c index 099ab1c6832c..555bda1146fb 100644 --- a/drivers/iio/pressure/mpl115_i2c.c +++ b/drivers/iio/pressure/mpl115_i2c.c @@ -53,6 +53,7 @@ MODULE_DEVICE_TABLE(i2c, mpl115_i2c_id); static struct i2c_driver mpl115_i2c_driver = { .driver = { .name = "mpl115", + .pm = pm_ptr(&mpl115_dev_pm_ops), }, .probe = mpl115_i2c_probe, .id_table = mpl115_i2c_id, diff --git a/drivers/iio/pressure/mpl115_spi.c b/drivers/iio/pressure/mpl115_spi.c index 7feec87e2704..58d218fd90dc 100644 --- a/drivers/iio/pressure/mpl115_spi.c +++ b/drivers/iio/pressure/mpl115_spi.c @@ -92,6 +92,7 @@ MODULE_DEVICE_TABLE(spi, mpl115_spi_ids); static struct spi_driver mpl115_spi_driver = { .driver = { .name = "mpl115", + .pm = pm_ptr(&mpl115_dev_pm_ops), }, .probe = mpl115_spi_probe, .id_table = mpl115_spi_ids, -- 2.34.1