From nobody Wed Nov 27 06:38:20 2024 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33C87194A70; Fri, 11 Oct 2024 15:38:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661102; cv=none; b=chZRbyrRE+D7aHB2kG8Wzs8x4sWURBUeLJUPgZnaeMAvma2NE9bC9akswdbcCX2a4b19JO8f3RNhIPeUtgqucUHE99gQU6CSouZidHeYgdf+nwUjKCVMYYlv85U6L2b/f4KrGUsWvnJBU7IRvyasBS6dC803we0zqdQd8O2JTgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661102; c=relaxed/simple; bh=Z6uBrdvSJ43EqogOegiG6Xcd5YZbpV5K4LXOn5qNKB8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kVd5c3UzVo19qsKT4G8RrxDaDJGi8sFGlbuJudgi+3xAfMdUuNgBKppxXfTVNi6x+c6Nzh5e46LrhykP2PtgYHIY0mKmLR4aLZd+x/RhAvWl1nuFCgVI95XTA2WqD4K/5mirQZUt4ltRk8+Lvgf9ft3TmnY3s6X1SaNV799lhfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com; spf=pass smtp.mailfrom=justinweiss.com; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b=Xq+f91GK; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=jRCj2Pqm; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b="Xq+f91GK"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="jRCj2Pqm" Received: from phl-compute-07.internal (phl-compute-07.phl.internal [10.202.2.47]) by mailfout.phl.internal (Postfix) with ESMTP id 4BBC91380213; Fri, 11 Oct 2024 11:38:19 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-07.internal (MEProxy); Fri, 11 Oct 2024 11:38:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=justinweiss.com; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1728661099; x= 1728747499; bh=Ac+EBU0kzwQVqMurDpXKcoQ0wJQlhmrAdN2yUg+g31o=; b=X q+f91GKCkd24U2A3vDZKzXUUGQfzP+m3tHafBbilAx8e6nMlePW4zXxBqpOYkB97 bFDdVEIkMcbCYbp3tFn/NiLJQLDpj575ZLqhoMzWTq1iNhBseJMUNOr9CPE/7kzJ 0CfMl0kq65J0NtjHbaY/DjmRX4FWQwj/4RT6NYgjkhwAtxARG/n7mnaw2xUIKGFf pcnz1IGMNvi7WFEF74CrDg0k7Trs9/yX2Sj4lAgl1MYE/g2XsCzhxeAwfWi3FJOk PuYs2TrwbIbv5v2IAQaeWelKZKFKP/6oZzSqLyenifU+4ptJElWYqGsrVWbXP9U/ 670XL5kZq7ODt7eiGP2Gg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1728661099; x= 1728747499; bh=Ac+EBU0kzwQVqMurDpXKcoQ0wJQlhmrAdN2yUg+g31o=; b=j RCj2Pqmr4TaTIlIOVu9ZYFXDdAZ3KQBxSMNjnSgJGRcA2e5KgExy++b/MBqZyTDy vhXJeD12MF6J/TMeHfw+bF3IFNAaFcKWFiwuUEKfXjqNfm9ZAXd1PlkxeRoSNCwE xNvuXbf5HmA2skAebFb7fxtMw4W+HnIMPVWSgiZzHAcadzHr+1/orQrvWbxKvGGV oyCZzyopdb3u9mS1FMomStBRFvysQQW/X+dvojoiUhSdKufw6A/e+6Phs9Hy2/b8 k1za8XUZGVcc7XqeiIFeQaueBY6PJWhtesX8xD0/QDkfx+HZr3iYlskwoR4hINjY LpLpED3q18Ou7dAo2+GnQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdefkedgledtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomheplfhushhtihhnucghvghishhsuceojhhushhtihhnsehjuhhsthhinhifvg hishhsrdgtohhmqeenucggtffrrghtthgvrhhnpeeiffdtvdfgtddvieetffduhfejtdef teelkefgteekgeegffduiefhudeiveejkeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtgho mhdpnhgspghrtghpthhtohepkedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplh grnhiirghnohdrrghlvgigsehgmhgrihhlrdgtohhmpdhrtghpthhtohepjhhitgdvfees khgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhgrrhhssehmvghtrghfohhordguvgdprh gtphhtthhopehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtghomhdprhgtphhtthho pehlihhnuhigqdhiihhosehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplh hinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep uggvrhgvkhhjohhhnhdrtghlrghrkhesghhmrghilhdrtghomhdprhgtphhtthhopehphh hilhhmsehmrghnjhgrrhhordhorhhg X-ME-Proxy: Feedback-ID: icf614246:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Oct 2024 11:38:18 -0400 (EDT) From: Justin Weiss To: Alex Lanzano , Jonathan Cameron , Lars-Peter Clausen Cc: Justin Weiss , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, "Derek J . Clark" , =?UTF-8?q?Philip=20M=C3=BCller?= Subject: [PATCH 1/3] iio: imu: Add i2c driver for bmi260 imu Date: Fri, 11 Oct 2024 08:37:47 -0700 Message-ID: <20241011153751.65152-2-justin@justinweiss.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011153751.65152-1-justin@justinweiss.com> References: <20241011153751.65152-1-justin@justinweiss.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds i2c support for the Bosch BMI260 6-axis IMU to the Bosch BMI270 driver. Setup and operation is nearly identical to the Bosch BMI270, but has a different chip ID and requires different firmware. Firmware is requested and loaded from userspace. Signed-off-by: Justin Weiss --- drivers/iio/imu/bmi270/bmi270.h | 16 ++++++++++++++- drivers/iio/imu/bmi270/bmi270_core.c | 29 +++++++++++++++++++++++----- drivers/iio/imu/bmi270/bmi270_i2c.c | 22 ++++++++++++++++++--- drivers/iio/imu/bmi270/bmi270_spi.c | 11 ++++++++--- 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/drivers/iio/imu/bmi270/bmi270.h b/drivers/iio/imu/bmi270/bmi27= 0.h index 8ac20ad7ee94..51e374fd4290 100644 --- a/drivers/iio/imu/bmi270/bmi270.h +++ b/drivers/iio/imu/bmi270/bmi270.h @@ -10,10 +10,24 @@ struct device; struct bmi270_data { struct device *dev; struct regmap *regmap; + const struct bmi270_chip_info *chip_info; +}; + +enum bmi270_device_type { + BMI260, + BMI270, +}; + +struct bmi270_chip_info { + const char *name; + int chip_id; + const char *fw_name; }; =20 extern const struct regmap_config bmi270_regmap_config; +extern const struct bmi270_chip_info bmi270_chip_info[]; =20 -int bmi270_core_probe(struct device *dev, struct regmap *regmap); +int bmi270_core_probe(struct device *dev, struct regmap *regmap, + const struct bmi270_chip_info *chip_info); =20 #endif /* BMI270_H_ */ diff --git a/drivers/iio/imu/bmi270/bmi270_core.c b/drivers/iio/imu/bmi270/= bmi270_core.c index aeda7c4228df..e5ee80c12166 100644 --- a/drivers/iio/imu/bmi270/bmi270_core.c +++ b/drivers/iio/imu/bmi270/bmi270_core.c @@ -11,6 +11,7 @@ #include "bmi270.h" =20 #define BMI270_CHIP_ID_REG 0x00 +#define BMI260_CHIP_ID_VAL 0x27 #define BMI270_CHIP_ID_VAL 0x24 #define BMI270_CHIP_ID_MSK GENMASK(7, 0) =20 @@ -55,6 +56,7 @@ #define BMI270_PWR_CTRL_ACCEL_EN_MSK BIT(2) #define BMI270_PWR_CTRL_TEMP_EN_MSK BIT(3) =20 +#define BMI260_INIT_DATA_FILE "bmi260-init-data.fw" #define BMI270_INIT_DATA_FILE "bmi270-init-data.fw" =20 enum bmi270_scan { @@ -66,6 +68,20 @@ enum bmi270_scan { BMI270_SCAN_GYRO_Z, }; =20 +const struct bmi270_chip_info bmi270_chip_info[] =3D { + [BMI260] =3D { + .name =3D "bmi260", + .chip_id =3D BMI260_CHIP_ID_VAL, + .fw_name =3D BMI260_INIT_DATA_FILE, + }, + [BMI270] =3D { + .name =3D "bmi270", + .chip_id =3D BMI270_CHIP_ID_VAL, + .fw_name =3D BMI270_INIT_DATA_FILE, + } +}; +EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, IIO_BMI270); + static int bmi270_get_data(struct bmi270_data *bmi270_device, int chan_type, int axis, int *val) { @@ -154,8 +170,8 @@ static int bmi270_validate_chip_id(struct bmi270_data *= bmi270_device) if (ret) return dev_err_probe(dev, ret, "Failed to read chip id"); =20 - if (chip_id !=3D BMI270_CHIP_ID_VAL) - dev_info(dev, "Unknown chip id 0x%x", chip_id); + if (chip_id !=3D bmi270_device->chip_info->chip_id) + return dev_err_probe(dev, -ENODEV, "Unknown chip id 0x%x", chip_id); =20 return 0; } @@ -187,7 +203,8 @@ static int bmi270_write_calibration_data(struct bmi270_= data *bmi270_device) return dev_err_probe(dev, ret, "Failed to prepare device to load init data"); =20 - ret =3D request_firmware(&init_data, BMI270_INIT_DATA_FILE, dev); + ret =3D request_firmware(&init_data, + bmi270_device->chip_info->fw_name, dev); if (ret) return dev_err_probe(dev, ret, "Failed to load init data file"); =20 @@ -274,7 +291,8 @@ static int bmi270_chip_init(struct bmi270_data *bmi270_= device) return bmi270_configure_imu(bmi270_device); } =20 -int bmi270_core_probe(struct device *dev, struct regmap *regmap) +int bmi270_core_probe(struct device *dev, struct regmap *regmap, + const struct bmi270_chip_info *chip_info) { int ret; struct bmi270_data *bmi270_device; @@ -287,6 +305,7 @@ int bmi270_core_probe(struct device *dev, struct regmap= *regmap) bmi270_device =3D iio_priv(indio_dev); bmi270_device->dev =3D dev; bmi270_device->regmap =3D regmap; + bmi270_device->chip_info =3D chip_info; =20 ret =3D bmi270_chip_init(bmi270_device); if (ret) @@ -294,7 +313,7 @@ int bmi270_core_probe(struct device *dev, struct regmap= *regmap) =20 indio_dev->channels =3D bmi270_channels; indio_dev->num_channels =3D ARRAY_SIZE(bmi270_channels); - indio_dev->name =3D "bmi270"; + indio_dev->name =3D chip_info->name; indio_dev->modes =3D INDIO_DIRECT_MODE; indio_dev->info =3D &bmi270_info; =20 diff --git a/drivers/iio/imu/bmi270/bmi270_i2c.c b/drivers/iio/imu/bmi270/b= mi270_i2c.c index e9025d22d5cc..c8c90666c76b 100644 --- a/drivers/iio/imu/bmi270/bmi270_i2c.c +++ b/drivers/iio/imu/bmi270/bmi270_i2c.c @@ -18,28 +18,44 @@ static int bmi270_i2c_probe(struct i2c_client *client) { struct regmap *regmap; struct device *dev =3D &client->dev; + const struct bmi270_chip_info *chip_info; + + chip_info =3D i2c_get_match_data(client); + if (!chip_info) + return -ENODEV; =20 regmap =3D devm_regmap_init_i2c(client, &bmi270_i2c_regmap_config); if (IS_ERR(regmap)) return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init i2c regmap"); =20 - return bmi270_core_probe(dev, regmap); + return bmi270_core_probe(dev, regmap, chip_info); } =20 static const struct i2c_device_id bmi270_i2c_id[] =3D { - { "bmi270", 0 }, + { "bmi260", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { "bmi270", (kernel_ulong_t)&bmi270_chip_info[BMI270] }, { } }; =20 +static const struct acpi_device_id bmi270_acpi_match[] =3D { + { "BOSC0260", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { "BMI0260", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { "BOSC0160", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { "BMI0160", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { "10EC5280", (kernel_ulong_t)&bmi270_chip_info[BMI260] }, + { }, +}; + static const struct of_device_id bmi270_of_match[] =3D { - { .compatible =3D "bosch,bmi270" }, + { .compatible =3D "bosch,bmi270", .data =3D &bmi270_chip_info[BMI270] }, { } }; =20 static struct i2c_driver bmi270_i2c_driver =3D { .driver =3D { .name =3D "bmi270_i2c", + .acpi_match_table =3D bmi270_acpi_match, .of_match_table =3D bmi270_of_match, }, .probe =3D bmi270_i2c_probe, diff --git a/drivers/iio/imu/bmi270/bmi270_spi.c b/drivers/iio/imu/bmi270/b= mi270_spi.c index 34d5ba6273bb..3d240f9651bc 100644 --- a/drivers/iio/imu/bmi270/bmi270_spi.c +++ b/drivers/iio/imu/bmi270/bmi270_spi.c @@ -50,6 +50,11 @@ static int bmi270_spi_probe(struct spi_device *spi) { struct regmap *regmap; struct device *dev =3D &spi->dev; + const struct bmi270_chip_info *chip_info; + + chip_info =3D spi_get_device_match_data(spi); + if (!chip_info) + return -ENODEV; =20 regmap =3D devm_regmap_init(dev, &bmi270_regmap_bus, dev, &bmi270_spi_regmap_config); @@ -57,16 +62,16 @@ static int bmi270_spi_probe(struct spi_device *spi) return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init i2c regmap"); =20 - return bmi270_core_probe(dev, regmap); + return bmi270_core_probe(dev, regmap, chip_info); } =20 static const struct spi_device_id bmi270_spi_id[] =3D { - { "bmi270" }, + { "bmi270", (kernel_ulong_t)&bmi270_chip_info[BMI270] }, { } }; =20 static const struct of_device_id bmi270_of_match[] =3D { - { .compatible =3D "bosch,bmi270" }, + { .compatible =3D "bosch,bmi270", .data =3D &bmi270_chip_info[BMI270] }, { } }; =20 --=20 2.47.0 From nobody Wed Nov 27 06:38:20 2024 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5B0D195FCE; Fri, 11 Oct 2024 15:38:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661103; cv=none; b=X7sertNs9dyXuIEWlvC4N7drLUa3Im+zSQEErZIQmOZHerOfBnG3AJgoIZhCcMPMqfbcCTQIJn8voB03IIk/XP4CDgQT4lejsN5/55+QTS5zue9BbXz786bwnNhXV65ywQ+a5JfN4GnuHQL3svlTa030AOY40gWht6WSuI0w2PQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661103; c=relaxed/simple; bh=Xsa7E4sI2a3AFjkvFeja04vjdO52xi11YqzXkhiDSA8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J7dqBUHJtxHXAaL993Dqm+2AkxDYtWnpDvLY2I2tdUwDgAEZH0s4g3gqklZsslCjux5Qr85Qgk6aqQ6pihacko29WcTk/o/5wroGcaqclWZiNZC/HVYS6PguGwoQB7O664Qs5eHn7CMoQmPCL0WO6/bzg40lvhIBAt73Z4Umpaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com; spf=pass smtp.mailfrom=justinweiss.com; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b=YVF7q28/; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=I1vvCQJD; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b="YVF7q28/"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="I1vvCQJD" Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id BF39A1380225; Fri, 11 Oct 2024 11:38:20 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Fri, 11 Oct 2024 11:38:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=justinweiss.com; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1728661100; x= 1728747500; bh=nl/3aTrRjSgjCiMNNGf2bPVcKSWbVjCvRjUJQchF8eE=; b=Y VF7q28/N7+5e7a925HBkYf3qzeIMNNZelX2WGraJotiJG9qz3aSl7j6OdYabBeCZ NwbudDZV0sdK9e3ey1+rSHg8EE7ypUt8kytsyWNyZhr9hYO8BuwaooVcpWvCsCBq PCTT6EA5l4mkn0LTvpPewqzEfYiIT4GSl8jnmPyMiU1Fsb3sZUlW4P08Wf6TV4+f nn8ExJOfwwJlhc37415JSZ4RD879eBByS5st3ekGQAz4cUhry5jpctQiJtbEivKj NteaTzfqvsM0USJb2XJ2wBbqXC7lDq6Irzc7N0JsSDL6Qbi/S8MvwVyUFMX1UUvg 9CmwdTOhbKlJmNL39c7aQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1728661100; x= 1728747500; bh=nl/3aTrRjSgjCiMNNGf2bPVcKSWbVjCvRjUJQchF8eE=; b=I 1vvCQJDbb4QwkuO00Pv3I/58/9BE/qCioye+YCgU0XjN2S5L7WwfDX7GeGVFXulU cMQ9XNUf9QLEQWar0jGh40cxNW7syvGY9SaHjXKwKrVBBj2NRwZVvlhhuwvdt08O E16wCknBgv/tHSn+MHyznW/PrNrsBaVDPwhZNww8eXPVKw3Mti2ctAw+BuGX9tDF svFLq4ijcqjJorW+m4eWSmslftPfAo8No0auTZBvSFm7mUQ+u6sMfXNL6zowjoe2 w1IseZ+as/NMGU+aYJguR1S+ox4yQcv7FNEjcuApXbCtYxTJEND5jY3buOrbgAHu suvrmC9v4wBITd/i2rcsA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdefkedgledtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomheplfhushhtihhnucghvghishhsuceojhhushhtihhnsehjuhhsthhinhifvg hishhsrdgtohhmqeenucggtffrrghtthgvrhhnpeeiffdtvdfgtddvieetffduhfejtdef teelkefgteekgeegffduiefhudeiveejkeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtgho mhdpnhgspghrtghpthhtohepkedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplh grnhiirghnohdrrghlvgigsehgmhgrihhlrdgtohhmpdhrtghpthhtohepjhhitgdvfees khgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhgrrhhssehmvghtrghfohhordguvgdprh gtphhtthhopehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtghomhdprhgtphhtthho pehlihhnuhigqdhiihhosehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplh hinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep uggvrhgvkhhjohhhnhdrtghlrghrkhesghhmrghilhdrtghomhdprhgtphhtthhopehphh hilhhmsehmrghnjhgrrhhordhorhhg X-ME-Proxy: Feedback-ID: icf614246:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Oct 2024 11:38:19 -0400 (EDT) From: Justin Weiss To: Alex Lanzano , Jonathan Cameron , Lars-Peter Clausen Cc: Justin Weiss , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, "Derek J . Clark" , =?UTF-8?q?Philip=20M=C3=BCller?= Subject: [PATCH 2/3] iio: imu: Add triggered buffer for Bosch BMI270 IMU Date: Fri, 11 Oct 2024 08:37:48 -0700 Message-ID: <20241011153751.65152-3-justin@justinweiss.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011153751.65152-1-justin@justinweiss.com> References: <20241011153751.65152-1-justin@justinweiss.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Set up a triggered buffer for the accel and angl_vel values. Signed-off-by: Justin Weiss --- drivers/iio/imu/bmi270/Kconfig | 1 + drivers/iio/imu/bmi270/bmi270.h | 8 +++++ drivers/iio/imu/bmi270/bmi270_core.c | 47 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/drivers/iio/imu/bmi270/Kconfig b/drivers/iio/imu/bmi270/Kconfig index 0ffd29794fda..6362acc706da 100644 --- a/drivers/iio/imu/bmi270/Kconfig +++ b/drivers/iio/imu/bmi270/Kconfig @@ -6,6 +6,7 @@ config BMI270 tristate select IIO_BUFFER + select IIO_TRIGGERED_BUFFER =20 config BMI270_I2C tristate "Bosch BMI270 I2C driver" diff --git a/drivers/iio/imu/bmi270/bmi270.h b/drivers/iio/imu/bmi270/bmi27= 0.h index 51e374fd4290..335400c34b0d 100644 --- a/drivers/iio/imu/bmi270/bmi270.h +++ b/drivers/iio/imu/bmi270/bmi270.h @@ -11,6 +11,14 @@ struct bmi270_data { struct device *dev; struct regmap *regmap; const struct bmi270_chip_info *chip_info; + + /* + * Ensure natural alignment for timestamp if present. + * Max length needed: 2 * 3 channels + 4 bytes padding + 8 byte ts. + * If fewer channels are enabled, less space may be needed, as + * long as the timestamp is still aligned to 8 bytes. + */ + __le16 buf[12] __aligned(8); }; =20 enum bmi270_device_type { diff --git a/drivers/iio/imu/bmi270/bmi270_core.c b/drivers/iio/imu/bmi270/= bmi270_core.c index e5ee80c12166..f49db5d1bffd 100644 --- a/drivers/iio/imu/bmi270/bmi270_core.c +++ b/drivers/iio/imu/bmi270/bmi270_core.c @@ -7,6 +7,8 @@ #include =20 #include +#include +#include =20 #include "bmi270.h" =20 @@ -66,6 +68,7 @@ enum bmi270_scan { BMI270_SCAN_GYRO_X, BMI270_SCAN_GYRO_Y, BMI270_SCAN_GYRO_Z, + BMI270_SCAN_TIMESTAMP, }; =20 const struct bmi270_chip_info bmi270_chip_info[] =3D { @@ -82,6 +85,29 @@ const struct bmi270_chip_info bmi270_chip_info[] =3D { }; EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, IIO_BMI270); =20 +static irqreturn_t bmi270_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf =3D p; + struct iio_dev *indio_dev =3D pf->indio_dev; + struct bmi270_data *bmi270_device =3D iio_priv(indio_dev); + int i, ret, j =3D 0, base =3D BMI270_ACCEL_X_REG; + __le16 sample; + + for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) { + ret =3D regmap_bulk_read(bmi270_device->regmap, + base + i * sizeof(sample), + &sample, sizeof(sample)); + if (ret) + goto done; + bmi270_device->buf[j++] =3D sample; + } + + iio_push_to_buffers_with_timestamp(indio_dev, bmi270_device->buf, pf->tim= estamp); +done: + iio_trigger_notify_done(indio_dev->trig); + return IRQ_HANDLED; +} + static int bmi270_get_data(struct bmi270_data *bmi270_device, int chan_type, int axis, int *val) { @@ -139,6 +165,13 @@ static const struct iio_info bmi270_info =3D { .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \ BIT(IIO_CHAN_INFO_FREQUENCY), \ + .scan_index =3D BMI270_SCAN_ACCEL_##_axis, \ + .scan_type =3D { \ + .sign =3D 's', \ + .realbits =3D 16, \ + .storagebits =3D 16, \ + .endianness =3D IIO_LE \ + }, \ } =20 #define BMI270_ANG_VEL_CHANNEL(_axis) { \ @@ -148,6 +181,13 @@ static const struct iio_info bmi270_info =3D { .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \ BIT(IIO_CHAN_INFO_FREQUENCY), \ + .scan_index =3D BMI270_SCAN_GYRO_##_axis, \ + .scan_type =3D { \ + .sign =3D 's', \ + .realbits =3D 16, \ + .storagebits =3D 16, \ + .endianness =3D IIO_LE \ + }, \ } =20 static const struct iio_chan_spec bmi270_channels[] =3D { @@ -157,6 +197,7 @@ static const struct iio_chan_spec bmi270_channels[] =3D= { BMI270_ANG_VEL_CHANNEL(X), BMI270_ANG_VEL_CHANNEL(Y), BMI270_ANG_VEL_CHANNEL(Z), + IIO_CHAN_SOFT_TIMESTAMP(BMI270_SCAN_TIMESTAMP), }; =20 static int bmi270_validate_chip_id(struct bmi270_data *bmi270_device) @@ -317,6 +358,12 @@ int bmi270_core_probe(struct device *dev, struct regma= p *regmap, indio_dev->modes =3D INDIO_DIRECT_MODE; indio_dev->info =3D &bmi270_info; =20 + ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, + bmi270_trigger_handler, NULL); + if (ret) + return ret; + return devm_iio_device_register(dev, indio_dev); } EXPORT_SYMBOL_NS_GPL(bmi270_core_probe, IIO_BMI270); --=20 2.47.0 From nobody Wed Nov 27 06:38:20 2024 Received: from fhigh-a6-smtp.messagingengine.com (fhigh-a6-smtp.messagingengine.com [103.168.172.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38249198E8C; Fri, 11 Oct 2024 15:38:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661105; cv=none; b=QQtY0RP9jZR8g1E7vGGU9JtT7djlPqpEzM+V3uyzPb0wRsg05O+UYWmRD/dWHx/z/1yIJl1VA/0/+eQ9TIDcU/ZBmmoTL5TOaNeB9sOtddY1eugM2lts1bzK4NdUTT+fBdSYmEyqRaFA919cmexaC8CwpTwwtNffPQ76S63zGJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728661105; c=relaxed/simple; bh=/gIHlnCs+B6mRPHx5vHgZ2v5blQcCUsf23vSNA4/6hY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YIsQzDt52PtXEs5ibGVw3kyiT4NZS310Nt/3XFchPi6tQH334AuO2IJGx6VL8wyFLico4KIKeTHXacQlyIJ4A3GfFAgA66KBWwyhHpefJ6YQ/CcFPG0s/qpwEvH8dURFiAInY6YVv8qXgQWNX7EjzfOEjSI0P4BX8+Dtz21D4bo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com; spf=pass smtp.mailfrom=justinweiss.com; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b=jqELNGf0; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=fgXqxvAo; arc=none smtp.client-ip=103.168.172.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=justinweiss.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=justinweiss.com header.i=@justinweiss.com header.b="jqELNGf0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="fgXqxvAo" Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 4F3C81140227; Fri, 11 Oct 2024 11:38:22 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-04.internal (MEProxy); Fri, 11 Oct 2024 11:38:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=justinweiss.com; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1728661102; x= 1728747502; bh=H5NaoeN4GP3Lew/+d70cuR23NQP3Zyua7iTIzyO//hk=; b=j qELNGf03uBzaAiwdbbDRO38MDTS1fgoYaODLaVKUm+merUyIoy4Vt8wDjQN+9MsB GjthXSKc9Ljfq411u+iIhQNlGai68IJARmKxhbeb4Od1Fc9GHXlgONc7nBvsGBTI x9UGX1JLlNI0RbGgnYG4hcK8p7E7eTJKzknsJTeYkKLgB+a3IaERxhoHm1AIj4Z+ y1kpjz1rP8HqPtFlDLlLzQn4mMYZ3X9sLltZwhmUDvkuHuesGbpWbpJmCJ+7v5yy Ey1D2h9nkVszOuiSyt3r4/HcKFwfIYDlXGDnh7NiWDyQJGb7pppoJ9nwOEhFff9l m+SFe51AAYL8jtgiOq5fA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1728661102; x= 1728747502; bh=H5NaoeN4GP3Lew/+d70cuR23NQP3Zyua7iTIzyO//hk=; b=f gXqxvAoVeE75mW/BDvncmFi8lGz8rkBk/0YHujGzuvHtKbtOmfqv8SCJh6R3xWdv roO/2jE2Y8+77/mK2Iqs9B7TMJFZmH/phtiCpGOTXsZeJsNGdufgndM8jVqzWpqb bXmSv3w5E288NRco2JwVfquWb9dkFyC1edid3xFpYrBXZXvDMtzDoCkhmoEaURWu Q46j6pUKVCELyDpFddnFVWqPBBXPmto2sNdNeCJzOIg5jM9NeXdJPEk/M7BK5AVs aQ/EN62ZvDN1jixsR8yd+hE/VVBq/eYz1whUXp9Mz/PewmqPcKJe6dZwFzM+nUB3 fG77YWZVAyiwX2W2d6+JQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdefkedgledtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhephffvvefufffkofgjfhgggfestdekredtredttden ucfhrhhomheplfhushhtihhnucghvghishhsuceojhhushhtihhnsehjuhhsthhinhifvg hishhsrdgtohhmqeenucggtffrrghtthgvrhhnpeeiffdtvdfgtddvieetffduhfejtdef teelkefgteekgeegffduiefhudeiveejkeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtgho mhdpnhgspghrtghpthhtohepkedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplh grnhiirghnohdrrghlvgigsehgmhgrihhlrdgtohhmpdhrtghpthhtohepjhhitgdvfees khgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhgrrhhssehmvghtrghfohhordguvgdprh gtphhtthhopehjuhhsthhinhesjhhushhtihhnfigvihhsshdrtghomhdprhgtphhtthho pehlihhnuhigqdhiihhosehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplh hinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep uggvrhgvkhhjohhhnhdrtghlrghrkhesghhmrghilhdrtghomhdprhgtphhtthhopehphh hilhhmsehmrghnjhgrrhhordhorhhg X-ME-Proxy: Feedback-ID: icf614246:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Oct 2024 11:38:20 -0400 (EDT) From: Justin Weiss To: Alex Lanzano , Jonathan Cameron , Lars-Peter Clausen Cc: Justin Weiss , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, "Derek J . Clark" , =?UTF-8?q?Philip=20M=C3=BCller?= Subject: [PATCH 3/3] iio: imu: Add scale and sampling frequency to BMI270 IMU Date: Fri, 11 Oct 2024 08:37:49 -0700 Message-ID: <20241011153751.65152-4-justin@justinweiss.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241011153751.65152-1-justin@justinweiss.com> References: <20241011153751.65152-1-justin@justinweiss.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add read and write functions and create _available entries. Use IIO_CHAN_INFO_SAMP_FREQ instead of IIO_CHAN_INFO_FREQUENCY to match the BMI160 / BMI323 drivers. Signed-off-by: Justin Weiss --- drivers/iio/imu/bmi270/bmi270_core.c | 293 ++++++++++++++++++++++++++- 1 file changed, 291 insertions(+), 2 deletions(-) diff --git a/drivers/iio/imu/bmi270/bmi270_core.c b/drivers/iio/imu/bmi270/= bmi270_core.c index f49db5d1bffd..ce7873dc3211 100644 --- a/drivers/iio/imu/bmi270/bmi270_core.c +++ b/drivers/iio/imu/bmi270/bmi270_core.c @@ -7,6 +7,7 @@ #include =20 #include +#include #include #include =20 @@ -34,6 +35,9 @@ #define BMI270_ACC_CONF_BWP_NORMAL_MODE 0x02 #define BMI270_ACC_CONF_FILTER_PERF_MSK BIT(7) =20 +#define BMI270_ACC_CONF_RANGE_REG 0x41 +#define BMI270_ACC_CONF_RANGE_MSK GENMASK(1, 0) + #define BMI270_GYR_CONF_REG 0x42 #define BMI270_GYR_CONF_ODR_MSK GENMASK(3, 0) #define BMI270_GYR_CONF_ODR_200HZ 0x09 @@ -42,6 +46,9 @@ #define BMI270_GYR_CONF_NOISE_PERF_MSK BIT(6) #define BMI270_GYR_CONF_FILTER_PERF_MSK BIT(7) =20 +#define BMI270_GYR_CONF_RANGE_REG 0x43 +#define BMI270_GYR_CONF_RANGE_MSK GENMASK(2, 0) + #define BMI270_INIT_CTRL_REG 0x59 #define BMI270_INIT_CTRL_LOAD_DONE_MSK BIT(0) =20 @@ -85,6 +92,236 @@ const struct bmi270_chip_info bmi270_chip_info[] =3D { }; EXPORT_SYMBOL_NS_GPL(bmi270_chip_info, IIO_BMI270); =20 +enum bmi270_sensor_type { + BMI270_ACCEL =3D 0, + BMI270_GYRO, +}; + +struct bmi270_scale { + u8 bits; + int uscale; +}; + +struct bmi270_odr { + u8 bits; + int odr; + int uodr; +}; + +static const struct bmi270_scale bmi270_accel_scale[] =3D { + { 0x00, 598}, + { 0x01, 1197}, + { 0x02, 2394}, + { 0x03, 4788}, +}; + +static const struct bmi270_scale bmi270_gyro_scale[] =3D { + { 0x00, 1065}, + { 0x01, 532}, + { 0x02, 266}, + { 0x03, 133}, + { 0x04, 66}, +}; + +struct bmi270_scale_item { + const struct bmi270_scale *tbl; + int num; +}; + +static const struct bmi270_scale_item bmi270_scale_table[] =3D { + [BMI270_ACCEL] =3D { + .tbl =3D bmi270_accel_scale, + .num =3D ARRAY_SIZE(bmi270_accel_scale), + }, + [BMI270_GYRO] =3D { + .tbl =3D bmi270_gyro_scale, + .num =3D ARRAY_SIZE(bmi270_gyro_scale), + }, +}; + +static const struct bmi270_odr bmi270_accel_odr[] =3D { + {0x01, 0, 781250}, + {0x02, 1, 562500}, + {0x03, 3, 125000}, + {0x04, 6, 250000}, + {0x05, 12, 500000}, + {0x06, 25, 0}, + {0x07, 50, 0}, + {0x08, 100, 0}, + {0x09, 200, 0}, + {0x0A, 400, 0}, + {0x0B, 800, 0}, + {0x0C, 1600, 0}, +}; + +static const struct bmi270_odr bmi270_gyro_odr[] =3D { + {0x06, 25, 0}, + {0x07, 50, 0}, + {0x08, 100, 0}, + {0x09, 200, 0}, + {0x0A, 400, 0}, + {0x0B, 800, 0}, + {0x0C, 1600, 0}, + {0x0D, 3200, 0}, +}; + +struct bmi270_odr_item { + const struct bmi270_odr *tbl; + int num; +}; + +static const struct bmi270_odr_item bmi270_odr_table[] =3D { + [BMI270_ACCEL] =3D { + .tbl =3D bmi270_accel_odr, + .num =3D ARRAY_SIZE(bmi270_accel_odr), + }, + [BMI270_GYRO] =3D { + .tbl =3D bmi270_gyro_odr, + .num =3D ARRAY_SIZE(bmi270_gyro_odr), + }, +}; + +static int bmi270_set_scale(struct bmi270_data *data, + int chan_type, int uscale) +{ + int i; + int reg; + struct bmi270_scale_item bmi270_scale_item; + + switch (chan_type) { + case IIO_ACCEL: + reg =3D BMI270_ACC_CONF_RANGE_REG; + bmi270_scale_item =3D bmi270_scale_table[BMI270_ACCEL]; + break; + case IIO_ANGL_VEL: + reg =3D BMI270_GYR_CONF_RANGE_REG; + bmi270_scale_item =3D bmi270_scale_table[BMI270_GYRO]; + break; + default: + return -EINVAL; + } + + for (i =3D 0; i < bmi270_scale_item.num; i++) + if (bmi270_scale_item.tbl[i].uscale =3D=3D uscale) + break; + + if (i =3D=3D bmi270_scale_item.num) + return -EINVAL; + + return regmap_write(data->regmap, reg, + bmi270_scale_item.tbl[i].bits); +} + +static int bmi270_get_scale(struct bmi270_data *bmi270_device, + int chan_type, int *uscale) +{ + int i, ret, val; + int reg; + struct bmi270_scale_item bmi270_scale_item; + + switch (chan_type) { + case IIO_ACCEL: + reg =3D BMI270_ACC_CONF_RANGE_REG; + bmi270_scale_item =3D bmi270_scale_table[BMI270_ACCEL]; + break; + case IIO_ANGL_VEL: + reg =3D BMI270_GYR_CONF_RANGE_REG; + bmi270_scale_item =3D bmi270_scale_table[BMI270_GYRO]; + break; + default: + return -EINVAL; + } + + ret =3D regmap_read(bmi270_device->regmap, reg, &val); + if (ret) + return ret; + + for (i =3D 0; i < bmi270_scale_item.num; i++) + if (bmi270_scale_item.tbl[i].bits =3D=3D val) { + *uscale =3D bmi270_scale_item.tbl[i].uscale; + return 0; + } + + return -EINVAL; +} + +static int bmi270_set_odr(struct bmi270_data *data, int chan_type, + int odr, int uodr) +{ + int i; + int reg, mask; + struct bmi270_odr_item bmi270_odr_item; + + switch (chan_type) { + case IIO_ACCEL: + reg =3D BMI270_ACC_CONF_REG; + mask =3D BMI270_ACC_CONF_ODR_MSK; + bmi270_odr_item =3D bmi270_odr_table[BMI270_ACCEL]; + break; + case IIO_ANGL_VEL: + reg =3D BMI270_GYR_CONF_REG; + mask =3D BMI270_GYR_CONF_ODR_MSK; + bmi270_odr_item =3D bmi270_odr_table[BMI270_GYRO]; + break; + default: + return -EINVAL; + } + + for (i =3D 0; i < bmi270_odr_item.num; i++) + if (bmi270_odr_item.tbl[i].odr =3D=3D odr && + bmi270_odr_item.tbl[i].uodr =3D=3D uodr) + break; + + if (i >=3D bmi270_odr_item.num) + return -EINVAL; + + return regmap_update_bits(data->regmap, + reg, + mask, + bmi270_odr_item.tbl[i].bits); +} + +static int bmi270_get_odr(struct bmi270_data *data, int chan_type, + int *odr, int *uodr) +{ + int i, val, ret; + int reg, mask; + struct bmi270_odr_item bmi270_odr_item; + + switch (chan_type) { + case IIO_ACCEL: + reg =3D BMI270_ACC_CONF_REG; + mask =3D BMI270_ACC_CONF_ODR_MSK; + bmi270_odr_item =3D bmi270_odr_table[BMI270_ACCEL]; + break; + case IIO_ANGL_VEL: + reg =3D BMI270_GYR_CONF_REG; + mask =3D BMI270_GYR_CONF_ODR_MSK; + bmi270_odr_item =3D bmi270_odr_table[BMI270_GYRO]; + break; + default: + return -EINVAL; + } + + ret =3D regmap_read(data->regmap, reg, &val); + if (ret) + return ret; + + val &=3D mask; + + for (i =3D 0; i < bmi270_odr_item.num; i++) + if (val =3D=3D bmi270_odr_item.tbl[i].bits) + break; + + if (i >=3D bmi270_odr_item.num) + return -EINVAL; + + *odr =3D bmi270_odr_item.tbl[i].odr; + *uodr =3D bmi270_odr_item.tbl[i].uodr; + + return 0; +} + static irqreturn_t bmi270_trigger_handler(int irq, void *p) { struct iio_poll_func *pf =3D p; @@ -149,13 +386,65 @@ static int bmi270_read_raw(struct iio_dev *indio_dev, return ret; =20 return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + *val =3D 0; + ret =3D bmi270_get_scale(bmi270_device, chan->type, val2); + return ret ? ret : IIO_VAL_INT_PLUS_MICRO; + case IIO_CHAN_INFO_SAMP_FREQ: + ret =3D bmi270_get_odr(bmi270_device, chan->type, val, val2); + return ret ? ret : IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; } } =20 +static int bmi270_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct bmi270_data *data =3D iio_priv(indio_dev); + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + return bmi270_set_scale(data, chan->type, val2); + case IIO_CHAN_INFO_SAMP_FREQ: + return bmi270_set_odr(data, chan->type, val, val2); + default: + return -EINVAL; + } + + return 0; +} + +static +IIO_CONST_ATTR(in_accel_sampling_frequency_available, + "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600"); +static +IIO_CONST_ATTR(in_anglvel_sampling_frequency_available, + "25 50 100 200 400 800 1600 3200"); +static +IIO_CONST_ATTR(in_accel_scale_available, + "0.000598 0.001197 0.002394 0.004788"); +static +IIO_CONST_ATTR(in_anglvel_scale_available, + "0.001065 0.000532 0.000266 0.000133 0.000066"); + +static struct attribute *bmi270_attrs[] =3D { + &iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr, + &iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr, + &iio_const_attr_in_accel_scale_available.dev_attr.attr, + &iio_const_attr_in_anglvel_scale_available.dev_attr.attr, + NULL, +}; + +static const struct attribute_group bmi270_attrs_group =3D { + .attrs =3D bmi270_attrs, +}; + static const struct iio_info bmi270_info =3D { .read_raw =3D bmi270_read_raw, + .write_raw =3D bmi270_write_raw, + .attrs =3D &bmi270_attrs_group, }; =20 #define BMI270_ACCEL_CHANNEL(_axis) { \ @@ -164,7 +453,7 @@ static const struct iio_info bmi270_info =3D { .channel2 =3D IIO_MOD_##_axis, \ .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \ - BIT(IIO_CHAN_INFO_FREQUENCY), \ + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ .scan_index =3D BMI270_SCAN_ACCEL_##_axis, \ .scan_type =3D { \ .sign =3D 's', \ @@ -180,7 +469,7 @@ static const struct iio_info bmi270_info =3D { .channel2 =3D IIO_MOD_##_axis, \ .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \ - BIT(IIO_CHAN_INFO_FREQUENCY), \ + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ .scan_index =3D BMI270_SCAN_GYRO_##_axis, \ .scan_type =3D { \ .sign =3D 's', \ --=20 2.47.0