From patchwork Sun Oct 27 23:09:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gwendal Grignou X-Patchwork-Id: 11214423 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 BBF411747 for ; Sun, 27 Oct 2019 23:10:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8E9FD214E0 for ; Sun, 27 Oct 2019 23:10:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MgmyEigc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728614AbfJ0XKr (ORCPT ); Sun, 27 Oct 2019 19:10:47 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:37446 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728553AbfJ0XKG (ORCPT ); Sun, 27 Oct 2019 19:10:06 -0400 Received: by mail-pg1-f196.google.com with SMTP id p1so5383880pgi.4 for ; Sun, 27 Oct 2019 16:10:05 -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=SCl5z2eaTL650HgTHRN/eqR6Yxn6ClMA4d1O+k3PtWI=; b=MgmyEigcMJiw5EsHJWuOQbYV/fzp8/jId/rgw8AGezc77qqugbgG4vHfCHB66Bos9z gnTu4t5+QjJSC9CZerr9ZFHwNxq15J3C8O0bJuaTLxLBXT2W6BIwCMbisC4Vgg7XwdY1 a436Wdf5CEyKMiP2iPXdq1NP2hbTt2rcKjwRE= 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=SCl5z2eaTL650HgTHRN/eqR6Yxn6ClMA4d1O+k3PtWI=; b=Kyw4x25fUq5KOQc3LobvKe3PRyFUn2irT0AoZ67zcAEomPbhj6BoT62r+2KbprigEZ Wr/LZJ5E45eqvVzTTKCcZLcAkSaI3D/RFGC+zMHO1RAOSuTHzuYQswzV04Obgw5e1sYE 9uCPxKrolmYeG1XHCA6Dw/AaSSHQQB3lzS9wHKXnNxwIFjXjkqJBkEQPPb2QMN3I18H5 0LsB8NfehV0D77MfMiJ0NM3OXEGR8GOpElnCkr9eV5kkZd01PvDkNejrvebv0zkCFep8 KjicdqfCrmOD6x7lHKkbCdj15OuoYcwkPI9ZTPn9EgMRo+jG44ZiswyCodqHOci9WnWT QQAw== X-Gm-Message-State: APjAAAUJN9pKyxBzRU/e+7QHJjgX/G+/v67qNrRzIgIUPeZ9/RSki3C1 smUufiuAzhwA6peR1FxkjLGPHA== X-Google-Smtp-Source: APXvYqxjUvl2zOdBtcg8h25mMfe8v9fcOgfMA6sxYCQc4nLDeQsPBnMI66hb9AiQIrvoBInWVTTXfg== X-Received: by 2002:a63:cb4f:: with SMTP id m15mr18170844pgi.325.1572217805045; Sun, 27 Oct 2019 16:10:05 -0700 (PDT) Received: from localhost ([2620:15c:202:1:3c8f:512b:3522:dfaf]) by smtp.gmail.com with ESMTPSA id i7sm7634049pjs.1.2019.10.27.16.10.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Oct 2019 16:10:04 -0700 (PDT) From: Gwendal Grignou To: briannorris@chromium.org, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, lee.jones@linaro.org, bleung@chromium.org, enric.balletbo@collabora.com, dianders@chromium.org, groeck@chromium.org, fabien.lahoudere@collabora.com Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Gwendal Grignou Subject: [PATCH v3 11/18] platform: chrome: sensorhub: Add code to spread timestmap Date: Sun, 27 Oct 2019 16:09:14 -0700 Message-Id: <20191027230921.205251-12-gwendal@chromium.org> X-Mailer: git-send-email 2.24.0.rc0.303.g954a862665-goog In-Reply-To: <20191027230921.205251-1-gwendal@chromium.org> References: <20191027230921.205251-1-gwendal@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 EC FIFO can send sensor events in batch. Spread them based on previous (TSa) and currnet timestamp (TSb) EC FIFO iio events +-----------+ | TSa | +-----------+ +---------------------------------------+ | event 1 | | event 1 | TSb - (TSb - TSa)/n * (n-1) | +-----------+ +---------------------------------------+ | event 2 | | event 2 | TSb - (TSb - TSa)/n * (n-2) | +-----------+ +---------------------------------------+ | ... | ------> | .... | | +-----------+ +---------------------------------------+ | event n-1 | | event 2 | TSb - (TSb - TSa)/n | +-----------+ +---------------------------------------+ | event n | | event 2 | TSb | +-----------+ +---------------------------------------+ | TSb | +-----------+ Signed-off-by: Gwendal Grignou --- No changes in v3. Changes in v2: - Use CROS_EC_SENSOR_LAST_TS instead of LAST_TS to avoid name colisions. .../platform/chrome/cros_ec_sensorhub_ring.c | 99 ++++++++++++++++++- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c index d77e71303d80e..380807ef4cdde 100644 --- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c +++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c @@ -191,6 +191,97 @@ static bool cros_ec_ring_process_event( return true; } +/* + * cros_ec_ring_spread_add: Calculate proper timestamps then + * add to ringbuffer (legacy). + * + * If there is a sample with a proper timestamp + * timestamp | count + * older_unprocess_out --> TS1 | 1 + * TS1 | 2 + * out --> TS1 | 3 + * next_out --> TS2 | + * We spread time for the samples [older_unprocess_out .. out] + * between TS1 and TS2: [TS1+1/4, TS1+2/4, TS1+3/4, TS2]. + * + * If we reach the end of the samples, we compare with the + * current timestamp: + * + * older_unprocess_out --> TS1 | 1 + * TS1 | 2 + * out --> TS1 | 3 + * We know have [TS1+1/3, TS1+2/3, current timestamp] + */ +static void cros_ec_ring_spread_add( + struct cros_ec_sensorhub *sensorhub, + unsigned long sensor_mask, + s64 current_timestamp, + struct cros_ec_sensors_ring_sample *last_out) +{ + struct cros_ec_sensors_ring_sample *out; + int i; + + for_each_set_bit(i, &sensor_mask, BITS_PER_LONG) { + s64 older_timestamp; + s64 timestamp; + struct cros_ec_sensors_ring_sample *older_unprocess_out = + sensorhub->ring; + struct cros_ec_sensors_ring_sample *next_out; + int count = 1; + + for (out = sensorhub->ring; out < last_out; out = next_out) { + s64 time_period; + + next_out = out + 1; + if (out->sensor_id != i) + continue; + + /* Timestamp to start with */ + older_timestamp = out->timestamp; + + /* find next sample */ + while (next_out < last_out && next_out->sensor_id != i) + next_out++; + + if (next_out >= last_out) { + timestamp = current_timestamp; + } else { + timestamp = next_out->timestamp; + if (timestamp == older_timestamp) { + count++; + continue; + } + } + + /* + * The next sample has a new timestamp, + * spread the unprocessed samples. + */ + if (next_out < last_out) + count++; + time_period = div_s64(timestamp - older_timestamp, + count); + + for (; older_unprocess_out <= out; + older_unprocess_out++) { + if (older_unprocess_out->sensor_id != i) + continue; + older_timestamp += time_period; + older_unprocess_out->timestamp = + older_timestamp; + } + count = 1; + /* The next_out sample has a valid timestamp, skip. */ + next_out++; + older_unprocess_out = next_out; + } + } + + /* push the event into the kfifo */ + for (out = sensorhub->ring; out < last_out; out++) + cros_sensorhub_send_sample(sensorhub, out); +} + /* * cros_ec_sensorhub_ring_handler - the trigger handler function * @@ -324,9 +415,11 @@ static void cros_ec_sensorhub_ring_handler(struct cros_ec_sensorhub *sensorhub) } } - /* push the event into the kfifo */ - for (out = sensorhub->ring; out < last_out; out++) - cros_sensorhub_send_sample(sensorhub, out); + /* + * Spread samples in case of batching, then add them to the ringbuffer. + */ + cros_ec_ring_spread_add(sensorhub, sensor_mask, + current_timestamp, last_out); ring_handler_end: sensorhub->fifo_timestamp[CROS_EC_SENSOR_LAST_TS] = current_timestamp;