From patchwork Tue Jan 9 18:00:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: srinivas pandruvada X-Patchwork-Id: 186493 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp284069dyi; Tue, 9 Jan 2024 10:01:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IG2v6qts+iVKkZVD5/1cCNkfSPtJZa2gFZ509dB4ZmPwpeLoNIPrEKLZIG09bxP6zhF4VYd X-Received: by 2002:a05:6512:2249:b0:50e:b234:622a with SMTP id i9-20020a056512224900b0050eb234622amr3385640lfu.92.1704823310645; Tue, 09 Jan 2024 10:01:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704823310; cv=none; d=google.com; s=arc-20160816; b=jo5nn9Hf8F9fD8/XDEdG1eUxmXrTRURriWdYHe07mtF8KbaXC8yZvpildcMQe0K0pZ x5edCEW9N+Wml064LkvRNjy/txuQrbKPOCfaka4CM1J35E7OJ3rgcNNo7vVeRYDUtdfw Nb5ge/yMNjfkW/t3jIJQFhPmVvSSw3LGCJKoHt/1+Qd8keLHjB17FWJXFRhuPH5tWKLu NsMYC0m+cDUazUuqTBcJly0PCjTO9ur0s2kbkBxCSpBGoIMKsOGO82dSqOPjuVQrHqJT QWWKKdqQptCbh9MkWLERx4URzXdtyoZtPBsaudodjpM3/7Qbv/wvACUeNi9kfeeiFC7j 9p8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=F8ydjqu7H7KOueTzXABBzC5KQFd4AruADs0Vwe8FWrA=; fh=+qefaNsEkXb+6cE14coUwfWXDSnXoSFS2c07agj5LL0=; b=Z0WkwB7XovENx+PtdgN2LSg5FOouLZNlZD0vRn0HUUH9+UC56TgWGKTeI1rZOQ+JoQ HMQZK3y4GW5HlnplWLS3tl5fPL+6Y20tMcj1r1uN5uKQc1Hii3FYYhvmPItsShwdhziV a/dHH6g7z5oOzwcjdRXqRSMAgOhPqeSmk+/eQW63WLMCdOIersrS2YAEc7p6/qfxe8ti leTU63JRWyUQqvShCOsk9HpMh/AHrnORxd0OVonvzd+4Q9xJim1Ldpuc9TwQ4Yts8HVE CsRcYgsb6wVIz7zVLGpTV/mNmh7Fg0jkESNponMf1Urfb1DgVQhPK6SqOM2NpYbcH/FO 2o/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lfXMYMK2; spf=pass (google.com: domain of linux-kernel+bounces-21201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21201-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id v24-20020a1709060b5800b00a27a719a521si943026ejg.961.2024.01.09.10.01.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 10:01:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lfXMYMK2; spf=pass (google.com: domain of linux-kernel+bounces-21201-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21201-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 647071F26912 for ; Tue, 9 Jan 2024 18:01:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B548F3D0A9; Tue, 9 Jan 2024 18:00:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lfXMYMK2" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BA823BB25; Tue, 9 Jan 2024 18:00:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704823221; x=1736359221; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=oxuf2qv5qNCQwDrG+FQheyibqpQ4k7NBlmbN6ka5nqM=; b=lfXMYMK2uPAJmEFYDIHSgWra9t91SoSqWPJp6mCjch8lhI0TYaEck9NM 8FrpQpbKMkD1Fi9VlBci8z0A6vjdj7RjluUiokGCtxgwslwkWHO/dL7dM UBFRsniJQ9z1PVTDKl4+yUpfqKOQi6WkQZ+5qhDXLPX2a1wjeTcW/mzNO 6cmKjOaA6t5VionqWt63KTIRD3l8Ncm5Ae88Jjwcl70nId5zVURxI0wfB ABEvnpqRXAYACj/Ef5BX1yzDX9pbp2SHqW+BGEWWMZIVohyKlh7rUHwo1 5ptKHq/Ci3MYq6iqzk3vA/ZUP346FQujjOxQmYUjfKFtW09geAgAVPvGk w==; X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="429458612" X-IronPort-AV: E=Sophos;i="6.04,183,1695711600"; d="scan'208";a="429458612" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jan 2024 10:00:19 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10947"; a="852269564" X-IronPort-AV: E=Sophos;i="6.04,183,1695711600"; d="scan'208";a="852269564" Received: from spandruv-desk.jf.intel.com ([10.54.75.14]) by fmsmga004.fm.intel.com with ESMTP; 09 Jan 2024 10:00:18 -0800 From: Srinivas Pandruvada To: jikos@kernel.org, jic23@kernel.org, lars@metafoo.de, Basavaraj.Natikar@amd.com Cc: linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Srinivas Pandruvada Subject: [PATCH v3 1/4] iio: hid-sensor-als: Assign channels dynamically Date: Tue, 9 Jan 2024 10:00:04 -0800 Message-Id: <20240109180007.3373784-2-srinivas.pandruvada@linux.intel.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240109180007.3373784-1-srinivas.pandruvada@linux.intel.com> References: <20240109180007.3373784-1-srinivas.pandruvada@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787636807780389180 X-GMAIL-MSGID: 1787636807780389180 Instead of assuming that every channel defined statically by als_channels[] is present, assign dynamically based on presence of the respective usage id in the descriptor. This will allow to register ALS with limited channel support. Append the timestamp as the last channel. When not all usage ids are present, the scan index is adjusted to exclude unsupported channels. There is no intentional function changes done. Signed-off-by: Srinivas Pandruvada --- v3: Addressed comments from Jonthan: - Remove channel allocation and move to iio_priv() - Parse all usage IDs in a single loop and continue for failure. This way the temperature and chromaticity will not need any special processing to parse usage ids. - Don't leave empty channel indexes v2: New change drivers/iio/light/hid-sensor-als.c | 56 +++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c index 5cd27f04b45e..72a7c01c97f8 100644 --- a/drivers/iio/light/hid-sensor-als.c +++ b/drivers/iio/light/hid-sensor-als.c @@ -25,17 +25,26 @@ struct als_state { struct hid_sensor_hub_callbacks callbacks; struct hid_sensor_common common_attributes; struct hid_sensor_hub_attribute_info als[CHANNEL_SCAN_INDEX_MAX]; + struct iio_chan_spec channels[CHANNEL_SCAN_INDEX_MAX + 1]; struct { u32 illum[CHANNEL_SCAN_INDEX_MAX]; + u32 scan_index[CHANNEL_SCAN_INDEX_MAX]; u64 timestamp __aligned(8); } scan; int scale_pre_decml; int scale_post_decml; int scale_precision; int value_offset; + int num_channels; s64 timestamp; }; +/* The order of usage ids must match scan index starting from CHANNEL_SCAN_INDEX_INTENSITY */ +static const u32 als_usage_ids[] = { + HID_USAGE_SENSOR_LIGHT_ILLUM, + HID_USAGE_SENSOR_LIGHT_ILLUM, +}; + static const u32 als_sensitivity_addresses[] = { HID_USAGE_SENSOR_DATA_LIGHT, HID_USAGE_SENSOR_LIGHT_ILLUM, @@ -216,11 +225,14 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev, struct als_state *als_state = iio_priv(indio_dev); int ret = -EINVAL; u32 sample_data = *(u32 *)raw_data; + int scan_index; switch (usage_id) { case HID_USAGE_SENSOR_LIGHT_ILLUM: - als_state->scan.illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data; - als_state->scan.illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data; + scan_index = als_state->scan.scan_index[CHANNEL_SCAN_INDEX_INTENSITY]; + als_state->scan.illum[scan_index] = sample_data; + scan_index = als_state->scan.scan_index[CHANNEL_SCAN_INDEX_ILLUM]; + als_state->scan.illum[scan_index] = sample_data; ret = 0; break; case HID_USAGE_SENSOR_TIME_TIMESTAMP: @@ -237,27 +249,39 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev, /* Parse report which is specific to an usage id*/ static int als_parse_report(struct platform_device *pdev, struct hid_sensor_hub_device *hsdev, - struct iio_chan_spec *channels, unsigned usage_id, struct als_state *st) { - int ret; + struct iio_chan_spec *channels; + int ret, index = 0; int i; + channels = st->channels; + for (i = 0; i <= CHANNEL_SCAN_INDEX_ILLUM; ++i) { ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT, usage_id, - HID_USAGE_SENSOR_LIGHT_ILLUM, + als_usage_ids[i], &st->als[i]); if (ret < 0) - return ret; - als_adjust_channel_bit_mask(channels, i, st->als[i].size); + continue; + + channels[index] = als_channels[i]; + st->scan.scan_index[i] = index; + + als_adjust_channel_bit_mask(channels, index, st->als[i].size); + ++index; dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index, st->als[i].report_id); } + st->num_channels = index; + /* Return success even if one usage id is present */ + if (index) + ret = 0; + st->scale_precision = hid_sensor_format_scale(usage_id, &st->als[CHANNEL_SCAN_INDEX_INTENSITY], &st->scale_pre_decml, &st->scale_post_decml); @@ -293,15 +317,7 @@ static int hid_als_probe(struct platform_device *pdev) return ret; } - indio_dev->channels = devm_kmemdup(&pdev->dev, als_channels, - sizeof(als_channels), GFP_KERNEL); - if (!indio_dev->channels) { - dev_err(&pdev->dev, "failed to duplicate channels\n"); - return -ENOMEM; - } - ret = als_parse_report(pdev, hsdev, - (struct iio_chan_spec *)indio_dev->channels, hsdev->usage, als_state); if (ret) { @@ -309,8 +325,14 @@ static int hid_als_probe(struct platform_device *pdev) return ret; } - indio_dev->num_channels = - ARRAY_SIZE(als_channels); + /* Add timestamp channel */ + als_state->channels[als_state->num_channels] = als_channels[CHANNEL_SCAN_INDEX_TIMESTAMP]; + als_state->channels[als_state->num_channels].scan_index = als_state->num_channels; + + /* +1 for adding timestamp channel */ + indio_dev->num_channels = als_state->num_channels + 1; + + indio_dev->channels = als_state->channels; indio_dev->info = &als_info; indio_dev->name = name; indio_dev->modes = INDIO_DIRECT_MODE;