From patchwork Mon Aug 3 23:41:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Campello X-Patchwork-Id: 11699249 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 91C6314E3 for ; Mon, 3 Aug 2020 23:42:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B784622D05 for ; Mon, 3 Aug 2020 23:42:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XNDeIGNp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728926AbgHCXmh (ORCPT ); Mon, 3 Aug 2020 19:42:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728912AbgHCXmH (ORCPT ); Mon, 3 Aug 2020 19:42:07 -0400 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 356CDC061757 for ; Mon, 3 Aug 2020 16:42:07 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id l17so40361922iok.7 for ; Mon, 03 Aug 2020 16:42:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bZtgyz4w9bioKhpkBgBZgXBORnt82DhjEIEidPD/D0c=; b=XNDeIGNp2H4ZbKTQ3i4Ti8f6et3wSK/SnNykPc+bdzRVUKA+SGd464nUM4ENll3c27 69/3TYIyyHmi4cZrJmyK+vmJJDIDz9yEBHdLjdtr0iz+pzS+9KUyewCQg4N8wf8UiBrh hZe6pLZ2DRm1jdBqN+wEnMvKvk2Y753T/sH5A= 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:mime-version:content-transfer-encoding; bh=bZtgyz4w9bioKhpkBgBZgXBORnt82DhjEIEidPD/D0c=; b=mldrxpmSnm1Bhh/ozo5fur1U0u+93bcT9jFdbRDz/iIhPfYPxKXvCrMS4WhNkGlQWR 4Xy9rwvNaFLWMfeQSawPyaRQGMOyuDm88mczT6ZmaayLL+4IodJ8Je5XJZLFiqA8loxA yv++i4df9VoR35gu+QIxAg9Re3wMpARGl7DR/VVh8Si57noouwteyWeLsfOce2NSIiyo ZMh5r4Lvmc38cD/6JynzEhPhiVZ3lj92qDFNMT+/fdOqe6s+mnNt8Ax5MTexgs+N/fZr wtJ7vFoxPw2wNLdLh6zT907c3jW2nDQ1PIyrptNocy3aJATRlxd+Sr7S0pQ22yBlr17R a0Cw== X-Gm-Message-State: AOAM530ydI18WzgC8LRjjZrT3fVGs3JVLsq+uhU5fgL/jBATnQJ+0GPb +QzoWtF7QXV2b55OUmoWSTINmw== X-Google-Smtp-Source: ABdhPJzcRmjkFF3frNBKgigwLZQlEiezRtbcaLOq4pOuDMxQ0MDjYIWPRUPDi1VetefN+u9EeKkoLA== X-Received: by 2002:a02:9185:: with SMTP id p5mr2491651jag.25.1596498126584; Mon, 03 Aug 2020 16:42:06 -0700 (PDT) Received: from derch.Home (97-122-92-59.hlrn.qwest.net. [97.122.92.59]) by smtp.gmail.com with ESMTPSA id g2sm5468435ioe.4.2020.08.03.16.42.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 16:42:06 -0700 (PDT) From: Daniel Campello To: LKML , LKML Cc: Jonathan Cameron , Daniel Campello , Andy Shevchenko , Stephen Boyd , Douglas Anderson , Enrico Granata , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , linux-iio@vger.kernel.org Subject: [PATCH v4 07/15] iio: sx9310: Use long instead of int for channel bitmaps Date: Mon, 3 Aug 2020 17:41:46 -0600 Message-Id: <20200803131544.v4.7.Iecaa50e469918a385b3e5dab375e442540ea2ad4@changeid> X-Mailer: git-send-email 2.28.0.163.g6104cc2f0b6-goog In-Reply-To: <20200803234154.320400-1-campello@chromium.org> References: <20200803234154.320400-1-campello@chromium.org> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Uses for_each_set_bit() macro to loop over channel bitmaps. Signed-off-by: Daniel Campello Reviewed-by: Andy Shevchenko Reviewed-by: Stephen Boyd --- Changes in v4: None Changes in v3: - Added static assert for number of channels. Changes in v2: - Changed prox_stat to chan_prox_stat bitmap. drivers/iio/proximity/sx9310.c | 39 ++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c index 20a26b73b5ae90..45569a0657060c 100644 --- a/drivers/iio/proximity/sx9310.c +++ b/drivers/iio/proximity/sx9310.c @@ -118,6 +118,7 @@ /* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */ #define SX9310_NUM_CHANNELS 4 +static_assert(SX9310_NUM_CHANNELS < BITS_PER_LONG); struct sx9310_data { /* Serialize access to registers and channel configuration */ @@ -129,7 +130,7 @@ struct sx9310_data { * Last reading of the proximity status for each channel. * We only send an event to user space when this changes. */ - bool prox_stat[SX9310_NUM_CHANNELS]; + unsigned long chan_prox_stat; bool trigger_enabled; /* Ensure correct alignment of timestamp when present. */ struct { @@ -139,7 +140,8 @@ struct sx9310_data { /* Remember enabled channels and sample rate during suspend. */ unsigned int suspend_ctrl0; struct completion completion; - unsigned int chan_read, chan_event; + unsigned long chan_read; + unsigned long chan_event; int channel_users[SX9310_NUM_CHANNELS]; unsigned int whoami; }; @@ -282,15 +284,16 @@ static const struct regmap_config sx9310_regmap_config = { }; static int sx9310_update_chan_en(struct sx9310_data *data, - unsigned int chan_read, - unsigned int chan_event) + unsigned long chan_read, + unsigned long chan_event) { int ret; + unsigned long channels = chan_read | chan_event; - if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) { + if ((data->chan_read | data->chan_event) != channels) { ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SENSOREN_MASK, - chan_read | chan_event); + channels); if (ret) return ret; } @@ -531,6 +534,7 @@ static void sx9310_push_events(struct iio_dev *indio_dev) unsigned int val, chan; struct sx9310_data *data = iio_priv(indio_dev); s64 timestamp = iio_get_time_ns(indio_dev); + unsigned long prox_changed; /* Read proximity state on all channels */ ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val); @@ -539,24 +543,23 @@ static void sx9310_push_events(struct iio_dev *indio_dev) return; } - for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) { + /* + * Only iterate over channels with changes on proximity status that have + * events enabled. + */ + prox_changed = (data->chan_prox_stat ^ val) & data->chan_event; + + for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) { int dir; u64 ev; - bool new_prox = val & BIT(chan); - - if (!(data->chan_event & BIT(chan))) - continue; - if (new_prox == data->prox_stat[chan]) - /* No change on this channel. */ - continue; - dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; + dir = val & BIT(chan) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, IIO_EV_TYPE_THRESH, dir); iio_push_event(indio_dev, ev, timestamp); - data->prox_stat[chan] = new_prox; } + data->chan_prox_stat = val; } static irqreturn_t sx9310_irq_thread_handler(int irq, void *private) @@ -713,13 +716,13 @@ static irqreturn_t sx9310_trigger_handler(int irq, void *private) static int sx9310_buffer_preenable(struct iio_dev *indio_dev) { struct sx9310_data *data = iio_priv(indio_dev); - unsigned int channels = 0; + unsigned long channels = 0; int bit, ret; mutex_lock(&data->mutex); for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) - channels |= BIT(indio_dev->channels[bit].channel); + __set_bit(indio_dev->channels[bit].channel, &channels); ret = sx9310_update_chan_en(data, channels, data->chan_event); mutex_unlock(&data->mutex);