From nobody Sat Feb 7 18:21:12 2026 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C1E13915C8 for ; Thu, 22 Jan 2026 16:23:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099033; cv=none; b=ZuuiS/6yMYolX4xHBJjfwEQvWXnDT402aQxYLfTD0qvy8huMbgepX4rOH71GgwaCAORou4bK0yyEcEJQuIaLWgAiVLSj6f9yhbCqRCeFVukF4uF8CeBTL5iHEM9wSQlkZc7VgFHRK+rUaaktS2QKMzYA74PslMcZ2Nf6WumbJmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099033; c=relaxed/simple; bh=aBIjT8HJMfYZDveJouRj4sa520yQuhHwAmIb09eSjwY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mAOZ0lwNr3rqgCqJ3eRnA6SSyVDysd87cnTlell+pDSfnJj59IJl1wfcQEQbmN0r2zDTD+4j0MGsG7gqPVvMak6Y6gfZqU5yi3+HUhdizovz8KbEnQ6+zpqFe7sLqIPbh/cFP02zbHEgcHzL+wHo9LjRBP9c3Usly5z0kJ6LHLw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=L9NACxKx; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="L9NACxKx" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-47ee3da7447so9164565e9.0 for ; Thu, 22 Jan 2026 08:23:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1769099019; x=1769703819; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=RHmHZviAuhOhpKSUpCtTa1iFHF2rU7EWwgoioRxdGwg=; b=L9NACxKxJohW2pvYswoGX1tigiv5hopL5ESdVmwlCae1wLUQL51zDWRJGuhmbyNZ0y ts1kaQ8y7IBgvQFW9lst1l7dFyzYQuHoK7zsXnl3kD+GzvfPruw31KGXaTCyec6UUq2R jwmWrH/Go3NadphI/Rpc7i12r8jiW0eu0ctJNfhrChB0mEOsLlnN3Ik5H7sVl/GM+7G5 zpjjyBRpzRjpL8/BkrfB9wIDQdOJ/GdpfuM1DKrmK1xHhn9i39gb0QHIWb1rLX3hCB66 rw/uMiy8W7pMZhMDe8X1AKpy/Wii+6lGfr0SLsS1YedRm5L63O47aji9OW53Rku9DPTn xtCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769099019; x=1769703819; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=RHmHZviAuhOhpKSUpCtTa1iFHF2rU7EWwgoioRxdGwg=; b=FTn/+NvPOH8BijJS1XMqbmoHhEpyHHoWn3eYg/XAky6TQBPx6ClaBrF4ePN3O7Ekju Z8T+mblZwiQK6tUL5Z5lhkng8ShfDgCuGj9mgLmUvXqA1brEJ56XuUS0H2j5d+R1Oqt3 dWHPsZj+CYfi/1crBakd2tZOaxRiICzcqOtvwYYmt6mFBZFRUzKO3j7NL6BSLlSNN9uq wq4MDYjsuScmC/O00of9FOVz3CTrHixzEohRIYvV1Nm9nRLYBJMxCAJPJ71SDoXpI6ft r4JpPBLZT48Gpr0JTrgGP54xbba3c4aGEkPSlasE4M8QQbqGLXPUmcnTJE550u4FJmks cQ8Q== X-Forwarded-Encrypted: i=1; AJvYcCW6P+kmVvINOtSmQ+XCDC4+B77FmtC8HoLi6xLgN7b3d5FXvq+PKF6HuONbJ7kZOxoyrAko+qX0pTucg1Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyUwbGvusuKNATyU8j15Khe89fhZGfWZRZsJrrReDRCK0bfCKde akTa3w8A8RD6n1vC6LPstenWoBkkamumQDAHmKe8GXBJM4h3rZVNTzbQIIIvZCKAr90= X-Gm-Gg: AZuq6aIVcT2X2dQ+yGl4TeSwwGuoVFNiJ97FVp2BLZvdsRePxh7DYyBWECuUAWI4pVS vcNRi0YPq+G4TNuaCcjTF4YqRz5TrkB7I7o7VLTemrDxoPE+/+sjcpfDTk9nVYGQ7baDq9QqNcu Ty5y+FM5RmCgNLFd8KoPnH1dou++Hp6t2PW190pgKlB2nKXmfQkNGbRqsZskMIXvttWzPZ3mu5b +p9NF6hfONDsiTqXoW2KlXC38c8PH7H4u2SlhDzeLwEDZWoURqjv8fECJ+Yip7cxXVi7hNs2ZdY qvBQAHCWYh8K++4wDWu/3mPDKRjHDtYNuhfU6cI0By+/n0XVYLtILPrpZ5XvCL1HaDAyNfZKapn t2cs2DGuH5CRvD7UhQ8AjygO2sTqUfcY/8RrQJV3+2pJ8sobJg73/zmPOSA== X-Received: by 2002:a05:600c:8285:b0:477:9ce2:a0d8 with SMTP id 5b1f17b1804b1-4804c8f29f1mr4045955e9.0.1769099017865; Thu, 22 Jan 2026 08:23:37 -0800 (PST) Received: from localhost ([151.57.192.129]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4804706f14fsm78889845e9.13.2026.01.22.08.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jan 2026 08:23:37 -0800 (PST) From: Francesco Lavra To: Lorenzo Bianconi , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 1/4] iio: imu: st_lsm6dsx: Set FIFO ODR for accelerometer and gyroscope only Date: Thu, 22 Jan 2026 17:23:32 +0100 Message-Id: <20260122162335.2020006-2-flavra@baylibre.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260122162335.2020006-1-flavra@baylibre.com> References: <20260122162335.2020006-1-flavra@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1747; i=flavra@baylibre.com; h=from:subject; bh=aBIjT8HJMfYZDveJouRj4sa520yQuhHwAmIb09eSjwY=; b=owEB7QES/pANAwAKAe3xO3POlDZfAcsmYgBpck7oKHlBS2+YWZdjOWGvX/xqPbpahixKnyHdW ADRDgnr6CSJAbMEAAEKAB0WIQSGV4VPlTvcox7DFObt8TtzzpQ2XwUCaXJO6AAKCRDt8TtzzpQ2 XyJfDACg8mGeBAgcwaglN2TERZ6vbyZu9tlJLV7qdJo0WDYHYQhkwu9JvY7xW2rg/lUITTNTGBe PjBUCQp3CDwVk8PB5cC1pR5C0NSdgEpIE0yVbgLtQ4eBNEJKeAcwWp2MoJHTV1BDQSA4c0cjqcO 5Dbvr7bgbE2gVlZWcBFVeYZra9bH8CcrpkgRc7rjYTf7uKmiESIv6jb9AZSNEzShN9ZR+z0htXq DKbOI042A05tUe0yWsLyq1SyxqhjE3xMCCpx/aMQGiM4/4qZaH41MV9DrNsv54ZdT5odLW5JDMY 7PJbt2S0lcGmF2thgMNZrZpDrT9HNh0igeuGL5xikHb2uG2Ue8xYuhOpDaO3gw5fO0apiWQe2uZ /pgH4jtVFPou0KN36Lq4iOYkDj1JBRPcbErWB+PxIaVOvJiDAO0qD1CMydvlQoFuTD89NQ4LJgk ROVJJBHtOHv/xnGSLMeY4YoX0x/ja5hzuPGrxusJKXS1+JmgtimhgJHkfRjTKH5vhJwu0= X-Developer-Key: i=flavra@baylibre.com; a=openpgp; fpr=8657854F953BDCA31EC314E6EDF13B73CE94365F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The st_lsm6dsx_set_fifo_odr() function, which is called when enabling and disabling the hardware FIFO, checks the contents of the hw->settings->batch array at index sensor->id, and then sets the current ODR value in sensor registers that depend on whether the register address is set in the above array element. This logic is valid for internal sensors only, i.e. the accelerometer and gyroscope; however, since commit c91c1c844ebd ("iio: imu: st_lsm6dsx: add i2c embedded controller support"), this function is called also when configuring the hardware FIFO for external sensors (i.e. sensors accessed through the sensor hub functionality), which can result in unrelated device registers being written. Add a check to the beginning of st_lsm6dsx_set_fifo_odr() so that it does not touch any registers unless it is called for internal sensors. Fixes: c91c1c844ebd ("iio: imu: st_lsm6dsx: add i2c embedded controller sup= port") Signed-off-by: Francesco Lavra --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/i= mu/st_lsm6dsx/st_lsm6dsx_buffer.c index 55d877745575..1ee2fc5f5f1f 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c @@ -225,6 +225,10 @@ static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_s= ensor *sensor, const struct st_lsm6dsx_reg *batch_reg; u8 data; =20 + /* Only internal sensors have a FIFO ODR configuration register. */ + if (sensor->id >=3D ARRAY_SIZE(hw->settings->batch)) + return 0; + batch_reg =3D &hw->settings->batch[sensor->id]; if (batch_reg->addr) { int val; --=20 2.39.5 From nobody Sat Feb 7 18:21:12 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FF4A3EE651 for ; Thu, 22 Jan 2026 16:23:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099028; cv=none; b=JV6wRDmxMio6nT5XurypiH1s9oKIwTtlWuB1XdSXHQvy2lJiCZvWCB46p7VrY7+fyTkhYaFvDGqkaAUs64fKP0/sKhpGeCLz4yBokpdSVfQl36ZXZv63I3pcc3RDngwSEX0zJlEmpCicf043N/IUyP8K7L7cI2dPHRHsSeMLrt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099028; c=relaxed/simple; bh=MkM0A/HsyN7aXTv8muMJJ3kq/6hFQVpMJ9om7R8iWDM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qe853ym7lYzOsH11w6q+GURVl3/BtT1PILXPBm9m+IShn8eVpGROyy5TnkAtll69rae/Jwmjf8AMYQ2i9ZUehg6brOglwppqFvVRZR90ntCmDmP6UFYa8bZbnOJkhfk5YkyWU46YAwN/3pP3yGA6FTrEbqfMrufhRvce9j6lOQM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=EAH0f3ho; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="EAH0f3ho" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4801eb2c0a5so12144185e9.3 for ; Thu, 22 Jan 2026 08:23:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1769099019; x=1769703819; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=X2z+T2tcANuxuNpL/O66Pi5lTdKISXJnPtDtrt0skqs=; b=EAH0f3hoCmJ3qLW3HZyX21bK6V8dBE25jcvMv0ocLcyMDUvYkzlXEv3cYUlSfykMiL jSoUOmOriq4gcbpcx0EFS7PsHW9OxmuN9KKmmewayO3t9mYy5P5r5UvIbrukbGcn6LAj mjS7IVa/psdFqFFbZ7lN3q5zTBqHU8yqj51BckV5uCsz7+fEwPCRFaFanqoQtwqljqPN VYGQX6Qjzik78h/UuX5SMFBCeWSkVUOB5l+LlbtTozYh8BWi99qQGDGahWddf399Zn2n zbnIsEM4vFhZT3oUvM3GWy9uUgD3IAPs5xJQD59J5mudW2EG0ad0mY7xJ034Gw8s7cPX U8nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769099019; x=1769703819; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=X2z+T2tcANuxuNpL/O66Pi5lTdKISXJnPtDtrt0skqs=; b=rMypQVmvzUHokOhjB4jISXlgaGE7p+qd+ImxLNjfW+G25zsAehAya8HVw/EpkdYQyA NyEXArb0Opotg+REkt4EVQzM6qbNKdhTHrfj9LG2T+rtGzioTK/L+Ujkn1GkrIS/uAjS Fx0Podi7jUhr0uy6q8XhRpdFixWZtBSRmmQGfC1SKr1d0SeZMsWv7poE4PD9IjuCb8z2 nba97B0Eq5Y3WXpx9DXP8IAqn3mjEI2Al0L0lhHZVMDHqj/Ke+kXFd35ABWO0csy3jf9 d5VDbcELrtGdBV4tJB5fWKLMIXJtRFi5uh9opQ7/xfnedDCPkaQBJqUoNgVSvgYfyHrK VkoA== X-Forwarded-Encrypted: i=1; AJvYcCW3IrcghFk5QEBt7K6FcgQFaJG9ZDccbFziswivDgepKOJvgYdnlpkTEauJfYLxqyXjITSdmi1jjb24gyw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/hrjCbSmPV1mCX5Tl4xqjbwLOEpPxP6ZLP5I/bPh8pYyoSkhL aCl8mUj2L0LP7szvmGNNirmMZUKnn4J6POBSco/7ux0SIJC6KKvs/7UgbdWrJt1haLk= X-Gm-Gg: AZuq6aKzxhoxBxjtq9XIzp1N5aw+whZ93I6jTppqStXf2hiMK0pEsvs4vgqiGKnUEoc V/YgRLLEd34DRSzKCqtXbqEIDohgAOa96pjibRQHN5p6ZStfq/NTaO5H8bbYkN5LDydB97kHwrE c3hPkD4DznDDZxUS6PCohT6lpXhHQ4wH3uvTuEu06WCNy8O7KMYzKYOEqQlK4tDNf+zmcH6KzWE /x57FOcgyLN/S7whMryMZNJKWATOE0nwyx8fB3TF3a1j9pFqeC039WvlDNn6WBXqb3JDy52F1f7 CUdj66gQj2q4cOZFvZJLLW6Ri9U72Mtb9qUyROtGXjFIQB6RmAPCcZSendbd2Dt0MCnmKmS8Mf3 f2CK5jVoyJ/3CRZA2rRZEbpeJOfiICBr0MibHp/ULvdLMt58xiEt9QWsClLmpJdQNw+xz X-Received: by 2002:a05:600c:19ca:b0:480:3b4e:41b8 with SMTP id 5b1f17b1804b1-4804c9db040mr2784365e9.33.1769099019276; Thu, 22 Jan 2026 08:23:39 -0800 (PST) Received: from localhost ([151.57.192.129]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43596291e0asm17268482f8f.15.2026.01.22.08.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jan 2026 08:23:39 -0800 (PST) From: Francesco Lavra To: Lorenzo Bianconi , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 2/4] iio: imu: st_lsm6dsx: Set buffer sampling frequency for accelerometer only Date: Thu, 22 Jan 2026 17:23:33 +0100 Message-Id: <20260122162335.2020006-3-flavra@baylibre.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260122162335.2020006-1-flavra@baylibre.com> References: <20260122162335.2020006-1-flavra@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2119; i=flavra@baylibre.com; h=from:subject; bh=MkM0A/HsyN7aXTv8muMJJ3kq/6hFQVpMJ9om7R8iWDM=; b=owEB7QES/pANAwAKAe3xO3POlDZfAcsmYgBpck7o8/bTUHWtdx2A2C6OfJtUtmuStD+mgbvOu BhcilH85FCJAbMEAAEKAB0WIQSGV4VPlTvcox7DFObt8TtzzpQ2XwUCaXJO6AAKCRDt8TtzzpQ2 X9FaC/9hVNScf0X4967gBKmXzMyIjnUD3tvR3u7sGNDAD1UEmydk9fQ/0mLaPjN9Q9PflDEq8Z7 ujhXCfRMcFbnR5RJ/0VcJ8BMdjK8GYG4iEAPauj6RhCUuLN8nJn9ORtTZzGnYm2p4H4sx0eIVEG 4u+a8vKFf7TOeMq0Do12SdNpr+nHTR1kgLNvsBhn1I3f7PiKJoFUpcG+zRFrceIWmUZJnCZ/tHo S7J+qT3/TFBzsOR32Fnx2dMobLAXfUI/SxemaRJXc+UeduWq6UaGKH3Yn3DLWhrM8DHVTS1Z2gU TeWfLKQD8xVper+z7cKKmZ6q6OQ4eCSVb2lBZ1UgeUNqVjngQTEukMwCJJjnCKJOuvrQo+NEOiq x/vgGtshNZ0kpI5TkL64E8syxQG6H3o2AyIHcZ/WUmE+FcziLsIi8wDYK6oHuQfgqTTactcaUC8 Hqqs2w1u8ios7gvgrBpaaIwWcZz+ECX1B6P8s/91G2RLn01wxpaDqWpg2iTmhQOP2sVsM= X-Developer-Key: i=flavra@baylibre.com; a=openpgp; fpr=8657854F953BDCA31EC314E6EDF13B73CE94365F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The st_lsm6dsx_hwfifo_odr_store() function, which is called when userspace writes the buffer sampling frequency sysfs attribute, calls st_lsm6dsx_check_odr(), which accesses the odr_table array at index `sensor->id`; since this array is only 2 entries long, an access for any sensor type other than accelerometer or gyroscope is an out-of-bounds access. The motivation for being able to set a buffer frequency different from the sensor sampling frequency is to support use cases that need accurate event detection (which requires a high sampling frequency) while retrieving sensor data at low frequency. Since all the supported event types are generated from acceleration data only, do not create the buffer sampling frequency attribute for sensor types other than the accelerometer. Fixes: 6b648a36c200 ("iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO b= atch data rate") Signed-off-by: Francesco Lavra --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/i= mu/st_lsm6dsx/st_lsm6dsx_buffer.c index 1ee2fc5f5f1f..5b28a3ffcc3d 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c @@ -862,12 +862,21 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw) int i, ret; =20 for (i =3D 0; i < ST_LSM6DSX_ID_MAX; i++) { + const struct iio_dev_attr **attrs; + if (!hw->iio_devs[i]) continue; =20 + /* + * For the accelerometer, allow setting FIFO sampling frequency + * values different from the sensor sampling frequency, which + * may be needed to keep FIFO data rate low while sampling + * acceleration data at high rates for accurate event detection. + */ + attrs =3D i =3D=3D ST_LSM6DSX_ID_ACC ? st_lsm6dsx_buffer_attrs : NULL; ret =3D devm_iio_kfifo_buffer_setup_ext(hw->dev, hw->iio_devs[i], &st_lsm6dsx_buffer_ops, - st_lsm6dsx_buffer_attrs); + attrs); if (ret) return ret; } --=20 2.39.5 From nobody Sat Feb 7 18:21:12 2026 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02BB33E461C for ; Thu, 22 Jan 2026 16:23:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099033; cv=none; b=IJadD0ZqJEgYWvjYwy/QPfbeVDjVJ2tmpqnfZ0Sw0ZU0TiOdiONd7asSQrpd7rckK9OPTsXsiWi2B1nHPT9dV013a3CMaRobj2kHhYpN5ALETLbbr3RC6W44uCtItxaf5K5zRJieTncEv/P9hCbtGVMarW3w76gmZ3D9X1Qfg3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099033; c=relaxed/simple; bh=PfmODYCm5rMmK7m/DHbR719NzpB5H2VEDNy6yDKy03s=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qn+f/5WmS3Euu+wxBxzNnGGX3QHYVR2LUqIMScstNKHa77MzsOvf1VjXrXwC5jxKf0lmPOqEfCryVx6UbHahVYN1NUGakbKvh9SMDQ4kaZrIePqXm8ISIgAAyboLjLMa5XmPBGrU275JwJGP4wTfcMRWzZ9//QgdTFvco1fcMiQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=1VH1iz/d; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="1VH1iz/d" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-47ff94b46afso10187225e9.1 for ; Thu, 22 Jan 2026 08:23:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1769099020; x=1769703820; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Zl/f9Vr/KBTc2N5tZDEA6huhuIWhhr6+cS2Ujvjr4tM=; b=1VH1iz/dSJU9HzBxWfaymK0UBvO4d2Sv/Gjof+A0gFJLfgJd8RJnrWqo08fmM26U01 h6Q777fKIMTDCjj3E+Dcm0GpriYO9AE7JM+ZaEJv5pQ3hk/dj182Lc1hodFbC7iFRVMW /2AP3fy1sCkwQ8UK0yS3+Qt5Iwo24R0y4ZVwbVivQYFHMh4jP40nfMGMHyXL6O/c1HMs 9Kv0OQbd8oN9buvz4AgiBnNlCJluaza9NK1YYIUrdJVBfw/2qJirM2o4r55TjOkptAai A/3/Y4MhU3aebn9BHPkxqSGTNvk6ba9LZ4rM61fP30S7D938iNvYQAHBNT65filQsMXT abMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769099020; x=1769703820; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Zl/f9Vr/KBTc2N5tZDEA6huhuIWhhr6+cS2Ujvjr4tM=; b=DA31/rV7wBmMQp1nI7sQq3pougLC0v7sapVDcK4Wl9eSjkbYeEHj9BQ7GIuH7NkdwF 2uU9DVQSRQWkVlFCaLwu1g0ZF+IaMCFe/9Ib7rXM0oCEKrl3PePxuaJQ1OxPhD/pTFI4 XXWGq0HqDMgkx2f9MmpJAM+TsG2CsvtJe5tBRcfUDHoUKWdNpl8p9dh9aD5JQTYFuJDb iVLOmrvKDpo2RvCXWUcagfUYL3Mv4oj12HE9xpeeCoU5nMv84AZjRwtPuLETf4gIa0Y2 ar1QbdvZgZoNcKSGa563dtuhAW2JH80Sz8R1aEfw4HPTFRgSyRYEa7bR+OriI9pm5in6 1jig== X-Forwarded-Encrypted: i=1; AJvYcCW2QeWtuS+jz6qtKjDzgKpV5XsB7g8jUJ0m8QN/JbpBJTbHKJMyu3IkS+61Dwz4Owp+f7YqzDpD1DiTLd0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8BI2Y1VAC+pXfZ2Xci2lcpvESc8tkCPdGDnBiZN9RcqbzCdXW yz3WTa1jTXHSuGEYr2QkASgRTlmsH36rKnCBJWp8BEqF2WYUzBnSFvB+8ojav0dyhUg= X-Gm-Gg: AZuq6aKzc/ZwsYY6EfV5d00JtjKUY5D9HxfnfRERjVCcqV4Ap99ZYWur/opRhkbrL7f gEm9d6Uxu11ofBwcJyoroaA0kwF/Xi7MLSmdswzED0dq2UztlkEtlMSPYQ5s/Il17vDZ/WW05PZ /hC9FWUe0/Fu9NGpvtvKdRKMo2BJqXi1B4/gL0VL2tDgnW7FyhYt21zFvIcuY7k6dpc6v9Lf2fM UcRao7RYYc2OrS49iFQeU2AI60+V6368NHB7kcy5xcLZ7/iG+eW2hOboUigCW2BDCJjc5zh0oO8 4dXnyrfiEDtAxNKiQUoTXGSooGrStdvypxAQRiqaAXndJZPvnkllfJQGnA+k7ZoGWqF43ukgq4E dinmi133bi8/AxSlhf1sx/oCncb77h/nY9MSx461NWd8L1kJVKE2uh3S2iQ== X-Received: by 2002:a05:600c:1f8f:b0:479:1348:c63e with SMTP id 5b1f17b1804b1-480470829c1mr55210245e9.9.1769099020474; Thu, 22 Jan 2026 08:23:40 -0800 (PST) Received: from localhost ([151.57.192.129]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4804704b4e6sm78749905e9.7.2026.01.22.08.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jan 2026 08:23:40 -0800 (PST) From: Francesco Lavra To: Lorenzo Bianconi , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 3/4] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO Date: Thu, 22 Jan 2026 17:23:34 +0100 Message-Id: <20260122162335.2020006-4-flavra@baylibre.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260122162335.2020006-1-flavra@baylibre.com> References: <20260122162335.2020006-1-flavra@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2293; i=flavra@baylibre.com; h=from:subject; bh=PfmODYCm5rMmK7m/DHbR719NzpB5H2VEDNy6yDKy03s=; b=owEB7QES/pANAwAKAe3xO3POlDZfAcsmYgBpck7pgyhWYQgyBUGgQsIIVzZcw3+kO9V5IIeI5 AeGG5tZnKyJAbMEAAEKAB0WIQSGV4VPlTvcox7DFObt8TtzzpQ2XwUCaXJO6QAKCRDt8TtzzpQ2 XzKPC/0UFIw3hI7t5P5/KJCIcTKUlPND0fF1birPI3LD+r4vm8FWlo7nUniLVXG0KEYVgv10SoT /ss+N6BxgIMya8HiPYJfkVBJl/f5LSvotvEfkclqgqXnpAL21JhJwcOIL6JioHjH3rn1xRKjSWF KRfv7r7FFd3EI1+webyZbjlVnUAPgnAV/zGZ6Z/FdqvOsg/khaBiA6CDO0dbDTXsH4ecuA2BOQG KgzfpNVeLTB1Woo7W39AhQN7M0R/ePLC6qJYhTbd4QrWVMJ1LQSG2jK7zo0lER2xuc3DxAA7FBK k4jpW0IfmQYTEZ5oCRjvEdoGmtzO1j8Yu3LrQrj+AxRmA4vKDeMMXv2h4FnXrGYk3u5p8yJJaN1 Vr8LaUAbrY7RQi0VrwTTbIy5ypfzMnrkATxqjzP5DO7PQoRoMmJ81sUmJoYbPwEblfmYCWtCOaR o2sFbBGCa+Nrh9p0wOd3TKEvOBk8wazKZ8dx3yKBPz/X0E1KjmGrtCOq66M2bFPwnOry0= X-Developer-Key: i=flavra@baylibre.com; a=openpgp; fpr=8657854F953BDCA31EC314E6EDF13B73CE94365F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The DRDY_MASK feature implemented in sensor chips marks gyroscope and accelerometer invalid samples (i.e. samples that have been acquired during the settling time of sensor filters) with the special values 0x7FFFh, 0x7FFE, and 0x7FFD. The driver checks FIFO samples against these special values in order to discard invalid samples; however, it does the check regardless of the type of samples being processed, whereas this feature is specific to gyroscope and accelerometer data. This could cause valid samples to be discarded. Fix the above check so that it takes into account the type of samples being processed. In st_lsm6dsx_push_tagged_data(), change the type of the data parameter to __le16 *, to reflect the fact that this function is called with an aligned data argument and avoid casting to __le16 * when checking sample values. Fixes: 960506ed2c69 ("iio: imu: st_lsm6dsx: enable drdy-mask if available") Signed-off-by: Francesco Lavra --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/i= mu/st_lsm6dsx/st_lsm6dsx_buffer.c index 5b28a3ffcc3d..ded9a96076e6 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c @@ -539,14 +539,14 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd static int st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag, - u8 *data, s64 ts) + __le16 *data, s64 ts) { - s16 val =3D le16_to_cpu(*(__le16 *)data); struct st_lsm6dsx_sensor *sensor; struct iio_dev *iio_dev; =20 /* invalid sample during bootstrap phase */ - if (val >=3D ST_LSM6DSX_INVALID_SAMPLE) + if ((tag =3D=3D ST_LSM6DSX_GYRO_TAG || tag =3D=3D ST_LSM6DSX_ACC_TAG) && + (s16)le16_to_cpup(data) >=3D ST_LSM6DSX_INVALID_SAMPLE) return -EINVAL; =20 /* @@ -670,7 +670,8 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *h= w) reset_ts =3D true; ts *=3D hw->ts_gain; } else { - st_lsm6dsx_push_tagged_data(hw, tag, iio_buff, + st_lsm6dsx_push_tagged_data(hw, tag, + (__le16 *)iio_buff, ts); } } --=20 2.39.5 From nobody Sat Feb 7 18:21:12 2026 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6C6E3E4E9E for ; Thu, 22 Jan 2026 16:23:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099040; cv=none; b=RR14PkU0BALwH5L/O5RKaDuReqkEvuP2xjJTzx25Ivk9KoDW382Lx2T3/K/XnWbA2XORiEljCPagg4sSDsClHdpJOjG6iw2a9bkCKlbwZ7mOgDFabGMvq0GqeIvuqc+MbaR9Zlf6tnyvEj1Ko48kcN05m+InO9i4FMBNefQR5A4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769099040; c=relaxed/simple; bh=gYcpQHVgQnLOgJGtrdVMiTkl/cFBGJOufvAWWbB2Pes=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UNtFhCYVtdkRcayQDIDInsbUfUVHesEViGwig7NZHdFpNVylJsmIcrJb9n/k0nMwkAPZYpHmR33I5w7Qcv/6htD+Dv/oJK2Ohpo/jM5Zwy0QOIaPpAibn8B7g3ludPZy6HKrsXUnDWlI5KqFTlbNWXFH8oT7bfkJzd7gMDvNorY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=IVGS6sR1; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="IVGS6sR1" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4327790c4e9so791175f8f.2 for ; Thu, 22 Jan 2026 08:23:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1769099022; x=1769703822; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=1y3j9Rai6zK10EQ6cwCgVv+aGfsaXgppGmnGw7PuFrM=; b=IVGS6sR1ewlE/EcvgWO2HY0EKtlfS6tB/u+2i8t1tcuDAhqLbhWt6TB8VoQVLATv0O 9mIdtUyRCdJPhv563VPOzagPXrCWr9fsPzsYIsk6J+tKRfn1dqOWDZ+CqiKbBu+vnwl4 pUb5duVBRmV1186e0LiWaidG3pEeBNLhirARpjO3Z4IKu8NJOjPE+QzVgjD3U2ARCAgY wksInSYyhrFElo+xU2Kjhb2a8qEDQ6IhY22RZUcZdzRnVz627uFUAVVygpeOic4HumhS uzSbPwcA22KfKmJ4pJgcULWAuyIFG0ODEaH/svjOcKuqswvFJyQsk0/Juyd1FpiXeqxF /QoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769099022; x=1769703822; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1y3j9Rai6zK10EQ6cwCgVv+aGfsaXgppGmnGw7PuFrM=; b=LhbDe7WWgcr7/zGEodjZlKqbc4d3QLI94C14ay8Xw9TDgWBCVRg3gFwLT3s8nUZ6od YvPpJiV9PGFzuh2eRUFKTLNvU5eTohwj0cOnYZz4QkoVxD1xDtkbpjwrIqykORtQW4dF SxA0GdgztbD+n28fAHDe6tYPmrkrm61CdFrhdoMzmLZOOe998ZmMhG2MKB05wwZQ9/64 Q/myHQ92ZGM2Hz1GW4kHg+a929qJmg/u2zfSwY41GaQay/z677nfAnYYCw7Df4+3OtL6 O2IPLIbm91WAKqhBveWm7HfmL67bTipEsbmpyCJSFqPB1751QkcViGaHcrHgb/P4DwcE JGYQ== X-Forwarded-Encrypted: i=1; AJvYcCWHgrrzMzF6/0GwI3iENTCSUFZk072r/ZpZloe2EdTftlAY0PklbjLTGCpv9VI9VL32a9CfDP0xzWpdbuU=@vger.kernel.org X-Gm-Message-State: AOJu0YwnJi8q3evE/UnyUmd0oK4pP7gHoYMPvyHgFX78qakik3CBUIzq GCl47Mep9cGNZ2aTpSttX/cDj14k+olGPRfCVXPcjBgGztmxkRTA1Q32whuyQoFMqnI= X-Gm-Gg: AZuq6aJ30ItF2KbbnuIwqnsrgAMcubESE48FiZDVzIckm4Qkt4TPU8272Ul5hKK2FcO kezcAW08A9lo0SYZNYPaoZEK9TUdncVyoqke08FoyqbAqpu2TctDhOpi6hztVlJ/EfyaPHgz2iH N6GaJH7jEk1xNC1muTgdcXtsXe4pFSlNmRj/X5nMVKBmeQzQ3W3cCyk1fxCKgFmfYCg/HrbY8uS Jzmxm9t2NXHjoYHfZgBY+UjeZ26+D+8+aAhbiuIOiVR0/zYbCJolWuGnYfm4WvXNS+XwtCkVW32 PwJJ6L1vW4HQjwAg+xyt3UAYQioDO66wYLvI+iCsbGRcR/zxt7XvnakBN17SHRBDDld6tPX+KX/ bwwMvn/XvaXb97EHcsU/BsawKv25THUe26y62bAunCy4vQ/ABWK8Osd7TCg== X-Received: by 2002:a05:6000:208a:b0:431:488:b9b4 with SMTP id ffacd0b85a97d-435b159625amr181049f8f.17.1769099021722; Thu, 22 Jan 2026 08:23:41 -0800 (PST) Received: from localhost ([151.57.192.129]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-435966322a2sm16687710f8f.38.2026.01.22.08.23.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jan 2026 08:23:41 -0800 (PST) From: Francesco Lavra To: Lorenzo Bianconi , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 4/4] iio: imu: st_lsm6dsx: Add support for rotation sensor Date: Thu, 22 Jan 2026 17:23:35 +0100 Message-Id: <20260122162335.2020006-5-flavra@baylibre.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260122162335.2020006-1-flavra@baylibre.com> References: <20260122162335.2020006-1-flavra@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=15225; i=flavra@baylibre.com; h=from:subject; bh=gYcpQHVgQnLOgJGtrdVMiTkl/cFBGJOufvAWWbB2Pes=; b=owEB7QES/pANAwAKAe3xO3POlDZfAcsmYgBpck7p0Lcu8Mka4mh42gRVUjPN8dEPr1SXLPuXx MlkjShSIOWJAbMEAAEKAB0WIQSGV4VPlTvcox7DFObt8TtzzpQ2XwUCaXJO6QAKCRDt8TtzzpQ2 X0wvDACsA833KE3Gk9v1JR/iMrOrSMuw3QGeHHAKPfbyvGU2dVVeTNZnYHINAuGGDYlAtD4NbBn asyGdYkN+a+SyJ4supZc1vl9rwfbdc4+eWPtQ6e3NjRGRNdUbX9fMs3yzed5BmDTHSvFNrpr7QF aPa3VlRwZAQzr5c7/YvnovHNXVSgWhoWQhT5WTW1/yFeTkQWhWIf6oZf+stHbM2a6D84LGF5Fp/ G9gPTi+6tY32LiTm53bF5lGarK4Oe8Qr1pbCy0WSsgIF36wKemiW+WUMB84Dh/iU0UyreGWlmzY 2VK7HPXr0yMJMR4whfmkbPw7XDEBFaCTBZrU0SfqskFg63B0AWxRri9u4M1Q6jhDqk332H58/0G OMviMfl5pT72cgKK0geJer2dF8NUDpuuEFzE+CeAOlS160dwDXTGL5/naeEipAhaQP9Tefn7MyB IGyrOIlCPBPcADIJvnqYNSbawFBkVmOqk+AP6ugS20nxu+lGQWa6DXQACadUUClqRF5XM= X-Developer-Key: i=flavra@baylibre.com; a=openpgp; fpr=8657854F953BDCA31EC314E6EDF13B73CE94365F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some IMU chips in the LSM6DSX family have sensor fusion features that combine data from the accelerometer and gyroscope. One of these features generates rotation vector data and makes it available in the hardware FIFO as a quaternion (more specifically, the X, Y and Z components of the quaternion vector, expressed as 16-bit half-precision floating-point numbers). Add support for a new sensor instance that allows receiving sensor fusion data, by defining a new struct st_lsm6dsx_sf_settings (which contains chip-specific details for the sensor fusion functionality), and adding this struct as a new field in struct st_lsm6dsx_settings. In st_lsm6dsx_core.c, populate this new struct for the LSM6DSV and LSM6DSV16X chips, and add the logic to initialize an additional IIO device if this struct is populated for the hardware type being probed. Note: a new IIO device is being defined (as opposed to adding channels to an existing device) because the rate at which sensor fusion data is generated may not match the data rate from any of the existing devices. Tested on LSMDSV16X. Signed-off-by: Francesco Lavra Acked-by: Lorenzo Bianconi --- drivers/iio/imu/st_lsm6dsx/Makefile | 2 +- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h | 26 +- .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 16 +- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 58 +++++ .../iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c | 235 ++++++++++++++++++ 5 files changed, 330 insertions(+), 7 deletions(-) create mode 100644 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c diff --git a/drivers/iio/imu/st_lsm6dsx/Makefile b/drivers/iio/imu/st_lsm6d= sx/Makefile index 57cbcd67d64f..19a488254de3 100644 --- a/drivers/iio/imu/st_lsm6dsx/Makefile +++ b/drivers/iio/imu/st_lsm6dsx/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only st_lsm6dsx-y :=3D st_lsm6dsx_core.o st_lsm6dsx_buffer.o \ - st_lsm6dsx_shub.o + st_lsm6dsx_shub.o st_lsm6dsx_fusion.o =20 obj-$(CONFIG_IIO_ST_LSM6DSX) +=3D st_lsm6dsx.o obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) +=3D st_lsm6dsx_i2c.o diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_l= sm6dsx/st_lsm6dsx.h index 07b1773c87bd..4173f670f7af 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h @@ -294,6 +294,7 @@ enum st_lsm6dsx_sensor_id { ST_LSM6DSX_ID_EXT0, ST_LSM6DSX_ID_EXT1, ST_LSM6DSX_ID_EXT2, + ST_LSM6DSX_ID_SF, ST_LSM6DSX_ID_MAX }; =20 @@ -301,6 +302,15 @@ enum st_lsm6dsx_ext_sensor_id { ST_LSM6DSX_ID_MAGN, }; =20 +struct st_lsm6dsx_sf_settings { + const struct iio_chan_spec *chan; + int chan_len; + struct st_lsm6dsx_odr_table_entry odr_table; + struct st_lsm6dsx_reg fifo_enable; + struct st_lsm6dsx_reg page_mux; + struct st_lsm6dsx_reg enable; +}; + /** * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings * @i2c_addr: I2c slave address list. @@ -388,6 +398,7 @@ struct st_lsm6dsx_settings { struct st_lsm6dsx_hw_ts_settings ts_settings; struct st_lsm6dsx_shub_settings shub_settings; struct st_lsm6dsx_event_settings event_settings; + struct st_lsm6dsx_sf_settings sf_settings; }; =20 enum st_lsm6dsx_fifo_mode { @@ -510,6 +521,9 @@ int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sens= or, u32 odr, u8 *val); int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name); int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enab= le); int st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw *hw, u8 *data, int le= n); +int st_lsm6dsx_sf_probe(struct st_lsm6dsx_hw *hw, const char *name); +int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable= ); +int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable); int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable); =20 static inline int @@ -564,12 +578,14 @@ st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio= _dev, static inline int st_lsm6dsx_device_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable) { - if (sensor->id =3D=3D ST_LSM6DSX_ID_EXT0 || - sensor->id =3D=3D ST_LSM6DSX_ID_EXT1 || - sensor->id =3D=3D ST_LSM6DSX_ID_EXT2) + switch (sensor->id) { + case ST_LSM6DSX_ID_EXT0 ... ST_LSM6DSX_ID_EXT2: return st_lsm6dsx_shub_set_enable(sensor, enable); - - return st_lsm6dsx_sensor_set_enable(sensor, enable); + case ST_LSM6DSX_ID_SF: + return st_lsm6dsx_sf_set_enable(sensor, enable); + default: + return st_lsm6dsx_sensor_set_enable(sensor, enable); + } } =20 static const diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/i= mu/st_lsm6dsx/st_lsm6dsx_buffer.c index ded9a96076e6..3b4fa57bf461 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c @@ -88,6 +88,7 @@ enum st_lsm6dsx_fifo_tag { ST_LSM6DSX_EXT0_TAG =3D 0x0f, ST_LSM6DSX_EXT1_TAG =3D 0x10, ST_LSM6DSX_EXT2_TAG =3D 0x11, + ST_LSM6DSX_ROT_TAG =3D 0x13, }; =20 static const @@ -226,8 +227,11 @@ static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_s= ensor *sensor, u8 data; =20 /* Only internal sensors have a FIFO ODR configuration register. */ - if (sensor->id >=3D ARRAY_SIZE(hw->settings->batch)) + if (sensor->id >=3D ARRAY_SIZE(hw->settings->batch)) { + if (sensor->id =3D=3D ST_LSM6DSX_ID_SF) + return st_lsm6dsx_sf_set_odr(sensor, enable); return 0; + } =20 batch_reg =3D &hw->settings->batch[sensor->id]; if (batch_reg->addr) { @@ -580,6 +584,16 @@ st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, = u8 tag, case ST_LSM6DSX_EXT2_TAG: iio_dev =3D hw->iio_devs[ST_LSM6DSX_ID_EXT2]; break; + case ST_LSM6DSX_ROT_TAG: + /* + * The sensor reports only the {X, Y, Z} elements of the + * quaternion vector; set the W value to 0 (it can be derived + * from the {X, Y, Z} values due to the property that the vector + * is normalized). + */ + data[ST_LSM6DSX_SAMPLE_SIZE / ST_LSM6DSX_CHAN_SIZE] =3D 0; + iio_dev =3D hw->iio_devs[ST_LSM6DSX_ID_SF]; + break; default: return -EINVAL; } diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu= /st_lsm6dsx/st_lsm6dsx_core.c index dc0ae0e488ce..c21163a06a71 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c @@ -94,6 +94,24 @@ =20 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f =20 +/* Raw values from the IMU are 16-bit half-precision floating-point number= s. */ +#define ST_LSM6DSX_CHANNEL_ROT \ +{ \ + .type =3D IIO_ROT, \ + .modified =3D 1, \ + .channel2 =3D IIO_MOD_QUATERNION, \ + .info_mask_shared_by_all =3D BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .scan_index =3D 0, \ + .scan_type =3D { \ + .sign =3D 'u', \ + .realbits =3D 16, \ + .storagebits =3D 16, \ + .endianness =3D IIO_LE, \ + .repeat =3D 4, \ + }, \ + .ext_info =3D st_lsm6dsx_ext_info, \ +} + static const struct iio_event_spec st_lsm6dsx_ev_motion[] =3D { { .type =3D IIO_EV_TYPE_THRESH, @@ -153,6 +171,11 @@ static const struct iio_chan_spec st_lsm6ds0_gyro_chan= nels[] =3D { IIO_CHAN_SOFT_TIMESTAMP(3), }; =20 +static const struct iio_chan_spec st_lsm6dsx_sf_channels[] =3D { + ST_LSM6DSX_CHANNEL_ROT, + IIO_CHAN_SOFT_TIMESTAMP(1), +}; + static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] =3D { { .reset =3D { @@ -1492,6 +1515,35 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_s= ensor_settings[] =3D { }, }, }, + .sf_settings =3D { + .chan =3D st_lsm6dsx_sf_channels, + .chan_len =3D ARRAY_SIZE(st_lsm6dsx_sf_channels), + .odr_table =3D { + .reg =3D { + .addr =3D 0x5e, + .mask =3D GENMASK(5, 3), + }, + .odr_avl[0] =3D { 15000, 0x00 }, + .odr_avl[1] =3D { 30000, 0x01 }, + .odr_avl[2] =3D { 60000, 0x02 }, + .odr_avl[3] =3D { 120000, 0x03 }, + .odr_avl[4] =3D { 240000, 0x04 }, + .odr_avl[5] =3D { 480000, 0x05 }, + .odr_len =3D 6, + }, + .fifo_enable =3D { + .addr =3D 0x44, + .mask =3D BIT(1), + }, + .page_mux =3D { + .addr =3D 0x01, + .mask =3D BIT(7), + }, + .enable =3D { + .addr =3D 0x04, + .mask =3D BIT(1), + }, + }, }, { .reset =3D { @@ -2899,6 +2951,12 @@ int st_lsm6dsx_probe(struct device *dev, int irq, in= t hw_id, return err; } =20 + if (hw->settings->sf_settings.chan) { + err =3D st_lsm6dsx_sf_probe(hw, name); + if (err) + return err; + } + if (hw->irq > 0) { err =3D st_lsm6dsx_irq_setup(hw); if (err < 0) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c b/drivers/iio/i= mu/st_lsm6dsx/st_lsm6dsx_fusion.c new file mode 100644 index 000000000000..7033aaeba13e --- /dev/null +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c @@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * STMicroelectronics st_lsm6dsx IMU sensor fusion + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "st_lsm6dsx.h" + +static int +st_lsm6dsx_sf_get_odr_val(const struct st_lsm6dsx_sf_settings *settings, + u32 odr, u8 *val) +{ + int i; + + for (i =3D 0; i < settings->odr_table.odr_len; i++) { + if (settings->odr_table.odr_avl[i].milli_hz =3D=3D odr) + break; + } + if (i =3D=3D settings->odr_table.odr_len) + return -EINVAL; + + *val =3D settings->odr_table.odr_avl[i].val; + return 0; +} + +/** + * st_lsm6dsx_sf_page_enable - Enable access to sensor fusion configuration + * registers. + * @hw: Sensor hardware instance. + * + * Return: 0 on success, negative value on error. + */ +static int st_lsm6dsx_sf_page_enable(struct st_lsm6dsx_hw *hw) +{ + const struct st_lsm6dsx_reg *mux =3D &hw->settings->sf_settings.page_mux; + + return regmap_set_bits(hw->regmap, mux->addr, mux->mask); +} + +/** + * st_lsm6dsx_sf_page_disable - Disable access to sensor fusion configurat= ion + * registers. + * @hw: Sensor hardware instance. + * + * Return: 0 on success, negative value on error. + */ +static int st_lsm6dsx_sf_page_disable(struct st_lsm6dsx_hw *hw) +{ + const struct st_lsm6dsx_reg *mux =3D &hw->settings->sf_settings.page_mux; + + return regmap_clear_bits(hw->regmap, mux->addr, mux->mask); +} + +int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable) +{ + struct st_lsm6dsx_hw *hw =3D sensor->hw; + const struct st_lsm6dsx_reg *en_reg; + int err; + + guard(mutex)(&hw->page_lock); + + en_reg =3D &hw->settings->sf_settings.enable; + err =3D st_lsm6dsx_sf_page_enable(hw); + if (err) + return err; + + err =3D regmap_assign_bits(hw->regmap, en_reg->addr, en_reg->mask, enable= ); + if (err) { + st_lsm6dsx_sf_page_disable(hw); + return err; + } + + return st_lsm6dsx_sf_page_disable(hw); +} + +int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable) +{ + const struct st_lsm6dsx_sf_settings *settings; + struct st_lsm6dsx_hw *hw =3D sensor->hw; + u8 data; + int err; + + guard(mutex)(&hw->page_lock); + + err =3D st_lsm6dsx_sf_page_enable(hw); + if (err) + return err; + + settings =3D &hw->settings->sf_settings; + if (enable) { + const struct st_lsm6dsx_reg *reg =3D &settings->odr_table.reg; + u8 odr_val; + + st_lsm6dsx_sf_get_odr_val(settings, sensor->hwfifo_odr_mHz, + &odr_val); + data =3D ST_LSM6DSX_SHIFT_VAL(odr_val, reg->mask); + err =3D regmap_update_bits(hw->regmap, reg->addr, reg->mask, + data); + if (err) + goto out; + } + + err =3D regmap_assign_bits(hw->regmap, settings->fifo_enable.addr, + settings->fifo_enable.mask, enable); + if (err) + goto out; + + return st_lsm6dsx_sf_page_disable(hw); + +out: + st_lsm6dsx_sf_page_disable(hw); + + return err; +} + +static int st_lsm6dsx_sf_read_raw(struct iio_dev *iio_dev, + struct iio_chan_spec const *ch, + int *val, int *val2, long mask) +{ + struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); + + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + *val =3D sensor->hwfifo_odr_mHz / MILLI; + *val2 =3D (sensor->hwfifo_odr_mHz % MILLI) * (MICRO / MILLI); + return IIO_VAL_INT_PLUS_MICRO; + default: + return -EINVAL; + } +} + +static int st_lsm6dsx_sf_write_raw(struct iio_dev *iio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); + const struct st_lsm6dsx_sf_settings *settings; + int err; + + settings =3D &sensor->hw->settings->sf_settings; + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: { + u32 odr_mHz =3D val * MILLI + val2 * (MILLI / MICRO); + u8 odr_val; + + /* check that the requested frequency is supported */ + err =3D st_lsm6dsx_sf_get_odr_val(settings, odr_mHz, &odr_val); + if (err) + return err; + + sensor->hwfifo_odr_mHz =3D odr_mHz; + return 0; + } + default: + return -EINVAL; + } +} + +static ssize_t st_lsm6dsx_sf_sampling_freq_avail(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct st_lsm6dsx_sensor *sensor =3D iio_priv(dev_to_iio_dev(dev)); + const struct st_lsm6dsx_sf_settings *settings; + int len =3D 0; + + settings =3D &sensor->hw->settings->sf_settings; + for (unsigned int i =3D 0; i < settings->odr_table.odr_len; i++) { + u32 val =3D settings->odr_table.odr_avl[i].milli_hz; + + len +=3D sysfs_emit_at(buf, len, "%lu.%03lu ", val / MILLI, + val % MILLI); + } + buf[len - 1] =3D '\n'; + + return len; +} + +static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sf_sampling_freq_avail); +static struct attribute *st_lsm6dsx_sf_attributes[] =3D { + &iio_dev_attr_sampling_frequency_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group st_lsm6dsx_sf_attribute_group =3D { + .attrs =3D st_lsm6dsx_sf_attributes, +}; + +static const struct iio_info st_lsm6dsx_sf_info =3D { + .attrs =3D &st_lsm6dsx_sf_attribute_group, + .read_raw =3D st_lsm6dsx_sf_read_raw, + .write_raw =3D st_lsm6dsx_sf_write_raw, + .hwfifo_set_watermark =3D st_lsm6dsx_set_watermark, +}; + +int st_lsm6dsx_sf_probe(struct st_lsm6dsx_hw *hw, const char *name) +{ + const struct st_lsm6dsx_sf_settings *settings; + struct st_lsm6dsx_sensor *sensor; + struct iio_dev *iio_dev; + + iio_dev =3D devm_iio_device_alloc(hw->dev, sizeof(*sensor)); + if (!iio_dev) + return -ENOMEM; + + settings =3D &hw->settings->sf_settings; + sensor =3D iio_priv(iio_dev); + sensor->id =3D ST_LSM6DSX_ID_SF; + sensor->hw =3D hw; + sensor->hwfifo_odr_mHz =3D settings->odr_table.odr_avl[0].milli_hz; + sensor->watermark =3D 1; + iio_dev->modes =3D INDIO_DIRECT_MODE; + iio_dev->info =3D &st_lsm6dsx_sf_info; + iio_dev->channels =3D settings->chan; + iio_dev->num_channels =3D settings->chan_len; + if (snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name) >=3D + sizeof(sensor->name)) + return -E2BIG; + iio_dev->name =3D sensor->name; + + /* + * Put the IIO device pointer in the iio_devs array so that the caller + * can set up a buffer and register this IIO device. + */ + hw->iio_devs[ST_LSM6DSX_ID_SF] =3D iio_dev; + + return 0; +} --=20 2.39.5