From patchwork Tue Sep 20 18:09:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?M=C3=A5rten_Lindahl?= X-Patchwork-Id: 12982488 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5D83C6FA92 for ; Tue, 20 Sep 2022 18:10:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230366AbiITSKm (ORCPT ); Tue, 20 Sep 2022 14:10:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231339AbiITSKT (ORCPT ); Tue, 20 Sep 2022 14:10:19 -0400 Received: from smtp1.axis.com (smtp1.axis.com [195.60.68.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59F6B1029 for ; Tue, 20 Sep 2022 11:10:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1663697418; x=1695233418; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3Xs6q+yKSseagyY72GTw3KwU0yZ6BkMg1tA9pOF7Tr4=; b=gBrNZ96V694LuQ1d4lT9oZA/cT3EI2nsnIMJDwPuio+XlUUi+ml0BLiB MxQYhfwylknD3USP9ZCqOHusYkRk7xLEynmvrDBXtXr9EmUTEynzVzJQ+ aVsfLjzL3FvDxuQPMaWv1LoC2haHLwGdYWU4tMK2QBA7mv50x++prL2Qd wrzqIERSv+1Trbt/wg1zo9vlC4w4JgInpmVAMoyhVNOUhnUqC7T+KNJqj ldXWkz3AO1WrJ/oXEB890E8GwXNsuulk+5eIRNkXVbtbuGaVZXeoPmMW5 XAeC43+szLXIFFG/D0TxRgNaGPzEjr9bBz7hxDIRU7mxPQ4POgUViULSh w==; From: =?utf-8?q?M=C3=A5rten_Lindahl?= To: Jonathan Cameron , Lars-Peter Clausen CC: Paul Cercueil , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , , , =?utf-8?q?M=C3=A5rten_Linda?= =?utf-8?q?hl?= Subject: [PATCH 1/3] iio: light: vcnl4000: Preserve conf bits when toggle power Date: Tue, 20 Sep 2022 20:09:56 +0200 Message-ID: <20220920180958.2308229-2-marten.lindahl@axis.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220920180958.2308229-1-marten.lindahl@axis.com> References: <20220920180958.2308229-1-marten.lindahl@axis.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org As the vcnl4040 and vcnl4200 chip uses runtime power management for turning the ambient light and proximity sensors on/off, it overwrites the entire register each time. In ALS_CONF register bit fields ALS_IT, ALS_PERS, ALS_INT_EN are overwritten. In PS_CONF1 register bit fields PS_DUTY, PS_PERS, PS_IT, PS_HD, and PS_INT are overwritten. Add functions for preserving the affected bit fields when changing power state. Signed-off-by: MÃ¥rten Lindahl --- drivers/iio/light/vcnl4000.c | 53 ++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c index 3db4e26731bb..0b226c684957 100644 --- a/drivers/iio/light/vcnl4000.c +++ b/drivers/iio/light/vcnl4000.c @@ -74,6 +74,9 @@ #define VCNL4000_PROX_EN BIT(1) /* start proximity measurement */ #define VCNL4000_SELF_TIMED_EN BIT(0) /* start self-timed measurement */ +#define VCNL4040_ALS_CONF_ALS_SD BIT(0) /* Enable ambient light sensor */ +#define VCNL4040_PS_CONF1_PS_SD BIT(0) /* Enable proximity sensor */ + /* Bit masks for interrupt registers. */ #define VCNL4010_INT_THR_SEL BIT(0) /* Select threshold interrupt source */ #define VCNL4010_INT_THR_EN BIT(1) /* Threshold interrupt type */ @@ -188,16 +191,62 @@ static int vcnl4000_init(struct vcnl4000_data *data) return data->chip_spec->set_power_state(data, true); }; +static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, int val) +{ + int ret; + + switch (data->id) { + case VCNL4040: + case VCNL4200: + ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF); + if (ret < 0) + return ret; + + if (val) + ret &= ~VCNL4040_ALS_CONF_ALS_SD; + else + ret |= VCNL4040_ALS_CONF_ALS_SD; + + return i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, + ret); + default: + return -EINVAL; + } +} + +static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, int val) +{ + int ret; + + switch (data->id) { + case VCNL4040: + case VCNL4200: + ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1); + if (ret < 0) + return ret; + + if (val) + ret &= ~VCNL4040_PS_CONF1_PS_SD; + else + ret |= VCNL4040_PS_CONF1_PS_SD; + + return i2c_smbus_write_word_data(data->client, + VCNL4200_PS_CONF1, ret); + default: + return -EINVAL; + } +} + static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on) { u16 val = on ? 0 /* power on */ : 1 /* shut down */; int ret; - ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, val); + ret = vcnl4000_write_als_enable(data, !val); if (ret < 0) return ret; - ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val); + ret = vcnl4000_write_ps_enable(data, !val); if (ret < 0) return ret;