Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp14447627pxu; Tue, 5 Jan 2021 01:36:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJykuKguRD+dHrkQEU/iD1uSNVUwRuEQdGSdCTj73y4ZL4DzN786bNpbzTZqtTU8/nbRLMtY X-Received: by 2002:a17:906:417:: with SMTP id d23mr70344184eja.19.1609839415912; Tue, 05 Jan 2021 01:36:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609839415; cv=none; d=google.com; s=arc-20160816; b=oc7RRG+zqHfwGT8iNXx7MBd7Mb3iF4BKQAZIvGbpmDI8Vtzi/R+irZcleU22PLEskg PXzUOqZlWLWmWbCRoCTwYydD7+uC49qBUtpFpAmektHxV54CuLHxZI2OgcHTHDz+KHbH m4Z8tDGc+Gqdw9BnQhVExKTnvVYfiXbNH+1mXA7lWLRMDIkL/rnK1MvFSMaO2kuOyUX+ pJDJBRBr6IJFdy0FoKAQfLceb26GoSspIHybpN/6bPBfy/J3osvFlGwrjYEl5HbZMFdn IZFx8pWWFo6s5JY8pWVvv33tSIvik0pcVxzxQEhpbAGFUZK7agkz67b8emoNHrWHRvq7 rzhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:ironport-sdr:ironport-sdr; bh=639grJDQOOae8Vy+b7V4S4cpRjehcGvjID1eYOSICTk=; b=ML8axdR4YN/vLNEmtejvA1ZF4PkcOLdpnWWZrkY273tXa5zc6hy+CZ6DrbA2TKaADq if16Ymqs3/fyH7IyXZw4CM3BWasUQOBAdtpF/jv4TvhJTQjVQOFah5rv7MEKrGdB2Om+ Zs3uXjBeT1daUH7WI6oDJjlYbmLgButEiatOZyfetnBm80ouFo3rdcbNRtxbM65mDF6N gqSPuQqR/rS4xwYON8j1ymgEk/dWMSzgX3LyRZkfRUSHsI+BkOLpFRVFDmODgevjZ7Mz 9LUwIVBfbGIotOgHdjrZvs0xWw8W53Updt/kITLvMuWvudKrrISUNHEioX1NXOZAopmK GX6Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b14si34264451edw.332.2021.01.05.01.36.31; Tue, 05 Jan 2021 01:36:55 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728321AbhAEJe7 (ORCPT + 99 others); Tue, 5 Jan 2021 04:34:59 -0500 Received: from mga17.intel.com ([192.55.52.151]:51522 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728304AbhAEJe6 (ORCPT ); Tue, 5 Jan 2021 04:34:58 -0500 IronPort-SDR: I3QepB09Bl0i9b/SDeD9mqOyFqqzJcsUGAbV1jnKG/7xm/9aZeXJnkvMUC4r39WAiI7UfPra6i V8R0KF4kkwuA== X-IronPort-AV: E=McAfee;i="6000,8403,9854"; a="156864089" X-IronPort-AV: E=Sophos;i="5.78,476,1599548400"; d="scan'208";a="156864089" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jan 2021 01:34:10 -0800 IronPort-SDR: A4v6glU6sqRp/Iox4CIhTC7Qdp/0HOMWvEQT3YzPEcWDtKXBnR5S+GaZlNyvmTBNHhvkiF31xS B338BpenEg0w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,476,1599548400"; d="scan'208";a="569665165" Received: from host.sh.intel.com ([10.239.154.115]) by fmsmga005.fm.intel.com with ESMTP; 05 Jan 2021 01:34:08 -0800 From: Ye Xiang To: jikos@kernel.org, jic23@kernel.org, srinivas.pandruvada@linux.intel.com Cc: linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Ye Xiang Subject: [PATCH 4/6] iio: hid-sensor-magn-3d: Add timestamp channel Date: Tue, 5 Jan 2021 17:35:13 +0800 Message-Id: <20210105093515.19135-5-xiang.ye@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210105093515.19135-1-xiang.ye@intel.com> References: <20210105093515.19135-1-xiang.ye@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Each sample has a timestamp field with this change. This timestamp may be from the sensor hub when present or local kernel timestamp. And the unit of timestamp is nanosecond. Signed-off-by: Ye Xiang --- drivers/iio/magnetometer/hid-sensor-magn-3d.c | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c b/drivers/iio/magnetometer/hid-sensor-magn-3d.c index cacbd053b2c6..b79c8a5a98f9 100644 --- a/drivers/iio/magnetometer/hid-sensor-magn-3d.c +++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c @@ -24,6 +24,7 @@ enum magn_3d_channel { CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP, CHANNEL_SCAN_INDEX_NORTH_MAGN, CHANNEL_SCAN_INDEX_NORTH_TRUE, + CHANNEL_SCAN_INDEX_TIMESTAMP, MAGN_3D_CHANNEL_MAX, }; @@ -47,6 +48,7 @@ struct magn_3d_state { struct common_attributes magn_flux_attr; struct common_attributes rot_attr; + s64 timestamp; }; static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = { @@ -57,6 +59,7 @@ static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = { HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH, HID_USAGE_SENSOR_ORIENT_MAGN_NORTH, HID_USAGE_SENSOR_ORIENT_TRUE_NORTH, + HID_USAGE_SENSOR_TIME_TIMESTAMP, }; static const u32 magn_3d_sensitivity_addresses[] = { @@ -132,7 +135,8 @@ static const struct iio_chan_spec magn_3d_channels[] = { BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ) | BIT(IIO_CHAN_INFO_HYSTERESIS), - } + }, + IIO_CHAN_SOFT_TIMESTAMP(7) }; /* Adjust channel real bits based on report descriptor */ @@ -281,13 +285,6 @@ static const struct iio_info magn_3d_info = { .write_raw = &magn_3d_write_raw, }; -/* Function to push data to buffer */ -static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data) -{ - dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); - iio_push_to_buffers(indio_dev, data); -} - /* Callback handler to send event after all samples are received and captured */ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, unsigned usage_id, @@ -297,8 +294,15 @@ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, struct magn_3d_state *magn_state = iio_priv(indio_dev); dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); - if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) - hid_sensor_push_data(indio_dev, magn_state->iio_vals); + if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) { + if (!magn_state->timestamp) + magn_state->timestamp = iio_get_time_ns(indio_dev); + + iio_push_to_buffers_with_timestamp(indio_dev, + magn_state->iio_vals, + magn_state->timestamp); + magn_state->timestamp = 0; + } return 0; } @@ -329,6 +333,11 @@ static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev, offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH) + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP; break; + case HID_USAGE_SENSOR_TIME_TIMESTAMP: + magn_state->timestamp = + hid_sensor_convert_timestamp(&magn_state->magn_flux_attributes, + *(s64 *)raw_data); + return ret; default: return -EINVAL; } @@ -394,9 +403,10 @@ static int magn_3d_parse_report(struct platform_device *pdev, return -ENOMEM; } - st->iio_vals = devm_kcalloc(&pdev->dev, attr_count, - sizeof(u32), - GFP_KERNEL); + /* attr_count include timestamp channel, and the iio_vals should be aligned to 8byte */ + st->iio_vals = devm_kcalloc(&pdev->dev, + ((attr_count + 1) % 2 + (attr_count + 1) / 2) * 2, + sizeof(u32), GFP_KERNEL); if (!st->iio_vals) { dev_err(&pdev->dev, "failed to allocate space for iio values array\n"); @@ -412,11 +422,13 @@ static int magn_3d_parse_report(struct platform_device *pdev, (_channels[*chan_count]).scan_index = *chan_count; (_channels[*chan_count]).address = i; - /* Set magn_val_addr to iio value address */ - st->magn_val_addr[i] = &(st->iio_vals[*chan_count]); - magn_3d_adjust_channel_bit_mask(_channels, - *chan_count, - st->magn[i].size); + if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) { + /* Set magn_val_addr to iio value address */ + st->magn_val_addr[i] = &st->iio_vals[*chan_count]; + magn_3d_adjust_channel_bit_mask(_channels, + *chan_count, + st->magn[i].size); + } (*chan_count)++; } } -- 2.17.1