From patchwork Wed Apr 20 21:10:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820823 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 4A1C8C433F5 for ; Wed, 20 Apr 2022 21:11:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382341AbiDTVOB (ORCPT ); Wed, 20 Apr 2022 17:14:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382353AbiDTVN7 (ORCPT ); Wed, 20 Apr 2022 17:13:59 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B43F13B2A1; Wed, 20 Apr 2022 14:11:12 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id q3so2948555plg.3; Wed, 20 Apr 2022 14:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4ROcGHuFTnTL2fbWmLOSf06EmqWWnhDvcVWXtNXGstI=; b=dtdXqku7t/c6bQ12fBhLC3WNa1pewG3OxAFM2pWllY2YFRbL/fvdRm+dNoPywCBGzH 8n61Dcox8RxUuf8vq4xG/GlzYPdVS4CsiN0iVdSe++BPHsf6SHwQ10GXbPo8Ev39u+el 8uH/8rr5l2EUcNbb2L5yV9XcC6Kxa06VpjMID4CxYJU6pAnqhps8pW6ANQuKBlNPOucU QOixqOzZK82Z7eJDRxtA5tHa91frQT8mp7/hE88REUpwA3D+VhUfnQOJW/LISEBGEm6O u8+YKAzTnMgKg8Wb8vAgJz5BEkUY3CBHUBOC2rr8RA66CiW7ocVMfZfqgj9UgfoFfDY6 QcPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4ROcGHuFTnTL2fbWmLOSf06EmqWWnhDvcVWXtNXGstI=; b=mu495PcW3YtFr0PXogAhL3Slo+iE2TU+tzIT+26CK4QYT1xwj2mX1qPZNY0cveuSy5 EX+m9JtYRI9c4nX1KPlnVRSoSeu7VS6INMGZH1RnY12lS8fvQzWg5ZTNnOyMtSSo2A0o u6X3SrasCc1G/lVkYOL3sW9SCkkQDQv09cyzz4/EfL9hrGRCa7i5W6N5NWxcJ8Lvpe2m sMoVCsEalO9UcJycjIuJVRxTnrakXAmqwlNpY5FaCFQD4rH8SE3NhG4DGTz0mWWap0nS Fampqlefq2F/JhLuyycchyUmVlnn7NbcMcFIeK6tOesvKsEM59taGRWp/ACkNdEeTjqH wrOQ== X-Gm-Message-State: AOAM532KOLYdvI6r+7FvL6HDQEMUp61m7698gFgjb4pGW+4h4opsYzJ7 7EBybrFMv/TvhCpUPmREboA= X-Google-Smtp-Source: ABdhPJzGBxzQznDitw8/o43TMI+x10hD2vaevjzwGsjFxXXg1UWDzcO3RqAFHJYZcrv33zs6/dqI1w== X-Received: by 2002:a17:902:bd06:b0:158:8973:b16b with SMTP id p6-20020a170902bd0600b001588973b16bmr22532902pls.129.1650489072179; Wed, 20 Apr 2022 14:11:12 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:11 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/9] iio: accel: bma400: Fix the scale min and max macro values Date: Thu, 21 Apr 2022 02:40:57 +0530 Message-Id: <20220420211105.14654-2-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Changing the scale macro values to match the bma400 sensitivity for 1 LSB of all the available ranges. Fixes: 465c811f1f20 ("iio: accel: Add driver for the BMA400") Signed-off-by: Jagath Jog J Reviewed-by: Andy Shevchenko --- drivers/iio/accel/bma400.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index c4c8d74155c2..80330c7ce17f 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -83,8 +83,27 @@ #define BMA400_ACC_ODR_MIN_WHOLE_HZ 25 #define BMA400_ACC_ODR_MIN_HZ 12 -#define BMA400_SCALE_MIN 38357 -#define BMA400_SCALE_MAX 306864 +/* + * BMA400_SCALE_MIN macro value represents m/s^2 for 1 LSB before + * converting to micro values for +-2g range. + * + * For +-2g - 1 LSB = 0.976562 milli g = 0.009576 m/s^2 + * For +-4g - 1 LSB = 1.953125 milli g = 0.019153 m/s^2 + * For +-16g - 1 LSB = 7.8125 milli g = 0.076614 m/s^2 + * + * The raw value which is used to select the different ranges is determined + * by the first bit set position from the scale value, so BMA400_SCALE_MIN + * should be odd. + * + * Scale values for +-2g, +-4g, +-8g and +-16g are populated into bma400_scales + * array by left shifting BMA400_SCALE_MIN. + * e.g.: + * To select +-2g = 9577 << 0 = raw value to write is 0. + * To select +-8g = 9577 << 2 = raw value to write is 2. + * To select +-16g = 9577 << 3 = raw value to write is 3. + */ +#define BMA400_SCALE_MIN 9577 +#define BMA400_SCALE_MAX 76617 #define BMA400_NUM_REGULATORS 2 #define BMA400_VDD_REGULATOR 0 From patchwork Wed Apr 20 21:10:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820824 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 D2082C433F5 for ; Wed, 20 Apr 2022 21:11:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382353AbiDTVOG (ORCPT ); Wed, 20 Apr 2022 17:14:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382365AbiDTVOB (ORCPT ); Wed, 20 Apr 2022 17:14:01 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B58E93B556; Wed, 20 Apr 2022 14:11:14 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id o5so3126888pjr.0; Wed, 20 Apr 2022 14:11:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SM9i76rzlNMMohIuFLToG7B0unnUzDqCDVvWZiGt2yk=; b=NLwhTJBJXzAYGpisF+wKI09cW0hajVJDOqvJ4Q5rknjHXlu0rzvHf2fSF0uxqQqOhR 6HI+ribLLXelZI5jK3+DlRlLXFI3f5Y0AxKu/i6wb6Lj2rYCASJ4ZGq18Vh8tjsATwwk rCN7veSls1QEz4QnJNKtau1jRRjgUh7QFZu77l/vl/U9pYN7jYkHL0K+sP4zFTse0hXV ZIMxpBV3Da5nEPmdM3F9Uw3E6x1iYr1edqLTPFJBe5b12wTWE2oTlQfl/vDh6yzkeDC8 FV/Tm072sTXanefzOhlRAlKoKiOncJOv6oHkFY2G7LblHz9FT/bOo89MX5jK3FKfXD0a SmJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SM9i76rzlNMMohIuFLToG7B0unnUzDqCDVvWZiGt2yk=; b=Ymd2ei+1T8CxD2D1Ifl4ebCngaaQqlYYafFXi10cd3oZPJSUsWkvAz2VDNHuHdSdso M78xoRrP+FBuQPQ+Mdf3YHsdFOJ9kLANVOwVFOO4f4aP4e0qVe+V4nTbmxYlVkxahKQz 5k+ylnejtEhW4ShHc7neXgNhMWVh2SnSx+hmxhMGhiFS1QO8Be+iPOdJh5JNcBwJBb3d 4MIDuvGFkWWlT7aONk9FFqzgJq3rC8j/leFcB55EKuzBL+aYmRENb9Mkrx/N5CGk9uj6 fXDU6+kUGCLDXfSdpvMy7ow+p1wBTJzVTW/+yDodB3jPQv/Wjgf9NetcMz48t7KepPm6 1UzQ== X-Gm-Message-State: AOAM532mBJnZuHvHohH3E7PvaNoCmStYHCIWYxqJIr/OvQA9X8v1ma6s lSj0p9ePr3+M4bRUGA6a/Vw= X-Google-Smtp-Source: ABdhPJxZdomhYSJJl4Qu6izE+4I0Zzm0XYzeON0sHdxN+3ikBLr2pc/qBtOrN1AnYMHam3x2SXO9/g== X-Received: by 2002:a17:902:c792:b0:158:ba0c:cf6d with SMTP id w18-20020a170902c79200b00158ba0ccf6dmr22599298pla.131.1650489074176; Wed, 20 Apr 2022 14:11:14 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:13 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/9] iio: accel: bma400: Reordering of header files Date: Thu, 21 Apr 2022 02:40:58 +0530 Message-Id: <20220420211105.14654-3-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Reordering of header files and removing the iio/sysfs.h since custom attributes are not being used in the driver. Signed-off-by: Jagath Jog J Reviewed-by: Andy Shevchenko --- drivers/iio/accel/bma400_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 043002fe6f63..25ad1f7339bc 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -13,14 +13,14 @@ #include #include -#include -#include #include #include #include #include #include +#include + #include "bma400.h" /* From patchwork Wed Apr 20 21:10:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820825 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 92E1FC433FE for ; Wed, 20 Apr 2022 21:11:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382374AbiDTVOH (ORCPT ); Wed, 20 Apr 2022 17:14:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382373AbiDTVOE (ORCPT ); Wed, 20 Apr 2022 17:14:04 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B09DA3D1FC; Wed, 20 Apr 2022 14:11:16 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id n8so2955064plh.1; Wed, 20 Apr 2022 14:11:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OwclT365pHO8aTYKW7BCzuAnkLe27jWDU+2YGvjkHTQ=; b=jNGmdHnehF3Orer+BKDQZbSvHEwaRLtcMAD9CWvXp21PpZh5hibykaKdKUbSBs5dBv IHYBbQS7/qTjTtPB0u/nz8TLquOxAVGOaAIRgn5ZaLpYpgfXCoHrbjia6WkmMupV2A8A My4juQu8iRJ9NdWtzMsRrpTiGVVSLEhG85tHw8AVuunYypYd6VgcY5qP5cy6JXd6W/6n 0bRewqW+InS6zEFv9+NXU4WcV4/wgx801d9UCOzEVgVgvbynr4Zi9wMRXDzf49HTvk1H 8EPK7NrP6GUCbqIf9r13/5hcPtfICGPKF3E5YKiWJkkcGkTvJKHqR3FtBAyGc79joz4O svVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OwclT365pHO8aTYKW7BCzuAnkLe27jWDU+2YGvjkHTQ=; b=tbpHTw0TVxj1xwLLEvAt52mG0DrjC4s8CgVNW6Avok4c9/OwKRxHTq7b/1BE8kXwlU Q5uRnRjMwtcJJPNXH0wyXi5C3qxiYr/5RcynihpY+dNFzvCY1YUCt2PmTaRWgF/JpPOG DoXbMnTIjeSmixTX6Jd4ymcldfxlRe9DtADiibUmzwQpYkrUbpbkCdULnjUbN8/TSAm4 ukKnQGn1BUsCgmEaYtHRGbvSQanQlHUYlvk4w8iOb0+04xJE+Z/xHCrbckn+HYlOB28o ZvKRmhDqXo+7pKKDeN3PU67b9P9OVrAOz1z9MAIwr1+1LvlzXgkZ4+Hekq3gok1NhajV Bqug== X-Gm-Message-State: AOAM530FlazeRUJU37FDqaBDB+2yRlGlPuKlq7HaVQeDx4Y0/4dxIG91 j6xWOqsqE+EvsB9YOVIPbCEzRwHb51s= X-Google-Smtp-Source: ABdhPJz86fTB4mWoVZ9ChunRv/xKkZslL1OzyfAM79q4p7bQTAMNifYLCL38yqcRLGupJfSDiFc45Q== X-Received: by 2002:a17:90b:1644:b0:1d2:98ef:bd4f with SMTP id il4-20020a17090b164400b001d298efbd4fmr6582444pjb.228.1650489076160; Wed, 20 Apr 2022 14:11:16 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:15 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/9] iio: accel: bma400: conversion to device-managed function Date: Thu, 21 Apr 2022 02:40:59 +0530 Message-Id: <20220420211105.14654-4-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This is a conversion to device-managed by using devm_iio_device_register() inside probe function. Previously the bma400 was not put into power down mode in some error paths in probe where it now is, but that should cause no harm. The dev_set_drvdata() call, bma400_remove() function and hooks in the I2C and SPI driver struct is removed as devm_iio_device_register() function is used to automatically unregister on driver detach. Signed-off-by: Jagath Jog J Reviewed-by: Andy Shevchenko --- drivers/iio/accel/bma400.h | 2 - drivers/iio/accel/bma400_core.c | 77 ++++++++++++++++----------------- drivers/iio/accel/bma400_i2c.c | 8 ---- drivers/iio/accel/bma400_spi.c | 6 --- 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index 80330c7ce17f..1c8c47a9a317 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -113,6 +113,4 @@ extern const struct regmap_config bma400_regmap_config; int bma400_probe(struct device *dev, struct regmap *regmap, const char *name); -void bma400_remove(struct device *dev); - #endif diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 25ad1f7339bc..07674d89d978 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -560,6 +560,26 @@ static void bma400_init_tables(void) } } +static void bma400_regulators_disable(void *data_ptr) +{ + struct bma400_data *data = data_ptr; + + regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); +} + +static void bma400_power_disable(void *data_ptr) +{ + struct bma400_data *data = data_ptr; + int ret; + + mutex_lock(&data->mutex); + ret = bma400_set_power_mode(data, POWER_MODE_SLEEP); + mutex_unlock(&data->mutex); + if (ret) + dev_warn(data->dev, "Failed to put device into sleep mode (%pe)\n", + ERR_PTR(ret)); +} + static int bma400_init(struct bma400_data *data) { unsigned int val; @@ -569,13 +589,12 @@ static int bma400_init(struct bma400_data *data) ret = regmap_read(data->regmap, BMA400_CHIP_ID_REG, &val); if (ret) { dev_err(data->dev, "Failed to read chip id register\n"); - goto out; + return ret; } if (val != BMA400_ID_REG_VAL) { dev_err(data->dev, "Chip ID mismatch\n"); - ret = -ENODEV; - goto out; + return -ENODEV; } data->regulators[BMA400_VDD_REGULATOR].supply = "vdd"; @@ -589,27 +608,31 @@ static int bma400_init(struct bma400_data *data) "Failed to get regulators: %d\n", ret); - goto out; + return ret; } ret = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators); if (ret) { dev_err(data->dev, "Failed to enable regulators: %d\n", ret); - goto out; + return ret; } + ret = devm_add_action_or_reset(data->dev, bma400_regulators_disable, data); + if (ret) + return ret; + ret = bma400_get_power_mode(data); if (ret) { dev_err(data->dev, "Failed to get the initial power-mode\n"); - goto err_reg_disable; + return ret; } if (data->power_mode != POWER_MODE_NORMAL) { ret = bma400_set_power_mode(data, POWER_MODE_NORMAL); if (ret) { dev_err(data->dev, "Failed to wake up the device\n"); - goto err_reg_disable; + return ret; } /* * TODO: The datasheet waits 1500us here in the example, but @@ -618,19 +641,23 @@ static int bma400_init(struct bma400_data *data) usleep_range(1500, 2000); } + ret = devm_add_action_or_reset(data->dev, bma400_power_disable, data); + if (ret) + return ret; + bma400_init_tables(); ret = bma400_get_accel_output_data_rate(data); if (ret) - goto err_reg_disable; + return ret; ret = bma400_get_accel_oversampling_ratio(data); if (ret) - goto err_reg_disable; + return ret; ret = bma400_get_accel_scale(data); if (ret) - goto err_reg_disable; + return ret; /* * Once the interrupt engine is supported we might use the @@ -639,12 +666,6 @@ static int bma400_init(struct bma400_data *data) * channel. */ return regmap_write(data->regmap, BMA400_ACC_CONFIG2_REG, 0x00); - -err_reg_disable: - regulator_bulk_disable(ARRAY_SIZE(data->regulators), - data->regulators); -out: - return ret; } static int bma400_read_raw(struct iio_dev *indio_dev, @@ -822,32 +843,10 @@ int bma400_probe(struct device *dev, struct regmap *regmap, const char *name) indio_dev->num_channels = ARRAY_SIZE(bma400_channels); indio_dev->modes = INDIO_DIRECT_MODE; - dev_set_drvdata(dev, indio_dev); - - return iio_device_register(indio_dev); + return devm_iio_device_register(dev, indio_dev); } EXPORT_SYMBOL_NS(bma400_probe, IIO_BMA400); -void bma400_remove(struct device *dev) -{ - struct iio_dev *indio_dev = dev_get_drvdata(dev); - struct bma400_data *data = iio_priv(indio_dev); - int ret; - - mutex_lock(&data->mutex); - ret = bma400_set_power_mode(data, POWER_MODE_SLEEP); - mutex_unlock(&data->mutex); - - if (ret) - dev_warn(dev, "Failed to put device into sleep mode (%pe)\n", ERR_PTR(ret)); - - regulator_bulk_disable(ARRAY_SIZE(data->regulators), - data->regulators); - - iio_device_unregister(indio_dev); -} -EXPORT_SYMBOL_NS(bma400_remove, IIO_BMA400); - MODULE_AUTHOR("Dan Robertson "); MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor core"); MODULE_LICENSE("GPL"); diff --git a/drivers/iio/accel/bma400_i2c.c b/drivers/iio/accel/bma400_i2c.c index da104ffd3fe0..4f6e01a3b3a1 100644 --- a/drivers/iio/accel/bma400_i2c.c +++ b/drivers/iio/accel/bma400_i2c.c @@ -27,13 +27,6 @@ static int bma400_i2c_probe(struct i2c_client *client, return bma400_probe(&client->dev, regmap, id->name); } -static int bma400_i2c_remove(struct i2c_client *client) -{ - bma400_remove(&client->dev); - - return 0; -} - static const struct i2c_device_id bma400_i2c_ids[] = { { "bma400", 0 }, { } @@ -52,7 +45,6 @@ static struct i2c_driver bma400_i2c_driver = { .of_match_table = bma400_of_i2c_match, }, .probe = bma400_i2c_probe, - .remove = bma400_i2c_remove, .id_table = bma400_i2c_ids, }; diff --git a/drivers/iio/accel/bma400_spi.c b/drivers/iio/accel/bma400_spi.c index 51f23bdc0ea5..28e240400a3f 100644 --- a/drivers/iio/accel/bma400_spi.c +++ b/drivers/iio/accel/bma400_spi.c @@ -87,11 +87,6 @@ static int bma400_spi_probe(struct spi_device *spi) return bma400_probe(&spi->dev, regmap, id->name); } -static void bma400_spi_remove(struct spi_device *spi) -{ - bma400_remove(&spi->dev); -} - static const struct spi_device_id bma400_spi_ids[] = { { "bma400", 0 }, { } @@ -110,7 +105,6 @@ static struct spi_driver bma400_spi_driver = { .of_match_table = bma400_of_spi_match, }, .probe = bma400_spi_probe, - .remove = bma400_spi_remove, .id_table = bma400_spi_ids, }; From patchwork Wed Apr 20 21:11:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820826 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 EC934C433EF for ; Wed, 20 Apr 2022 21:11:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382377AbiDTVOI (ORCPT ); Wed, 20 Apr 2022 17:14:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382368AbiDTVOG (ORCPT ); Wed, 20 Apr 2022 17:14:06 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDB7847AFB; Wed, 20 Apr 2022 14:11:18 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id t4so2839589pgc.1; Wed, 20 Apr 2022 14:11:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=358PWS7msgQ1RPNVWPekZXGpRach+SDsp5gVHY2rRwI=; b=ceE+lgRtVyLiaQ9adyQOj6k+kHENGxqYJC05I7mCeSsw2+KA8HDbKa5HJNng8QT5vP Fd2fI69jqq0fcjVCacdkMZzGGqFz2bEErjhMABgM71SBRp0/G7ylsJMrRYxUC8EFY6nt B+ZZDMXJnCj/NW5fnShcvpVNVbZ654ORdE2TAwjbs4xtqWz0k072hLd9yZx8W3kdfbmY HIwrtsRf9PLWlpo5Syd2MNCgkHAacqALwBAGgW9WHwnYCEqj2te4NAEazDcoVwzAGQeV tohkyuImYmxf8rlWF8tropyxcrnCr0T/lRHOEpQQR4WzwJG7pbBUlimkGQ3P6Lgl8EjF SNig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=358PWS7msgQ1RPNVWPekZXGpRach+SDsp5gVHY2rRwI=; b=W1blF6EMPPH5j3wCqe7J/AvjFQ87pZ5a7VftWf+DqprLRSo5dwVT6iTGnOIhO6moJb ZITKJbwjvEUD3Ocd+GLIP8InNCToD/Tqwz85lxaa6tDMnYGKlPtnFtb8nuO5+xq3Osj+ SXpDrVhbsvv4lTxB1mWRV6njk7fO2XVWND+eOtfNrAK1exlX9naKWNQGHjDZD3XWrMpt L+/LezXCurs/e3Qn/XC+dB465Kw9pBVaERS7YtgCSKnItwC8TwSMqthDaBP4FO71Jxzx wSp91oGsKK0T6l85zXCLVkvV86fthKS3txEZ9KnTiZwlRV+90ec/zAo/11pmrxPISCap zNmA== X-Gm-Message-State: AOAM532lXz62g09r/KiHtAgxRSrmxTbT698Bo3WZ5e26YDbRAruQHuoH IsU9ourPrn9HOp58uZie+vY= X-Google-Smtp-Source: ABdhPJzX80F6GavIO1BJR+E+bxV0QnIM+c7eWHEVB/dm4LaZKc20TwgaEAvg8GM+9BERl3ZXQw8zzg== X-Received: by 2002:a05:6a00:170d:b0:50a:858e:2b6e with SMTP id h13-20020a056a00170d00b0050a858e2b6emr14770393pfc.48.1650489078190; Wed, 20 Apr 2022 14:11:18 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:17 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/9] iio: accel: bma400: Add triggered buffer support Date: Thu, 21 Apr 2022 02:41:00 +0530 Message-Id: <20220420211105.14654-5-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Added trigger buffer support to read continuous acceleration data from device with data ready interrupt which is mapped to INT1 pin. Signed-off-by: Jagath Jog J Reviewed-by: Andy Shevchenko --- drivers/iio/accel/Kconfig | 2 + drivers/iio/accel/bma400.h | 10 +- drivers/iio/accel/bma400_core.c | 162 +++++++++++++++++++++++++++++++- drivers/iio/accel/bma400_i2c.c | 2 +- drivers/iio/accel/bma400_spi.c | 2 +- 5 files changed, 170 insertions(+), 8 deletions(-) diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig index eac3f02662ae..958097814232 100644 --- a/drivers/iio/accel/Kconfig +++ b/drivers/iio/accel/Kconfig @@ -204,6 +204,8 @@ config BMA220 config BMA400 tristate "Bosch BMA400 3-Axis Accelerometer Driver" select REGMAP + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER select BMA400_I2C if I2C select BMA400_SPI if SPI help diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index 1c8c47a9a317..907e1a6c0a38 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -62,6 +62,13 @@ #define BMA400_ACC_CONFIG2_REG 0x1b #define BMA400_CMD_REG 0x7e +/* Interrupt registers */ +#define BMA400_INT_CONFIG0_REG 0x1f +#define BMA400_INT_CONFIG1_REG 0x20 +#define BMA400_INT1_MAP_REG 0x21 +#define BMA400_INT_IO_CTRL_REG 0x24 +#define BMA400_INT_DRDY_MSK BIT(7) + /* Chip ID of BMA 400 devices found in the chip ID register. */ #define BMA400_ID_REG_VAL 0x90 @@ -111,6 +118,7 @@ extern const struct regmap_config bma400_regmap_config; -int bma400_probe(struct device *dev, struct regmap *regmap, const char *name); +int bma400_probe(struct device *dev, struct regmap *regmap, int irq, + const char *name); #endif diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 07674d89d978..57910ccf9180 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -11,6 +11,7 @@ * - Create channel for sensor time */ +#include #include #include #include @@ -20,6 +21,10 @@ #include #include +#include +#include +#include +#include #include "bma400.h" @@ -61,6 +66,14 @@ struct bma400_data { struct bma400_sample_freq sample_freq; int oversampling_ratio; int scale; + struct iio_trigger *trig; + /* Correct time stamp alignment */ + struct { + __le16 buff[3]; + u8 temperature; + s64 ts __aligned(8); + } buffer ____cacheline_aligned; + __le16 status; }; static bool bma400_is_writable_reg(struct device *dev, unsigned int reg) @@ -152,7 +165,7 @@ static const struct iio_chan_spec_ext_info bma400_ext_info[] = { { } }; -#define BMA400_ACC_CHANNEL(_axis) { \ +#define BMA400_ACC_CHANNEL(_index, _axis) { \ .type = IIO_ACCEL, \ .modified = 1, \ .channel2 = IIO_MOD_##_axis, \ @@ -164,17 +177,32 @@ static const struct iio_chan_spec_ext_info bma400_ext_info[] = { BIT(IIO_CHAN_INFO_SCALE) | \ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ .ext_info = bma400_ext_info, \ + .scan_index = _index, \ + .scan_type = { \ + .sign = 's', \ + .realbits = 12, \ + .storagebits = 16, \ + .endianness = IIO_LE, \ + }, \ } static const struct iio_chan_spec bma400_channels[] = { - BMA400_ACC_CHANNEL(X), - BMA400_ACC_CHANNEL(Y), - BMA400_ACC_CHANNEL(Z), + BMA400_ACC_CHANNEL(0, X), + BMA400_ACC_CHANNEL(1, Y), + BMA400_ACC_CHANNEL(2, Z), { .type = IIO_TEMP, .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), + .scan_index = 3, + .scan_type = { + .sign = 's', + .realbits = 8, + .storagebits = 8, + .endianness = IIO_LE, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(4), }; static int bma400_get_temp_reg(struct bma400_data *data, int *val, int *val2) @@ -659,6 +687,10 @@ static int bma400_init(struct bma400_data *data) if (ret) return ret; + /* Configure INT1 pin to open drain */ + ret = regmap_write(data->regmap, BMA400_INT_IO_CTRL_REG, 0x06); + if (ret) + return ret; /* * Once the interrupt engine is supported we might use the * data_src_reg, but for now ensure this is set to the @@ -807,6 +839,29 @@ static int bma400_write_raw_get_fmt(struct iio_dev *indio_dev, } } +static int bma400_data_rdy_trigger_set_state(struct iio_trigger *trig, + bool state) +{ + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); + struct bma400_data *data = iio_priv(indio_dev); + int ret; + + ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG0_REG, + BMA400_INT_DRDY_MSK, + FIELD_PREP(BMA400_INT_DRDY_MSK, state)); + if (ret) + return ret; + + return regmap_update_bits(data->regmap, BMA400_INT1_MAP_REG, + BMA400_INT_DRDY_MSK, + FIELD_PREP(BMA400_INT_DRDY_MSK, state)); +} + +static const unsigned long bma400_avail_scan_masks[] = { + GENMASK(3, 0), + 0 +}; + static const struct iio_info bma400_info = { .read_raw = bma400_read_raw, .read_avail = bma400_read_avail, @@ -814,7 +869,72 @@ static const struct iio_info bma400_info = { .write_raw_get_fmt = bma400_write_raw_get_fmt, }; -int bma400_probe(struct device *dev, struct regmap *regmap, const char *name) +static const struct iio_trigger_ops bma400_trigger_ops = { + .set_trigger_state = &bma400_data_rdy_trigger_set_state, + .validate_device = &iio_trigger_validate_own_device, +}; + +static irqreturn_t bma400_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct bma400_data *data = iio_priv(indio_dev); + int ret, temp; + + /* Lock to protect the data->buffer */ + mutex_lock(&data->mutex); + + /* bulk read six registers, with the base being the LSB register */ + ret = regmap_bulk_read(data->regmap, BMA400_X_AXIS_LSB_REG, + &data->buffer.buff, sizeof(data->buffer.buff)); + if (ret) + goto unlock_err; + + ret = regmap_read(data->regmap, BMA400_TEMP_DATA_REG, &temp); + if (ret) + goto unlock_err; + + data->buffer.temperature = temp; + + iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, + iio_get_time_ns(indio_dev)); + + mutex_unlock(&data->mutex); + iio_trigger_notify_done(indio_dev->trig); + return IRQ_HANDLED; + +unlock_err: + mutex_unlock(&data->mutex); + return IRQ_NONE; +} + +static irqreturn_t bma400_interrupt(int irq, void *private) +{ + struct iio_dev *indio_dev = private; + struct bma400_data *data = iio_priv(indio_dev); + int ret; + + /* Lock to protect the data->status */ + mutex_lock(&data->mutex); + ret = regmap_bulk_read(data->regmap, BMA400_INT_STAT0_REG, + &data->status, + sizeof(data->status)); + if (ret) + goto unlock_err; + + if (FIELD_GET(BMA400_INT_DRDY_MSK, le16_to_cpu(data->status))) { + mutex_unlock(&data->mutex); + iio_trigger_poll_chained(data->trig); + return IRQ_HANDLED; + } + +unlock_err: + mutex_unlock(&data->mutex); + return IRQ_NONE; +} + +int bma400_probe(struct device *dev, struct regmap *regmap, int irq, + const char *name) { struct iio_dev *indio_dev; struct bma400_data *data; @@ -841,8 +961,40 @@ int bma400_probe(struct device *dev, struct regmap *regmap, const char *name) indio_dev->info = &bma400_info; indio_dev->channels = bma400_channels; indio_dev->num_channels = ARRAY_SIZE(bma400_channels); + indio_dev->available_scan_masks = bma400_avail_scan_masks; indio_dev->modes = INDIO_DIRECT_MODE; + if (irq > 0) { + data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", + indio_dev->name, + iio_device_id(indio_dev)); + if (!data->trig) + return -ENOMEM; + + data->trig->ops = &bma400_trigger_ops; + iio_trigger_set_drvdata(data->trig, indio_dev); + + ret = devm_iio_trigger_register(data->dev, data->trig); + if (ret) + return dev_err_probe(data->dev, ret, + "iio trigger register fail\n"); + + indio_dev->trig = iio_trigger_get(data->trig); + ret = devm_request_threaded_irq(dev, irq, NULL, + &bma400_interrupt, + IRQF_TRIGGER_RISING | IRQF_ONESHOT, + indio_dev->name, indio_dev); + if (ret) + return dev_err_probe(data->dev, ret, + "request irq %d failed\n", irq); + } + + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, + &bma400_trigger_handler, NULL); + if (ret) + return dev_err_probe(data->dev, ret, + "iio triggered buffer setup failed\n"); + return devm_iio_device_register(dev, indio_dev); } EXPORT_SYMBOL_NS(bma400_probe, IIO_BMA400); diff --git a/drivers/iio/accel/bma400_i2c.c b/drivers/iio/accel/bma400_i2c.c index 4f6e01a3b3a1..1ba2a982ea73 100644 --- a/drivers/iio/accel/bma400_i2c.c +++ b/drivers/iio/accel/bma400_i2c.c @@ -24,7 +24,7 @@ static int bma400_i2c_probe(struct i2c_client *client, return PTR_ERR(regmap); } - return bma400_probe(&client->dev, regmap, id->name); + return bma400_probe(&client->dev, regmap, client->irq, id->name); } static const struct i2c_device_id bma400_i2c_ids[] = { diff --git a/drivers/iio/accel/bma400_spi.c b/drivers/iio/accel/bma400_spi.c index 28e240400a3f..ec13c044b304 100644 --- a/drivers/iio/accel/bma400_spi.c +++ b/drivers/iio/accel/bma400_spi.c @@ -84,7 +84,7 @@ static int bma400_spi_probe(struct spi_device *spi) if (ret) dev_err(&spi->dev, "Failed to read chip id register\n"); - return bma400_probe(&spi->dev, regmap, id->name); + return bma400_probe(&spi->dev, regmap, spi->irq, id->name); } static const struct spi_device_id bma400_spi_ids[] = { From patchwork Wed Apr 20 21:11:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820827 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 E534FC433F5 for ; Wed, 20 Apr 2022 21:11:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382382AbiDTVOJ (ORCPT ); Wed, 20 Apr 2022 17:14:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382376AbiDTVOH (ORCPT ); Wed, 20 Apr 2022 17:14:07 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1AE93C4B1; Wed, 20 Apr 2022 14:11:20 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id c23so2982219plo.0; Wed, 20 Apr 2022 14:11:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=t5sPZakAg0ct8LT+itigj1pjaZMC1J3HuLuF+Ekf3LU=; b=p9S1lVu1ckEtxXD+cH0NNYPc9j1avtaWzgb7Noh+h3pv0zGKL+nc6gkqlKz3q47cro +khmYcjTWJl2LBLWRFwLP1LlzalZMuUNdQWKS1mVIW8j9xdvzE4jKL6VmDBPfkHDEMOL VrxFhJjp28iL+501Dm7A6kiuKOsZcfMqs7CYnX215qc9ZBooFJNpRUwSxwMe04zFOgnn fXqWU+7wMkDiHSEjOQ3g0IZOhhgtSqgi1Ak5Dn06cnXYYo9DEqNC4BDLzs9SNBxsc7y/ LVEimhm6hnQq/CR9OWjeN8OD88MC+Qnpp2Rh11sJ9EGrWS7XGeGFdebmKQG0pJChfF4Y mXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t5sPZakAg0ct8LT+itigj1pjaZMC1J3HuLuF+Ekf3LU=; b=ku9ivwpPhfd3JJ0SUyIspHZ3lIHccU5037xkQOVu8OWglR5u9fDyjAVEqWAqHBAHPA 9ksI9UK8K0Zj/miH4dRfoKJa5SJh5LZ0C/CaFfN/d0qSPYcI0v7AckkUGTWBivfwhFf/ mdEgDE1WDNwp1G6USGQEVG60G+uQ+mRbzYHaM9r5FhASXA/vI1utamuqqn7DjIMYGVhs veRmKkt+KK/jDQEx6LXQEqEKXJc/eJZlkx8ZfwWfKyZC8W5BIoAXElhLRIdW/432LqKV psnIKmC58PspsIwEW5qT7ngtw63JuuFZtboQZFBKzmu7AlJgEFpsVFBTVPvgUulIsT6K a7TQ== X-Gm-Message-State: AOAM530OacpvruO3aDieAt92HAuLOqIl/iJtNLSMDAsBe5xofxTEuksK ZJ1u/Kr+XGI962X86HOMyUc= X-Google-Smtp-Source: ABdhPJwmAbmlO5UQJECtLFzG/OBi1lXS7QwYWUSkk8uflR7xgavuQnL3dfESEawIQaEo052HqmjKrw== X-Received: by 2002:a17:902:b948:b0:153:9994:b587 with SMTP id h8-20020a170902b94800b001539994b587mr22755078pls.68.1650489080160; Wed, 20 Apr 2022 14:11:20 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:19 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/9] iio: accel: bma400: Add separate channel for step counter Date: Thu, 21 Apr 2022 02:41:01 +0530 Message-Id: <20220420211105.14654-6-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Added channel for step counter which can be enable or disable through the sysfs interface. Signed-off-by: Jagath Jog J --- drivers/iio/accel/bma400.h | 2 + drivers/iio/accel/bma400_core.c | 67 +++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index 907e1a6c0a38..32c08f8b0b98 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -53,6 +53,8 @@ #define BMA400_STEP_CNT1_REG 0x16 #define BMA400_STEP_CNT3_REG 0x17 #define BMA400_STEP_STAT_REG 0x18 +#define BMA400_STEP_INT_MSK BIT(0) +#define BMA400_STEP_RAW_LEN 0x03 /* * Read-write configuration registers diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 57910ccf9180..aafb5a40944d 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -19,6 +19,9 @@ #include #include #include +#include + +#include #include #include @@ -67,6 +70,7 @@ struct bma400_data { int oversampling_ratio; int scale; struct iio_trigger *trig; + int steps_enabled; /* Correct time stamp alignment */ struct { __le16 buff[3]; @@ -202,6 +206,12 @@ static const struct iio_chan_spec bma400_channels[] = { .endianness = IIO_LE, }, }, + { + .type = IIO_STEPS, + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_ENABLE), + .scan_index = -1, /* No buffer support */ + }, IIO_CHAN_SOFT_TIMESTAMP(4), }; @@ -570,6 +580,38 @@ static int bma400_set_power_mode(struct bma400_data *data, return 0; } +static int bma400_enable_steps(struct bma400_data *data, int val) +{ + int ret; + + if (data->steps_enabled == val) + return 0; + + ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG1_REG, + BMA400_STEP_INT_MSK, + FIELD_PREP(BMA400_STEP_INT_MSK, !!val)); + data->steps_enabled = val; + return ret; +} + +static int bma400_get_steps_reg(struct bma400_data *data, int *val) +{ + u8 *steps_raw; + int ret; + + steps_raw = kmalloc(BMA400_STEP_RAW_LEN, GFP_KERNEL); + if (!steps_raw) + return -ENOMEM; + + ret = regmap_bulk_read(data->regmap, BMA400_STEP_CNT0_REG, + steps_raw, BMA400_STEP_RAW_LEN); + if (ret) + return ret; + *val = get_unaligned_le24(steps_raw); + kfree(steps_raw); + return IIO_VAL_INT; +} + static void bma400_init_tables(void) { int raw; @@ -709,10 +751,17 @@ static int bma400_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_PROCESSED: - mutex_lock(&data->mutex); - ret = bma400_get_temp_reg(data, val, val2); - mutex_unlock(&data->mutex); - return ret; + switch (chan->type) { + case IIO_TEMP: + mutex_lock(&data->mutex); + ret = bma400_get_temp_reg(data, val, val2); + mutex_unlock(&data->mutex); + return ret; + case IIO_STEPS: + return bma400_get_steps_reg(data, val); + default: + return -EINVAL; + } case IIO_CHAN_INFO_RAW: mutex_lock(&data->mutex); ret = bma400_get_accel_reg(data, chan, val); @@ -753,6 +802,9 @@ static int bma400_read_raw(struct iio_dev *indio_dev, *val = data->oversampling_ratio; return IIO_VAL_INT; + case IIO_CHAN_INFO_ENABLE: + *val = data->steps_enabled; + return IIO_VAL_INT; default: return -EINVAL; } @@ -818,6 +870,11 @@ static int bma400_write_raw(struct iio_dev *indio_dev, ret = bma400_set_accel_oversampling_ratio(data, val); mutex_unlock(&data->mutex); return ret; + case IIO_CHAN_INFO_ENABLE: + mutex_lock(&data->mutex); + ret = bma400_enable_steps(data, val); + mutex_unlock(&data->mutex); + return ret; default: return -EINVAL; } @@ -834,6 +891,8 @@ static int bma400_write_raw_get_fmt(struct iio_dev *indio_dev, return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: return IIO_VAL_INT; + case IIO_CHAN_INFO_ENABLE: + return IIO_VAL_INT; default: return -EINVAL; } From patchwork Wed Apr 20 21:11:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820829 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 100CBC433FE for ; Wed, 20 Apr 2022 21:11:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382418AbiDTVOZ (ORCPT ); Wed, 20 Apr 2022 17:14:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382389AbiDTVOK (ORCPT ); Wed, 20 Apr 2022 17:14:10 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE92A3C499; Wed, 20 Apr 2022 14:11:22 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id c12so2936142plr.6; Wed, 20 Apr 2022 14:11:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hAEmCD6wSI6Cxq48pLmVwhxY/XUwjnD/tjbd65QD6sg=; b=ILBjjIg1OOmmtzchzIeUBCU7CE7d/wGl9tJCQas8qMlNrKgt4uniw2Yjfu1KbSSGOG Tr1/OjO/No9rAbyZK2U4a+Se8hjWTA0vjaTOoYm7QrNc+E3PdE+lIm8fiFnm8pf1SyDO gQ8edKRMC5jsMZtKQBwRRm21xHvXLpNzth01bK65d3E577HTITjzn2w3SmhixBm1Pksx D3b1B/Y2vmL25tk/QKv4Qjw4Ng3+dBDNmU+OFb0CUPlAncGFLyJwVWqrwinRG2GVtASi MyVyXsYfBaigYXcmjpWkvbsAyIHjqwTmc48PHCnRxISVQE/D05OwrGylVhP8Dvl+yDZ+ tWDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hAEmCD6wSI6Cxq48pLmVwhxY/XUwjnD/tjbd65QD6sg=; b=5aGtzqIi613kIDYSDk0HjVzuJ9worodqk+V7Uo0XwZIcXERCqE4xsRfpVlXES5qtM5 krVEZX7qQH8hIwIRq6PzzXvL9U+NTuVTVCb3w/K9zovR7jxxLbdGL+Nki5WVNCKKRJU4 c8aKk9BDpLJaBWb6M5QoY3f7Y3a8Oym5eNa00hSEglywDdhS4tOtkZFZ8Z4z9apfjmjS 2r/khYHbDtrtFXbc0FqSqOW/lQFmrhP6CVEo79gu14fbFlS4rdXZQVey3/wZ/L6VPT+o rww/NaxjEuMrDdmG7Zyr5Z4B+J/I6X8wsDyu92t9I1lBeAVGu+F/8niEHV6BN0sYhw9i mOGg== X-Gm-Message-State: AOAM531Dzh9qsm5lUWi7vtEbwEDdcBDbZzpil8JR/4Yr3I/SaTfOfudy FS8YOoR+V8/OqPrGoX5UN1Y= X-Google-Smtp-Source: ABdhPJxNxEed8gWLg55uO/FAC4wXYxX5RnOWPtHDCXtxPR2R1U7XBqzhpYO9cA+84PuojFvD40i5jA== X-Received: by 2002:a17:902:8a95:b0:156:a40a:71e5 with SMTP id p21-20020a1709028a9500b00156a40a71e5mr22148356plo.144.1650489082273; Wed, 20 Apr 2022 14:11:22 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:22 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/9] iio: accel: bma400: Add step change event Date: Thu, 21 Apr 2022 02:41:02 +0530 Message-Id: <20220420211105.14654-7-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Added support for event when there is a detection of step change. INT1 pin is used to interrupt and event is pushed to userspace. Signed-off-by: Jagath Jog J --- drivers/iio/accel/bma400.h | 2 + drivers/iio/accel/bma400_core.c | 75 +++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index 32c08f8b0b98..0faa40fdbbf8 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -39,6 +39,7 @@ #define BMA400_INT_STAT0_REG 0x0e #define BMA400_INT_STAT1_REG 0x0f #define BMA400_INT_STAT2_REG 0x10 +#define BMA400_INT12_MAP_REG 0x23 /* Temperature register */ #define BMA400_TEMP_DATA_REG 0x11 @@ -55,6 +56,7 @@ #define BMA400_STEP_STAT_REG 0x18 #define BMA400_STEP_INT_MSK BIT(0) #define BMA400_STEP_RAW_LEN 0x03 +#define BMA400_STEP_STAT_MASK GENMASK(9, 8) /* * Read-write configuration registers diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index aafb5a40944d..fe101df7b773 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -71,6 +72,7 @@ struct bma400_data { int scale; struct iio_trigger *trig; int steps_enabled; + bool step_event_en; /* Correct time stamp alignment */ struct { __le16 buff[3]; @@ -169,6 +171,12 @@ static const struct iio_chan_spec_ext_info bma400_ext_info[] = { { } }; +static const struct iio_event_spec bma400_step_detect_event = { + .type = IIO_EV_TYPE_CHANGE, + .dir = IIO_EV_DIR_NONE, + .mask_separate = BIT(IIO_EV_INFO_ENABLE), +}; + #define BMA400_ACC_CHANNEL(_index, _axis) { \ .type = IIO_ACCEL, \ .modified = 1, \ @@ -211,6 +219,8 @@ static const struct iio_chan_spec bma400_channels[] = { .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | BIT(IIO_CHAN_INFO_ENABLE), .scan_index = -1, /* No buffer support */ + .event_spec = &bma400_step_detect_event, + .num_event_specs = 1, }, IIO_CHAN_SOFT_TIMESTAMP(4), }; @@ -898,6 +908,58 @@ static int bma400_write_raw_get_fmt(struct iio_dev *indio_dev, } } +static int bma400_read_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir) +{ + struct bma400_data *data = iio_priv(indio_dev); + + switch (chan->type) { + case IIO_STEPS: + return data->step_event_en; + default: + return -EINVAL; + } +} + +static int bma400_steps_event_enable(struct bma400_data *data, int state) +{ + int ret; + + ret = bma400_enable_steps(data, 1); + if (ret) + return ret; + + ret = regmap_update_bits(data->regmap, BMA400_INT12_MAP_REG, + BMA400_STEP_INT_MSK, + FIELD_PREP(BMA400_STEP_INT_MSK, + state)); + if (ret) + return ret; + data->step_event_en = state; + return 0; +} + +static int bma400_write_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, int state) +{ + struct bma400_data *data = iio_priv(indio_dev); + int ret; + + switch (chan->type) { + case IIO_STEPS: + mutex_lock(&data->mutex); + ret = bma400_steps_event_enable(data, state); + mutex_unlock(&data->mutex); + return ret; + default: + return -EINVAL; + } +} + static int bma400_data_rdy_trigger_set_state(struct iio_trigger *trig, bool state) { @@ -926,6 +988,8 @@ static const struct iio_info bma400_info = { .read_avail = bma400_read_avail, .write_raw = bma400_write_raw, .write_raw_get_fmt = bma400_write_raw_get_fmt, + .read_event_config = bma400_read_event_config, + .write_event_config = bma400_write_event_config, }; static const struct iio_trigger_ops bma400_trigger_ops = { @@ -971,6 +1035,7 @@ static irqreturn_t bma400_interrupt(int irq, void *private) { struct iio_dev *indio_dev = private; struct bma400_data *data = iio_priv(indio_dev); + s64 timestamp = iio_get_time_ns(indio_dev); int ret; /* Lock to protect the data->status */ @@ -981,6 +1046,16 @@ static irqreturn_t bma400_interrupt(int irq, void *private) if (ret) goto unlock_err; + if (FIELD_GET(BMA400_STEP_STAT_MASK, le16_to_cpu(data->status))) { + iio_push_event(indio_dev, + IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD, + IIO_EV_DIR_NONE, + IIO_EV_TYPE_CHANGE, 0, 0, 0), + timestamp); + mutex_unlock(&data->mutex); + return IRQ_HANDLED; + } + if (FIELD_GET(BMA400_INT_DRDY_MSK, le16_to_cpu(data->status))) { mutex_unlock(&data->mutex); iio_trigger_poll_chained(data->trig); From patchwork Wed Apr 20 21:11:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820828 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 A1033C433F5 for ; Wed, 20 Apr 2022 21:11:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382422AbiDTVOY (ORCPT ); Wed, 20 Apr 2022 17:14:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382376AbiDTVOW (ORCPT ); Wed, 20 Apr 2022 17:14:22 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C514C4831B; Wed, 20 Apr 2022 14:11:24 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id z16so3111628pfh.3; Wed, 20 Apr 2022 14:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tZQUlUpggsqshIBZIEqFH3v1VH36ZYRjLjiMyZLTFwc=; b=bnf14GjVBIGcHhC+P3YgPJtQ1Z/pXD69S2oSQTrUnVzsdeNgHz926H932krAynjflv qVNTrsTWpgluDCt9c/m1cp2dGxsFb76JYH37fgnvtSoVzHnvy8v/8rADT1smUT2C8XxW QoI21m2psVOFxXBgTA92vAw+6FwKLj+UjwM+8J0tKjzDNs/h99gZKv2Rs+BPdMcnCRiI sxreCvg8Oa+YK4xU/DkEx8p7QPVgjIiIxQcNGnLtKsOdKSjuCiseUqGn8KExJODFcMhJ lJq9fhVB220rlQ2vMCyzj++a5w/CnjjgiiZNRLbXG51aKYGfHUEJiuNR0nCs5NN52qgK kLNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tZQUlUpggsqshIBZIEqFH3v1VH36ZYRjLjiMyZLTFwc=; b=RKJWDzYfFXyOM9B6Jcbd0MohrPJyWZ+qd29u4SGvKBDYTccOqQtKQc5REefTmvAgVH jK+WgWjjo6/3eerIpT5PGwqotHpxpNR5SDxrRw1cr5aUamNPjHJ8T/t5pfm9m9okKiGc cAMws79lPpZBR+gz8ERwlJf0Qp/L6bXeTWjpxEM/O9h5uBIxfabGIUdJfqzFwKPpmrTU gAq+Jzzig278bOP+f2M7lzo331VVCO83fQssQaZgAnq4vso6HJtwYGvJ6t5OtQpn0ZDJ Yv7Oh8rVMQpPc+ysrmuIY93zaLsFeR4lg7PEs5r4Bo5gfUMFYiPQ8xhJCYMA0U6Rj7N5 cNlQ== X-Gm-Message-State: AOAM532fKec3NFLZUmwziZBF+I9HLY/YE2HYmrK55onktu9bsdSde2Xl Qm05tIhq83c9clBadm3Y08Y= X-Google-Smtp-Source: ABdhPJy+5y/C5QWAesVuAENR/MyABQBJtQ0Wtcxo39iM2N3+7MhJ1wJmOddd5UmFwGw/cWzvRw2mKw== X-Received: by 2002:a05:6a02:182:b0:374:5a57:cbf9 with SMTP id bj2-20020a056a02018200b003745a57cbf9mr20561451pgb.616.1650489084290; Wed, 20 Apr 2022 14:11:24 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:24 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 7/9] iio: accel: bma400: Add activity recognition support Date: Thu, 21 Apr 2022 02:41:03 +0530 Message-Id: <20220420211105.14654-8-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support for activity recognition like STILL, WALKING, RUNNING and these events are pushed to the userspace whenever the STEP interrupt occurs. Signed-off-by: Jagath Jog J --- drivers/iio/accel/bma400_core.c | 86 +++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index fe101df7b773..073fff7d64a3 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -60,6 +60,12 @@ struct bma400_sample_freq { int uhz; }; +enum bma400_activity { + BMA400_STILL, + BMA400_WALKING, + BMA400_RUNNING, +}; + struct bma400_data { struct device *dev; struct regmap *regmap; @@ -73,6 +79,7 @@ struct bma400_data { struct iio_trigger *trig; int steps_enabled; bool step_event_en; + bool activity_event_en; /* Correct time stamp alignment */ struct { __le16 buff[3]; @@ -177,6 +184,12 @@ static const struct iio_event_spec bma400_step_detect_event = { .mask_separate = BIT(IIO_EV_INFO_ENABLE), }; +static const struct iio_event_spec bma400_activity_event = { + .type = IIO_EV_TYPE_CHANGE, + .dir = IIO_EV_DIR_NONE, + .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE), +}; + #define BMA400_ACC_CHANNEL(_index, _axis) { \ .type = IIO_ACCEL, \ .modified = 1, \ @@ -198,6 +211,16 @@ static const struct iio_event_spec bma400_step_detect_event = { }, \ } +#define BMA400_ACTIVITY_CHANNEL(_chan2) { \ + .type = IIO_ACTIVITY, \ + .modified = 1, \ + .channel2 = _chan2, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ + .scan_index = -1, /* No buffer support */ \ + .event_spec = &bma400_activity_event, \ + .num_event_specs = 1, \ +} + static const struct iio_chan_spec bma400_channels[] = { BMA400_ACC_CHANNEL(0, X), BMA400_ACC_CHANNEL(1, Y), @@ -222,6 +245,9 @@ static const struct iio_chan_spec bma400_channels[] = { .event_spec = &bma400_step_detect_event, .num_event_specs = 1, }, + BMA400_ACTIVITY_CHANNEL(IIO_MOD_STILL), + BMA400_ACTIVITY_CHANNEL(IIO_MOD_WALKING), + BMA400_ACTIVITY_CHANNEL(IIO_MOD_RUNNING), IIO_CHAN_SOFT_TIMESTAMP(4), }; @@ -660,6 +686,20 @@ static void bma400_power_disable(void *data_ptr) ERR_PTR(ret)); } +static enum iio_modifier bma400_act_to_mod(enum bma400_activity activity) +{ + switch (activity) { + case BMA400_STILL: + return IIO_MOD_STILL; + case BMA400_WALKING: + return IIO_MOD_WALKING; + case BMA400_RUNNING: + return IIO_MOD_RUNNING; + default: + return IIO_NO_MOD; + } +} + static int bma400_init(struct bma400_data *data) { unsigned int val; @@ -758,6 +798,7 @@ static int bma400_read_raw(struct iio_dev *indio_dev, { struct bma400_data *data = iio_priv(indio_dev); int ret; + unsigned int activity; switch (mask) { case IIO_CHAN_INFO_PROCESSED: @@ -769,6 +810,21 @@ static int bma400_read_raw(struct iio_dev *indio_dev, return ret; case IIO_STEPS: return bma400_get_steps_reg(data, val); + case IIO_ACTIVITY: + ret = regmap_read(data->regmap, BMA400_STEP_STAT_REG, + &activity); + if (ret) + return ret; + /* + * The device does not support confidence value levels, + * so we will always have 100% for current activity and + * 0% for the others. + */ + if (chan->channel2 == bma400_act_to_mod(activity)) + *val = 100; + else + *val = 0; + return IIO_VAL_INT; default: return -EINVAL; } @@ -918,6 +974,8 @@ static int bma400_read_event_config(struct iio_dev *indio_dev, switch (chan->type) { case IIO_STEPS: return data->step_event_en; + case IIO_ACTIVITY: + return data->activity_event_en; default: return -EINVAL; } @@ -955,6 +1013,18 @@ static int bma400_write_event_config(struct iio_dev *indio_dev, ret = bma400_steps_event_enable(data, state); mutex_unlock(&data->mutex); return ret; + case IIO_ACTIVITY: + mutex_lock(&data->mutex); + if (!data->step_event_en) { + ret = bma400_steps_event_enable(data, true); + if (ret) { + mutex_unlock(&data->mutex); + return ret; + } + } + data->activity_event_en = state; + mutex_unlock(&data->mutex); + return 0; default: return -EINVAL; } @@ -1037,6 +1107,7 @@ static irqreturn_t bma400_interrupt(int irq, void *private) struct bma400_data *data = iio_priv(indio_dev); s64 timestamp = iio_get_time_ns(indio_dev); int ret; + unsigned int act; /* Lock to protect the data->status */ mutex_lock(&data->mutex); @@ -1052,6 +1123,21 @@ static irqreturn_t bma400_interrupt(int irq, void *private) IIO_EV_DIR_NONE, IIO_EV_TYPE_CHANGE, 0, 0, 0), timestamp); + + if (data->activity_event_en) { + ret = regmap_read(data->regmap, BMA400_STEP_STAT_REG, + &act); + if (ret) + goto unlock_err; + + iio_push_event(indio_dev, + IIO_EVENT_CODE(IIO_ACTIVITY, 0, + bma400_act_to_mod(act), + IIO_EV_DIR_NONE, + IIO_EV_TYPE_CHANGE, 0, + 0, 0), + timestamp); + } mutex_unlock(&data->mutex); return IRQ_HANDLED; } From patchwork Wed Apr 20 21:11:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820830 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 DAD4CC433EF for ; Wed, 20 Apr 2022 21:11:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382450AbiDTVOZ (ORCPT ); Wed, 20 Apr 2022 17:14:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382419AbiDTVOY (ORCPT ); Wed, 20 Apr 2022 17:14:24 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 191FB483BC; Wed, 20 Apr 2022 14:11:27 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id z6-20020a17090a398600b001cb9fca3210so3254489pjb.1; Wed, 20 Apr 2022 14:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=br/mJIWvQycGaMYcFjzbZMt2KUCcKABdWBb1GxUivVQ=; b=SlfiELn3nd18umI6Db6JIdi1tHtLYQszGc3BnHDxYkPMQi7kwtBNf7lYn1e0/heeT/ Pa4sjXZCqdHfg0F7vPXFiebjPzJEMeB1rsX7V1/IVsF+Ftwje3CdXhxJW4oi7pO9C0MW KsnvHnGoD8ZV8uJNxvsffYtzXs0gniOPPWIs3Xwh4bNSMjJHTUDGmr5DJnNi1AYltJSF EXniVOHH2FWLzIjRcsUJzzNW431F0aTEIciRd2sJvEi0ZjekJfRSOMlawXGOUrmDOnDs X1/Qu/5FaROzkUKDeqgpIaOP/w6fc/WvjaP+he+i2EjHRM478gbKSTpPUmXxtMJ7+4N6 Jhgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=br/mJIWvQycGaMYcFjzbZMt2KUCcKABdWBb1GxUivVQ=; b=mSCgG/wfffIN7KrAgd0+0fE+PLktJzf9fEewC+k6gVRYW7xG+9o9OHbIK4eb4bD79G Abwzj/y+XZwBZ4GvQOToI+aXzy7LmJlkWzhFiBMWjjWc/Lh8Vc6BLfSadoxrfhoNze3Q FBQDL69wDiXQ28wydLO8Eq4XFmw9qf4v5GxSzCdwSHyApWSwy4S0+SjPnKwOFaRRvBRE ROCRfvYqdmbfbKPNvRZLNK2ikZK3w/65rBMyRG/GujUx0mn+lKFmHUtatt/Uc9dIlYdD yJmyzkPBbLW7RAS8Zm2utgctBy2fQTKT534fsmjG/tI0SpqkEjRvS6kP8YDAQTH9542Y GqlQ== X-Gm-Message-State: AOAM531tnJpAmgdywyOu7frbmjA5lTk9PVaqVc6W9xuRkEHTdW4ECkGg glLB7E/z3YrBHShNvqCPqH0rfxWfH5Q= X-Google-Smtp-Source: ABdhPJxVayMEedPoG5tEg+a0LzNW0Ew1TEiv3qCjfZrSSfbqtfxaA87lQAqAccWC2XoPn+U3/wHCwg== X-Received: by 2002:a17:903:11d0:b0:155:c240:a2c0 with SMTP id q16-20020a17090311d000b00155c240a2c0mr22251166plh.143.1650489086410; Wed, 20 Apr 2022 14:11:26 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:26 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 8/9] iio: accel: bma400: Add debugfs register access support Date: Thu, 21 Apr 2022 02:41:04 +0530 Message-Id: <20220420211105.14654-9-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support to read/write byte from/to bma400 device from the userspace using debugfs interface. Signed-off-by: Jagath Jog J --- drivers/iio/accel/bma400_core.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 073fff7d64a3..5b1b28972ef9 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -1030,6 +1030,19 @@ static int bma400_write_event_config(struct iio_dev *indio_dev, } } +static int bma400_debugfs_reg_access(struct iio_dev *indio_dev, + unsigned int reg, + unsigned int writeval, + unsigned int *readval) +{ + struct bma400_data *data = iio_priv(indio_dev); + + if (readval) + return regmap_read(data->regmap, reg, readval); + else + return regmap_write(data->regmap, reg, writeval); +} + static int bma400_data_rdy_trigger_set_state(struct iio_trigger *trig, bool state) { @@ -1060,6 +1073,7 @@ static const struct iio_info bma400_info = { .write_raw_get_fmt = bma400_write_raw_get_fmt, .read_event_config = bma400_read_event_config, .write_event_config = bma400_write_event_config, + .debugfs_reg_access = bma400_debugfs_reg_access, }; static const struct iio_trigger_ops bma400_trigger_ops = { From patchwork Wed Apr 20 21:11:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagath Jog J X-Patchwork-Id: 12820831 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 87214C433EF for ; Wed, 20 Apr 2022 21:11:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382386AbiDTVO2 (ORCPT ); Wed, 20 Apr 2022 17:14:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382429AbiDTVOZ (ORCPT ); Wed, 20 Apr 2022 17:14:25 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5427748895; Wed, 20 Apr 2022 14:11:29 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id h1so3080908pfv.12; Wed, 20 Apr 2022 14:11:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KDtBCxBd42917rOqXMzoKh0kTGTy3MMplQzqh97uOrE=; b=leVno3F6MPxz3zgD389/T6skxK3PYaLyvgJUy1oT1/Posuzcdj5K6P7LnrKB8WrVlr FSF1gdIOeFqlHFXQDHU1QLI8km40OLkGoYJEbcUac/IGVSQSISX2yh5/tE5IxnEKhXvA vInoy3zWdYc830thx/jpIJsa0UAzclRxOhSgbP0iMwyjAJZARMRdVvXpni/aDiasuZtW QvwN1ka3xhQFaHXEv73EQY/5+8k0tWPvX5a/V4trd90Mm2Ofx+6LscPCWiCuSjiA8GT5 VqVuXkhymubn5/JFOoDDUdr3/7+0Rrj5TgiskoUv9LioMCrE1o9fro9CYRuc3n1AvAFe Qqtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KDtBCxBd42917rOqXMzoKh0kTGTy3MMplQzqh97uOrE=; b=d+tbEogarwb8owRbRI8AMEacb2mFT2e136E+PmuyDMX0A5Y1GgckjmR8sAOJ9NCJ+a 7JQY7GP0MnLT9XebnfbL7KozGaTyHrIx3Zz04c9YzGyxc5fhmBuURnn6Z4nveIDv1/1X Ct80OQQ3b22fH7A+xCa2GQslSWyD5LlXsN+97kTZvrWLLS9avL+94U3DjHXWBA074Iq3 Ink8rO6w9YyG3xdUscT5A15S3Fi45eUz9eDd3WgYW6KCudGn4wTHNziaUzEIsrdB9hHZ pH7vVDFE9QuENy7u1E10lbBUiEKtTaWouQJP9AcCksSpNSAlbwJUkLxHP2hbCqtBZ7Ys 6LGQ== X-Gm-Message-State: AOAM5324s7d29XOEYJq2wwbsov5H5GaOhfg90dIxFUkoN0YwpzacTKj5 +qtip+9uMi3WqGyxNfUKTD4= X-Google-Smtp-Source: ABdhPJx/ZihlUfA4rHRIIlAi5Giz+e3/2dkMI55BqgXciW2x8svo92/tzsdDz+qCsaLYDn3AXOzAHA== X-Received: by 2002:a63:5007:0:b0:3aa:1670:e327 with SMTP id e7-20020a635007000000b003aa1670e327mr12742585pgb.124.1650489088522; Wed, 20 Apr 2022 14:11:28 -0700 (PDT) Received: from localhost.localdomain ([27.7.104.83]) by smtp.gmail.com with ESMTPSA id v13-20020a17090a00cd00b001cd4989fee4sm106652pjd.48.2022.04.20.14.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 14:11:28 -0700 (PDT) From: Jagath Jog J To: dan@dlrobertson.com, jic23@kernel.org, andy.shevchenko@gmail.com Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 9/9] iio: accel: bma400: Add support for activity and inactivity events Date: Thu, 21 Apr 2022 02:41:05 +0530 Message-Id: <20220420211105.14654-10-jagathjog1996@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220420211105.14654-1-jagathjog1996@gmail.com> References: <20220420211105.14654-1-jagathjog1996@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add support for activity and inactivity events for all axis based on the threshold, duration and hysteresis value set from the userspace. INT1 pin is used to interrupt and event is pushed to userspace. Signed-off-by: Jagath Jog J Reported-by: kernel test robot --- drivers/iio/accel/bma400.h | 11 ++ drivers/iio/accel/bma400_core.c | 217 ++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) diff --git a/drivers/iio/accel/bma400.h b/drivers/iio/accel/bma400.h index 0faa40fdbbf8..e8f802a82300 100644 --- a/drivers/iio/accel/bma400.h +++ b/drivers/iio/accel/bma400.h @@ -94,6 +94,17 @@ #define BMA400_ACC_ODR_MIN_WHOLE_HZ 25 #define BMA400_ACC_ODR_MIN_HZ 12 +/* Generic interrupts register */ +#define BMA400_GEN1INT_CONFIG0 0x3f +#define BMA400_GEN2INT_CONFIG0 0x4A +#define BMA400_GEN_CONFIG1_OFF 0x01 +#define BMA400_GEN_CONFIG2_OFF 0x02 +#define BMA400_GEN_CONFIG3_OFF 0x03 +#define BMA400_GEN_CONFIG31_OFF 0x04 +#define BMA400_INT_GEN1_MSK BIT(2) +#define BMA400_INT_GEN2_MSK BIT(3) +#define BMA400_GEN_HYST_MSK GENMASK(1, 0) + /* * BMA400_SCALE_MIN macro value represents m/s^2 for 1 LSB before * converting to micro values for +-2g range. diff --git a/drivers/iio/accel/bma400_core.c b/drivers/iio/accel/bma400_core.c index 5b1b28972ef9..792336b3b9ca 100644 --- a/drivers/iio/accel/bma400_core.c +++ b/drivers/iio/accel/bma400_core.c @@ -80,6 +80,7 @@ struct bma400_data { int steps_enabled; bool step_event_en; bool activity_event_en; + unsigned int generic_event_en; /* Correct time stamp alignment */ struct { __le16 buff[3]; @@ -87,6 +88,7 @@ struct bma400_data { s64 ts __aligned(8); } buffer ____cacheline_aligned; __le16 status; + __be16 duration; }; static bool bma400_is_writable_reg(struct device *dev, unsigned int reg) @@ -190,6 +192,25 @@ static const struct iio_event_spec bma400_activity_event = { .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE), }; +static const struct iio_event_spec bma400_accel_event[] = { + { + .type = IIO_EV_TYPE_MAG, + .dir = IIO_EV_DIR_FALLING, + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | + BIT(IIO_EV_INFO_PERIOD) | + BIT(IIO_EV_INFO_HYSTERESIS) | + BIT(IIO_EV_INFO_ENABLE), + }, + { + .type = IIO_EV_TYPE_MAG, + .dir = IIO_EV_DIR_RISING, + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | + BIT(IIO_EV_INFO_PERIOD) | + BIT(IIO_EV_INFO_HYSTERESIS) | + BIT(IIO_EV_INFO_ENABLE), + }, +}; + #define BMA400_ACC_CHANNEL(_index, _axis) { \ .type = IIO_ACCEL, \ .modified = 1, \ @@ -209,6 +230,8 @@ static const struct iio_event_spec bma400_activity_event = { .storagebits = 16, \ .endianness = IIO_LE, \ }, \ + .event_spec = bma400_accel_event, \ + .num_event_specs = ARRAY_SIZE(bma400_accel_event) \ } #define BMA400_ACTIVITY_CHANNEL(_chan2) { \ @@ -972,6 +995,17 @@ static int bma400_read_event_config(struct iio_dev *indio_dev, struct bma400_data *data = iio_priv(indio_dev); switch (chan->type) { + case IIO_ACCEL: + switch (dir) { + case IIO_EV_DIR_RISING: + return FIELD_GET(BMA400_INT_GEN1_MSK, + data->generic_event_en); + case IIO_EV_DIR_FALLING: + return FIELD_GET(BMA400_INT_GEN2_MSK, + data->generic_event_en); + default: + return -EINVAL; + } case IIO_STEPS: return data->step_event_en; case IIO_ACTIVITY: @@ -999,6 +1033,63 @@ static int bma400_steps_event_enable(struct bma400_data *data, int state) return 0; } +static int bma400_activity_event_en(struct bma400_data *data, + enum iio_event_direction dir, + int state) +{ + int ret, reg, msk, value, field_value; + + switch (dir) { + case IIO_EV_DIR_RISING: + reg = BMA400_GEN1INT_CONFIG0; + msk = BMA400_INT_GEN1_MSK; + value = 2; + field_value = FIELD_PREP(msk, state); + break; + case IIO_EV_DIR_FALLING: + reg = BMA400_GEN2INT_CONFIG0; + msk = BMA400_INT_GEN2_MSK; + value = 0; + field_value = FIELD_PREP(msk, state); + break; + default: + return -EINVAL; + } + + /* Enabling all axis for interrupt evaluation */ + ret = regmap_write(data->regmap, reg, 0xF8); + if (ret) + return ret; + + /* OR combination of all axis for interrupt evaluation */ + ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG1_OFF, value); + if (ret) + return ret; + + /* Initial value to avoid interrupts while enabling*/ + ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG2_OFF, 0x0A); + if (ret) + return ret; + + /* Initial duration value to avoid interrupts while enabling*/ + ret = regmap_write(data->regmap, reg + BMA400_GEN_CONFIG31_OFF, 0x0F); + if (ret) + return ret; + + ret = regmap_update_bits(data->regmap, BMA400_INT1_MAP_REG, msk, + field_value); + if (ret) + return ret; + + ret = regmap_update_bits(data->regmap, BMA400_INT_CONFIG0_REG, msk, + field_value); + if (ret) + return ret; + + set_mask_bits(&data->generic_event_en, msk, field_value); + return 0; +} + static int bma400_write_event_config(struct iio_dev *indio_dev, const struct iio_chan_spec *chan, enum iio_event_type type, @@ -1008,6 +1099,11 @@ static int bma400_write_event_config(struct iio_dev *indio_dev, int ret; switch (chan->type) { + case IIO_ACCEL: + mutex_lock(&data->mutex); + ret = bma400_activity_event_en(data, dir, state); + mutex_unlock(&data->mutex); + return ret; case IIO_STEPS: mutex_lock(&data->mutex); ret = bma400_steps_event_enable(data, state); @@ -1030,6 +1126,108 @@ static int bma400_write_event_config(struct iio_dev *indio_dev, } } +static int get_gen_config_reg(enum iio_event_direction dir) +{ + switch (dir) { + case IIO_EV_DIR_FALLING: + return BMA400_GEN2INT_CONFIG0; + case IIO_EV_DIR_RISING: + return BMA400_GEN1INT_CONFIG0; + default: + return -EINVAL; + } +} + +static int bma400_read_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int *val, int *val2) +{ + struct bma400_data *data = iio_priv(indio_dev); + int ret, reg; + + reg = get_gen_config_reg(dir); + if (reg < 0) + return -EINVAL; + + *val2 = 0; + switch (info) { + case IIO_EV_INFO_VALUE: + ret = regmap_read(data->regmap, reg + BMA400_GEN_CONFIG2_OFF, + val); + if (ret) + return ret; + return IIO_VAL_INT; + case IIO_EV_INFO_PERIOD: + mutex_lock(&data->mutex); + ret = regmap_bulk_read(data->regmap, + reg + BMA400_GEN_CONFIG3_OFF, + &data->duration, sizeof(data->duration)); + if (ret) { + mutex_unlock(&data->mutex); + return ret; + } + *val = be16_to_cpu(data->duration); + mutex_unlock(&data->mutex); + return IIO_VAL_INT; + case IIO_EV_INFO_HYSTERESIS: + ret = regmap_read(data->regmap, reg, val); + if (ret) + return ret; + *val = FIELD_GET(BMA400_GEN_HYST_MSK, *val); + return IIO_VAL_INT; + default: + return -EINVAL; + } +} + +static int bma400_write_event_value(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + enum iio_event_info info, + int val, int val2) +{ + struct bma400_data *data = iio_priv(indio_dev); + int reg, ret; + + reg = get_gen_config_reg(dir); + if (reg < 0) + return -EINVAL; + + switch (info) { + case IIO_EV_INFO_VALUE: + if (val < 1 || val > 255) + return -EINVAL; + + return regmap_write(data->regmap, reg + BMA400_GEN_CONFIG2_OFF, + val); + case IIO_EV_INFO_PERIOD: + if (val < 1 || val > 65535) + return -EINVAL; + + mutex_lock(&data->mutex); + put_unaligned_be16(val, &data->duration); + ret = regmap_bulk_write(data->regmap, + reg + BMA400_GEN_CONFIG3_OFF, + &data->duration, + sizeof(data->duration)); + mutex_unlock(&data->mutex); + return ret; + case IIO_EV_INFO_HYSTERESIS: + if (val < 0 || val > 3) + return -EINVAL; + + return regmap_update_bits(data->regmap, reg, + BMA400_GEN_HYST_MSK, + FIELD_PREP(BMA400_GEN_HYST_MSK, val)); + default: + return -EINVAL; + } +} + static int bma400_debugfs_reg_access(struct iio_dev *indio_dev, unsigned int reg, unsigned int writeval, @@ -1074,6 +1272,8 @@ static const struct iio_info bma400_info = { .read_event_config = bma400_read_event_config, .write_event_config = bma400_write_event_config, .debugfs_reg_access = bma400_debugfs_reg_access, + .write_event_value = bma400_write_event_value, + .read_event_value = bma400_read_event_value, }; static const struct iio_trigger_ops bma400_trigger_ops = { @@ -1122,6 +1322,7 @@ static irqreturn_t bma400_interrupt(int irq, void *private) s64 timestamp = iio_get_time_ns(indio_dev); int ret; unsigned int act; + unsigned int ev_dir = IIO_EV_DIR_NONE; /* Lock to protect the data->status */ mutex_lock(&data->mutex); @@ -1131,6 +1332,22 @@ static irqreturn_t bma400_interrupt(int irq, void *private) if (ret) goto unlock_err; + if (FIELD_GET(BMA400_INT_GEN1_MSK, le16_to_cpu(data->status))) + ev_dir = IIO_EV_DIR_RISING; + + if (FIELD_GET(BMA400_INT_GEN2_MSK, le16_to_cpu(data->status))) + ev_dir = IIO_EV_DIR_FALLING; + + if (ev_dir != IIO_EV_DIR_NONE) { + iio_push_event(indio_dev, + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, + IIO_MOD_X_OR_Y_OR_Z, + IIO_EV_TYPE_MAG, ev_dir), + timestamp); + mutex_unlock(&data->mutex); + return IRQ_HANDLED; + } + if (FIELD_GET(BMA400_STEP_STAT_MASK, le16_to_cpu(data->status))) { iio_push_event(indio_dev, IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,