From patchwork Mon Jul 4 00:26:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angel Iglesias X-Patchwork-Id: 12904510 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 4E7B4C433EF for ; Mon, 4 Jul 2022 00:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229484AbiGDA1C (ORCPT ); Sun, 3 Jul 2022 20:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229471AbiGDA1C (ORCPT ); Sun, 3 Jul 2022 20:27:02 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE3306257; Sun, 3 Jul 2022 17:26:59 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id r14so5371117wrg.1; Sun, 03 Jul 2022 17:26:59 -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:mime-version :content-transfer-encoding; bh=BkcdMGU0kig+gsHzMagFBYfZudhco/uW4n7M1LjkF1o=; b=WlhLsARN5Ah8xD7NZ0C8MzttUfgUqmdW0p3SHYQOjaI8ynZa1onWsIKtosZy+R3XGS xom+9+qMCNcYpQnHFcLQBvdAgnQ27Akq7IfIwXpgBfFe+JREdnrX9qU/d4SoWfyPseGV +0iI7ZKkSOTq70d8qkISnMd7EmdtS6D19+781k3iF3VqX743+o7LLA6wxqDL09tMZfO7 OWUUAaV/E4l96nKRg04Z6f1qztiHOI0ONEStU8wqF5EF1Y3C4qSiLZ2nR2yZuRuQiFb+ 87KTCiFDr4GB6YXS+i/Dcbb1rEiV9l3tOTLfdg4DRmDo+3uGxAlAiCcX5yoLrWXpJ8KW jW3A== 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:mime-version :content-transfer-encoding; bh=BkcdMGU0kig+gsHzMagFBYfZudhco/uW4n7M1LjkF1o=; b=4zTNtxkIEYIogLZRhKYC0hRw1spubREGVYQHdzwN4yIvaGAvq+0Kq679jXJDWWnq6J yy8Sh8zXk1IQxinrDS5MX28U8tTsHyWL5u/QepJa5shqjtDAI+u5lQ8RfgOV5mvVizHY O0UHkqTZ+UtYHcmVawAwW8Eu4jZBAxMFiGajtDUr6oGdfMefIUDNurBcFeT4wMadWc3C rLbHh87sYNccoCD/6t2vQV9iWKRUrKm+sV3cDxg6lwhLXMOy+bpdwNkQTnDB/EB4+A5A JAplUC/+5K46u6v9tQeXgNwINs4LJgR03Y04sDurTnWLq0l54O9odnPAbNlRbklNX5AD sf0g== X-Gm-Message-State: AJIora9k9NbW96bJwpbibsfd6q6q5fAtADO+CpXhLHRXihWKiYSSDz/n vCEnMUSGp2LkOVmsqZjIonKk5+dFsVY= X-Google-Smtp-Source: AGRyM1uw09AFRC0KaQ5ziWVcLEicUBQ2W9BgH2JIknPXfmgH8Q1RbiYu5+89GqFbSothY7MvzLhPcw== X-Received: by 2002:a05:6000:1c0d:b0:21b:a62e:5760 with SMTP id ba13-20020a0560001c0d00b0021ba62e5760mr24492463wrb.112.1656894418059; Sun, 03 Jul 2022 17:26:58 -0700 (PDT) Received: from xps-work.lan (214.red-83-37-4.dynamicip.rima-tde.net. [83.37.4.214]) by smtp.gmail.com with ESMTPSA id i206-20020a1c3bd7000000b003a03ae64f57sm18649180wma.8.2022.07.03.17.26.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 17:26:57 -0700 (PDT) From: Angel Iglesias To: linux-iio@vger.kernel.org Cc: Angel Iglesias , Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Andreas Klinger , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/5] dt-bindings: iio: pressure: bmp085: Add BMP380 compatible string Date: Mon, 4 Jul 2022 02:26:39 +0200 Message-Id: <20220704002641.207169-1-ang.iglesiasg@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Add bosch,bmp380 compatible string for the new family of sensors. This family includes the BMP380, BMP384 and BMP388. The register map in this family changes substantially and introduces new features but core concepts and operations carryover from the previous iterations Signed-off-by: Angel Iglesias Acked-by: Krzysztof Kozlowski --- Documentation/devicetree/bindings/iio/pressure/bmp085.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/iio/pressure/bmp085.yaml b/Documentation/devicetree/bindings/iio/pressure/bmp085.yaml index 49257f9251e8..72cd2c2d3f17 100644 --- a/Documentation/devicetree/bindings/iio/pressure/bmp085.yaml +++ b/Documentation/devicetree/bindings/iio/pressure/bmp085.yaml @@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/iio/pressure/bmp085.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: BMP085/BMP180/BMP280/BME280 pressure iio sensors +title: BMP085/BMP180/BMP280/BME280/BMP380 pressure iio sensors maintainers: - Andreas Klinger @@ -16,6 +16,7 @@ description: | https://www.bosch-sensortec.com/bst/products/all_products/bmp180 https://www.bosch-sensortec.com/bst/products/all_products/bmp280 https://www.bosch-sensortec.com/bst/products/all_products/bme280 + https://www.bosch-sensortec.com/bst/products/all_products/bmp380 properties: compatible: @@ -24,6 +25,7 @@ properties: - bosch,bmp180 - bosch,bmp280 - bosch,bme280 + - bosch,bmp380 reg: maxItems: 1 From patchwork Mon Jul 4 00:27:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angel Iglesias X-Patchwork-Id: 12904511 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 BF1B1C433EF for ; Mon, 4 Jul 2022 00:28:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232849AbiGDA2J (ORCPT ); Sun, 3 Jul 2022 20:28:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229471AbiGDA2I (ORCPT ); Sun, 3 Jul 2022 20:28:08 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A0FB6259; Sun, 3 Jul 2022 17:28:07 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id n4so3769866wru.11; Sun, 03 Jul 2022 17:28:07 -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:mime-version :content-transfer-encoding; bh=e61HiYnOkxlIFlPtVK9g7qilEmpC+C4CRTA6gqMaRHM=; b=oTDIs/3gD7juvWWfvTV3CEV/Wcw0UAVCm9kMPKrCDMyBi+FCz4QC3hhy86/QKXbPCR qEw9kSZKHudVewqBgbZO6i5oKmSJvO8e/F28vr+L93InewTh/66neE0NYWBMBWiXQdYJ N+uTA28aXwGbfNe9esf5yPbD0I43LrmEvXOYBK9d8TqqJpJXZw2jkUpBpCippP8iPWPh tvXulVhPsiksJ8+M6CY/F0nlFNxMNSH6WTYRnRVp7sxSo/Y/vn4XF7Vy7T+jH0K83a/c x94LSMFWiuOrnuVDyw012xPmYmEWJW5QZSysUEfwESZsxzEf3ma8X+JlNpL93pVFx78q U30Q== 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:mime-version :content-transfer-encoding; bh=e61HiYnOkxlIFlPtVK9g7qilEmpC+C4CRTA6gqMaRHM=; b=sU3BBLLJ4JNbmDo9mCV0qr7TCh/zclt9bOHD3mfn2cqy3sICIl4MtQDu3iLWibRy4a NtvMPD2hAMCvSQXddVKFMUcwlY1QRXo8tOxp0qerhXS1LDtV1tvd8gzlvnw3W5x+/xqj APGWAd16Ws+XJ1+TeGW9CRaIwKvBk2Ezh6rbXnIPSlHyihkkBmM6lgkBCnVW2mLanSc7 8nh2Ei7k+kK0+rMb7MlLMEETmp2ED/eLNM0/2uy80qnHaw9In5djbLCxtUOpuEPPHRWO 6ix1Ok3qiveNQYQFme8g3DocDgAvhiRBbAsz8y9lrko8i6lZU6CyPT97cBXu5OiseRiJ JxKQ== X-Gm-Message-State: AJIora/OtiJFboT8BTEVm9+QTKgB40fDIHq6RVTfsdc3Kxxxaw0Hj9FH z9VZQ9IL0elMF5Mg39gPZ5FYk38XC14= X-Google-Smtp-Source: AGRyM1s5RvPF2J53ALVfqoRRAyPjMqF/JdNK8xQ5PsTpJxm11Oq9ZQhJ/76HcLkZnV5Wz93i4I7r3Q== X-Received: by 2002:a05:6000:1ac7:b0:21d:134e:5d74 with SMTP id i7-20020a0560001ac700b0021d134e5d74mr25337164wry.78.1656894485482; Sun, 03 Jul 2022 17:28:05 -0700 (PDT) Received: from xps-work.lan (214.red-83-37-4.dynamicip.rima-tde.net. [83.37.4.214]) by smtp.gmail.com with ESMTPSA id j22-20020a05600c1c1600b003a046549a85sm14110820wms.37.2022.07.03.17.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 17:28:05 -0700 (PDT) From: Angel Iglesias To: linux-iio@vger.kernel.org Cc: Angel Iglesias , Jonathan Cameron , Lars-Peter Clausen , Andy Shevchenko , Nikita Yushchenko , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/5] iio: pressure: Kconfig: Add references to BMP380 Date: Mon, 4 Jul 2022 02:27:45 +0200 Message-Id: <20220704002747.207401-1-ang.iglesiasg@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Adds reference to BMP380 in bmp280 driver descriptions and symbols Signed-off-by: Angel Iglesias --- drivers/iio/pressure/Kconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig index 0ff756cea63a..c73a52b5aed7 100644 --- a/drivers/iio/pressure/Kconfig +++ b/drivers/iio/pressure/Kconfig @@ -17,13 +17,13 @@ config ABP060MG will be called abp060mg. config BMP280 - tristate "Bosch Sensortec BMP180/BMP280 pressure sensor I2C driver" + tristate "Bosch Sensortec BMP180/BMP280/BMP380 pressure sensor I2C driver" depends on (I2C || SPI_MASTER) select REGMAP select BMP280_I2C if (I2C) select BMP280_SPI if (SPI_MASTER) help - Say yes here to build support for Bosch Sensortec BMP180 and BMP280 + Say yes here to build support for Bosch Sensortec BMP1/2/380 pressure and temperature sensors. Also supports the BME280 with an additional humidity sensor channel. From patchwork Mon Jul 4 00:29:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angel Iglesias X-Patchwork-Id: 12904530 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 932F8C43334 for ; Mon, 4 Jul 2022 00:30:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232646AbiGDAaG (ORCPT ); Sun, 3 Jul 2022 20:30:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231774AbiGDAaG (ORCPT ); Sun, 3 Jul 2022 20:30:06 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14018D6; Sun, 3 Jul 2022 17:30:04 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id o4so11173709wrh.3; Sun, 03 Jul 2022 17:30:04 -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:mime-version :content-transfer-encoding; bh=fQQMb/XrQ7h1l98gYcALYwompA+H4chujIZcP50btuI=; b=nFD8vp5fN25Zg2lX9700Xm8KVz/sEDBylxOHezDbuPXyHnPsdHebGe3Hvq+Maf9S/I 99rCt0fviZ97cSyxeFR8BNNSX5RcFRFaOWQXIU2IElRGmcL0nwmSdHlgXE4MPvnlYXZ6 RYiPOKjS6/0nsLQkuGTCaAQIShAiofhMasfd8V9yuBnfJduqwYVshfga88vvTtcny2s1 xehgWRQFzbfemJVYHRAhT2TZP3WRT58rXAh/FxtQr7x7Kab1c5K6xSaDxvECwwWpu/WL PF09H5oN12RQpNH5CmAVJ0HIBFaZ+aIQnHmrY4TibV2BUwzQjcmzPXh81IiF7zbnu5xG b3+Q== 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:mime-version :content-transfer-encoding; bh=fQQMb/XrQ7h1l98gYcALYwompA+H4chujIZcP50btuI=; b=OZvNaLOSFtUbvfoApvqu6uAUHUS+j1+TirAgnYlyGZG7T5Qm6Md9sIkaYhh2YErMpq DvXXCnVQsFM5CAd9rM3HAYMLdVlrrw4REyzaSQ2Eu10YShQ6Map04Zu2ac3ktr0E8mKp 1y3sjgMxzExjaV030lhqk6afNBtChBpzLje5G9G74xpzkBeyXR0cPVYGLNmKN4wTg6/Y zprTOkljGAVHiXXLkBi4bNAgpbDMYfpOblyxehMLcRPMfoRll7iLum4lO5El3y5T4ucJ 8bKXu3vQ+va7pOAZSaELC5i/IaKDjEqLhcPtqQpvdje4gvW4nnWlOLbE42IEh3uW54Am M1xQ== X-Gm-Message-State: AJIora/ceoY03yON2bivR3qh+P0CjiDaxW/zp7yUDK43IAsjbBFpRVEZ KAHYwBkST0zO5be4SYdlvJI= X-Google-Smtp-Source: AGRyM1t5Sz35y5DD1K/3HQn8WwWFmWDT6NycdpdAoFNAufxPXFDIhhAEG80IWEUkCz2GXXHyQTh3Zw== X-Received: by 2002:a5d:4387:0:b0:21d:6983:13fc with SMTP id i7-20020a5d4387000000b0021d698313fcmr2875128wrq.696.1656894602714; Sun, 03 Jul 2022 17:30:02 -0700 (PDT) Received: from xps-work.lan (214.red-83-37-4.dynamicip.rima-tde.net. [83.37.4.214]) by smtp.gmail.com with ESMTPSA id j19-20020a5d6e53000000b002102b16b9a4sm28583435wrz.110.2022.07.03.17.30.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 17:30:02 -0700 (PDT) From: Angel Iglesias To: Jonathan Cameron Cc: Angel Iglesias , Lars-Peter Clausen , Paul Cercueil , Ulf Hansson , "Rafael J. Wysocki" , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/5] iio: pressure: bmp280: simplify driver initialization logic Date: Mon, 4 Jul 2022 02:29:55 +0200 Message-Id: <20220704002957.207850-1-ang.iglesiasg@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Extended chip_info structure with default values for configuration params to simplify and unify initialization logic for different supported sensors Signed-off-by: Angel Iglesias --- drivers/iio/pressure/bmp280-core.c | 91 ++++++++++++++++++------------ 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index bf8167f43c56..f39160b17cb3 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -107,19 +107,28 @@ struct bmp280_data { }; struct bmp280_chip_info { + unsigned int id_reg; + + int num_channels; + unsigned int start_up_time; + const int *oversampling_temp_avail; int num_oversampling_temp_avail; + int oversampling_temp_default; const int *oversampling_press_avail; int num_oversampling_press_avail; + int oversampling_press_default; const int *oversampling_humid_avail; int num_oversampling_humid_avail; + int oversampling_humid_default; int (*chip_config)(struct bmp280_data *); int (*read_temp)(struct bmp280_data *, int *); int (*read_press)(struct bmp280_data *, int *, int *); int (*read_humid)(struct bmp280_data *, int *, int *); + int (*read_calib)(struct bmp280_data *, unsigned int); }; /* @@ -147,15 +156,14 @@ static const struct iio_chan_spec bmp280_channels[] = { }, }; -static int bmp280_read_calib(struct bmp280_data *data, - struct bmp280_calib *calib, - unsigned int chip) +static int bmp280_read_calib(struct bmp280_data *data, unsigned int chip) { int ret; unsigned int tmp; __le16 l16; __be16 b16; struct device *dev = data->dev; + struct bmp280_calib *calib = &data->calib.bmp280; __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2]; __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2]; @@ -640,15 +648,22 @@ static int bmp280_chip_config(struct bmp280_data *data) static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; static const struct bmp280_chip_info bmp280_chip_info = { + .id_reg = BMP280_REG_ID, + .start_up_time = 2000, + .num_channels = 2, + .oversampling_temp_avail = bmp280_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), + .oversampling_temp_default = ilog2(2), .oversampling_press_avail = bmp280_oversampling_avail, .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), + .oversampling_press_default = ilog2(16), .chip_config = bmp280_chip_config, .read_temp = bmp280_read_temp, .read_press = bmp280_read_press, + .read_calib = bmp280_read_calib, }; static int bme280_chip_config(struct bmp280_data *data) @@ -670,19 +685,27 @@ static int bme280_chip_config(struct bmp280_data *data) } static const struct bmp280_chip_info bme280_chip_info = { + .id_reg = BMP280_REG_ID, + .start_up_time = 2000, + .num_channels = 3, + .oversampling_temp_avail = bmp280_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), + .oversampling_temp_default = ilog2(2), .oversampling_press_avail = bmp280_oversampling_avail, .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), + .oversampling_press_default = ilog2(16), .oversampling_humid_avail = bmp280_oversampling_avail, .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), + .oversampling_humid_default = ilog2(16), .chip_config = bme280_chip_config, .read_temp = bmp280_read_temp, .read_press = bmp280_read_press, .read_humid = bmp280_read_humid, + .read_calib = bmp280_read_calib, }; static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) @@ -748,11 +771,11 @@ static int bmp180_read_adc_temp(struct bmp280_data *data, int *val) return 0; } -static int bmp180_read_calib(struct bmp280_data *data, - struct bmp180_calib *calib) +static int bmp180_read_calib(struct bmp280_data *data, unsigned int chip) { int ret; int i; + struct bmp180_calib *calib = &data->calib.bmp180; __be16 buf[BMP180_REG_CALIB_COUNT / 2]; ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf, @@ -913,17 +936,24 @@ static const int bmp180_oversampling_temp_avail[] = { 1 }; static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; static const struct bmp280_chip_info bmp180_chip_info = { + .id_reg = BMP280_REG_ID, + .start_up_time = 2000, + .num_channels = 2, + .oversampling_temp_avail = bmp180_oversampling_temp_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp180_oversampling_temp_avail), + .oversampling_temp_default = ilog2(1), .oversampling_press_avail = bmp180_oversampling_press_avail, .num_oversampling_press_avail = ARRAY_SIZE(bmp180_oversampling_press_avail), + .oversampling_press_default = ilog2(8), .chip_config = bmp180_chip_config, .read_temp = bmp180_read_temp, .read_press = bmp180_read_press, + .read_calib = bmp180_read_calib, }; static irqreturn_t bmp085_eoc_irq(int irq, void *d) @@ -993,6 +1023,7 @@ int bmp280_common_probe(struct device *dev, int ret; struct iio_dev *indio_dev; struct bmp280_data *data; + const struct bmp280_chip_info *chip_info; unsigned int chip_id; struct gpio_desc *gpiod; @@ -1011,30 +1042,25 @@ int bmp280_common_probe(struct device *dev, switch (chip) { case BMP180_CHIP_ID: - indio_dev->num_channels = 2; - data->chip_info = &bmp180_chip_info; - data->oversampling_press = ilog2(8); - data->oversampling_temp = ilog2(1); - data->start_up_time = 10000; + chip_info = &bmp180_chip_info; break; case BMP280_CHIP_ID: - indio_dev->num_channels = 2; - data->chip_info = &bmp280_chip_info; - data->oversampling_press = ilog2(16); - data->oversampling_temp = ilog2(2); - data->start_up_time = 2000; + chip_info = &bmp280_chip_info; break; case BME280_CHIP_ID: - indio_dev->num_channels = 3; - data->chip_info = &bme280_chip_info; - data->oversampling_press = ilog2(16); - data->oversampling_humid = ilog2(16); - data->oversampling_temp = ilog2(2); - data->start_up_time = 2000; + chip_info = &bme280_chip_info; break; default: return -EINVAL; } + data->chip_info = chip_info; + + /* apply initial values from chip info structure */ + indio_dev->num_channels = chip_info->num_channels; + data->oversampling_press = chip_info->oversampling_press_default; + data->oversampling_humid = chip_info->oversampling_humid_default; + data->oversampling_temp = chip_info->oversampling_temp_default; + data->start_up_time = chip_info->start_up_time; /* Bring up regulators */ regulator_bulk_set_supply_names(data->supplies, @@ -1071,7 +1097,8 @@ int bmp280_common_probe(struct device *dev, } data->regmap = regmap; - ret = regmap_read(regmap, BMP280_REG_ID, &chip_id); + + ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id); if (ret < 0) return ret; if (chip_id != chip) { @@ -1091,20 +1118,12 @@ int bmp280_common_probe(struct device *dev, * non-volatile memory during production". Let's read them out at probe * time once. They will not change. */ - if (chip_id == BMP180_CHIP_ID) { - ret = bmp180_read_calib(data, &data->calib.bmp180); - if (ret < 0) { - dev_err(data->dev, - "failed to read calibration coefficients\n"); - return ret; - } - } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) { - ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id); - if (ret < 0) { - dev_err(data->dev, - "failed to read calibration coefficients\n"); - return ret; - } + + ret = data->chip_info->read_calib(data, chip_id); + if (ret < 0) { + dev_err(data->dev, + "failed to read calibration coefficients\n"); + return ret; } /* From patchwork Mon Jul 4 00:32:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angel Iglesias X-Patchwork-Id: 12904531 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 13F22C433EF for ; Mon, 4 Jul 2022 00:32:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229505AbiGDAck (ORCPT ); Sun, 3 Jul 2022 20:32:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229471AbiGDAcj (ORCPT ); Sun, 3 Jul 2022 20:32:39 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6546D6; Sun, 3 Jul 2022 17:32:37 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id u12-20020a05600c210c00b003a02b16d2b8so4749075wml.2; Sun, 03 Jul 2022 17:32:37 -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:mime-version :content-transfer-encoding; bh=a55OgZ+yBpctGqHaRumlXfS3a2jMHUBRUbZrBnrGLA4=; b=V52Iusa5T2ZrLDR00+0isXfOQ/tk6raogw50zTZJHsgRdnpVZrzGwdgrghBrBCMoBO 0iHNTxGHjrabetO4OGW3zVdxmz6SrIfNsUfIcpsu6e50q9RdPOwgQNgyBprHOUFRIlDr sDzB2+XzDiqb8EXn+UaM+6DsYttem9s82MXPpgBsFD6w+8txDH+SB4SgwPMFcDitq5jZ 7m8FWK+AnBYb/u8Er75kiI8Yn90E6EwMPcNYZTMoT5Nj5oHypMlZBR6qL0Lcqp7dSWf7 W4K1pwehoTTecW+lE+IXlr0tNFvJmFrJryawmVBEn3HHzQoGaJ9jwurqPX1UfbH7sGwu 1Ijg== 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:mime-version :content-transfer-encoding; bh=a55OgZ+yBpctGqHaRumlXfS3a2jMHUBRUbZrBnrGLA4=; b=B2UC1BzdVmvYEAd6G30FxQiysUfCkut6BsCK/fIs9RUjcpCWEDHJrx0ajtcooEF3Z8 T50OHmAM0dwv1csOxI937qCSLD/qtJ0RUABFDx2xLFl0AO4aeMr7BaW9tlRsWAF2WTNW wRauKVhDNu+9FIpo9SBs6wXIRkSoCeS0mo5dE3LEdWzwDhBjC51IiWbXyXczFBpZlPFK WyNIJWU4H+LhuiaA0CSxbpYKyTlZCDqexaqf62K0BoNm+CCMLJkrOIEHAbcx34F3k3wY C897yXmouE97LvBiCTwcNytaZZ83nvCxg7HdLqOI8ChMyOPk82+ri3+2EZmq7OBNitpW n3Ow== X-Gm-Message-State: AJIora/cu/nR7s6oAYZzUgdjrXx+Cc3hcleBE/ddpGnAs2gDqU26ogpH Of/YaTalDQgRHak7LiQhzCEZrEYCBdE= X-Google-Smtp-Source: AGRyM1uaWxtqne4nLnJ251JuUZNt3KV4InotlNr860xuqE5Yr82ZYFBfzlDa23vw67YdMVw36M3a2g== X-Received: by 2002:a05:600c:3c8a:b0:3a0:4ad8:d3c4 with SMTP id bg10-20020a05600c3c8a00b003a04ad8d3c4mr30253536wmb.43.1656894756278; Sun, 03 Jul 2022 17:32:36 -0700 (PDT) Received: from xps-work.lan (214.red-83-37-4.dynamicip.rima-tde.net. [83.37.4.214]) by smtp.gmail.com with ESMTPSA id i8-20020a5d5588000000b002102f2fac37sm28357842wrv.51.2022.07.03.17.32.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 17:32:35 -0700 (PDT) From: Angel Iglesias To: Jonathan Cameron Cc: Dan Carpenter , Angel Iglesias , Lars-Peter Clausen , Paul Cercueil , Ulf Hansson , "Rafael J. Wysocki" , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/5] iio: pressure: bmp280: Add support for BMP380 sensor family Date: Mon, 4 Jul 2022 02:32:09 +0200 Message-Id: <20220704003219.208409-1-ang.iglesiasg@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Adds compatibility with the new generation of this sensor, the BMP380 Includes basic sensor initialization to do pressure and temp measurements and allows tuning oversampling settings for each channel The compensation algorithms are adapted from the device datasheet and the repository https://github.com/BoschSensortec/BMP3-Sensor-API Signed-off-by: Angel Iglesias Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/iio/pressure/bmp280-core.c | 354 +++++++++++++++++++++++++++ drivers/iio/pressure/bmp280-i2c.c | 5 + drivers/iio/pressure/bmp280-regmap.c | 55 +++++ drivers/iio/pressure/bmp280-spi.c | 5 + drivers/iio/pressure/bmp280.h | 101 ++++++++ 5 files changed, 520 insertions(+) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index f39160b17cb3..0d2395a28df8 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -12,10 +12,12 @@ * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf + * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf */ #define pr_fmt(fmt) "bmp280: " fmt +#include #include #include #include @@ -29,6 +31,7 @@ #include #include #include +#include #include "bmp280.h" @@ -74,6 +77,24 @@ struct bmp280_calib { s8 H6; }; +/* See datasheet Section 3.11.1. */ +struct bmp380_calib { + u16 T1; + u16 T2; + s8 T3; + s16 P1; + s16 P2; + s8 P3; + s8 P4; + u16 P5; + u16 P6; + s8 P7; + s8 P8; + s16 P9; + s8 P10; + s8 P11; +}; + static const char *const bmp280_supply_names[] = { "vddd", "vdda" }; @@ -90,6 +111,7 @@ struct bmp280_data { union { struct bmp180_calib bmp180; struct bmp280_calib bmp280; + struct bmp380_calib bmp380; } calib; struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES]; unsigned int start_up_time; /* in microseconds */ @@ -138,6 +160,25 @@ struct bmp280_chip_info { enum { T1, T2, T3 }; enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; +enum { + /* Temperature calib indexes */ + BMP380_T1 = 0, + BMP380_T2 = 2, + BMP380_T3 = 4, + /* Pressure calib indexes */ + BMP380_P1 = 5, + BMP380_P2 = 7, + BMP380_P3 = 9, + BMP380_P4 = 10, + BMP380_P5 = 11, + BMP380_P6 = 13, + BMP380_P7 = 15, + BMP380_P8 = 16, + BMP380_P9 = 17, + BMP380_P10 = 19, + BMP380_P11 = 20 +}; + static const struct iio_chan_spec bmp280_channels[] = { { .type = IIO_PRESSURE, @@ -708,6 +749,309 @@ static const struct bmp280_chip_info bme280_chip_info = { .read_calib = bmp280_read_calib, }; +/* Send a command to BMP3XX sensors */ +static int bmp380_cmd(struct bmp280_data *data, u8 cmd) +{ + int ret; + unsigned int reg; + + /* check if device is ready to process a command */ + ret = regmap_read(data->regmap, BMP380_REG_STATUS, ®); + if (ret) { + dev_err(data->dev, "failed to read error register\n"); + return ret; + } + if (!(cmd & BMP380_STATUS_CMD_RDY_MASK)) { + dev_err(data->dev, "device is not ready to accept commands\n"); + return -EBUSY; + } + + /* send command to process */ + ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd); + if (ret) { + dev_err(data->dev, "failed to send command to device\n"); + return ret; + } + /* wait for 2ms for command to be proccessed */ + usleep_range(data->start_up_time, data->start_up_time + 100); + /* check for command processing error */ + ret = regmap_read(data->regmap, BMP380_REG_ERROR, ®); + if (ret) { + dev_err(data->dev, "error reading ERROR reg\n"); + return ret; + } + if (reg & BMP380_ERR_CMD_MASK) { + dev_err(data->dev, "error processing command 0x%X\n", cmd); + return -EINVAL; + } + dev_dbg(data->dev, "Command 0x%X proccessed successfully\n", cmd); + + return 0; +} + +/* + * Returns temperature in DegC, resolution is 0.01 DegC. Output value of + * "5123" equals 51.23 DegC. t_fine carries fine temperature as global + * value. + * + * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo + * https://github.com/BoschSensortec/BMP3-Sensor-API + */ +static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp) +{ + s64 var1, var2, var3, var4, var5, var6, comp_temp; + struct bmp380_calib *calib = &data->calib.bmp380; + + var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8); + var2 = var1 * ((s64) calib->T2); + var3 = var1 * var1; + var4 = var3 * ((s64) calib->T3); + var5 = (var2 << 18) + var4; + var6 = var5 >> 32; + data->t_fine = (s32) var6; + comp_temp = (var6 * 25) >> 14; + + comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP); + return (s32) comp_temp; +} + +/* + * Returns pressure in Pa as unsigned 32 bit integer in fractional Pascal. + * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa + * + * Taken from datasheet, Section 9.3. "Pressure compensation" and repository + * https://github.com/BoschSensortec/BMP3-Sensor-API + */ +static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press) +{ + s64 var1, var2, var3, var4, var5, var6, offset, sensitivity; + u64 comp_press; + struct bmp380_calib *calib = &data->calib.bmp380; + + var1 = ((s64)data->t_fine) * ((s64)data->t_fine); + var2 = var1 >> 6; + var3 = (var2 * ((s64) data->t_fine)) >> 8; + var4 = (((s64)calib->P8) * var3) >> 5; + var5 = (((s64) calib->P7) * var1) << 4; + var6 = (((s64) calib->P6) * ((s64)data->t_fine)) << 22; + offset = (((s64)calib->P5) << 47) + var4 + var5 + var6; + var2 = (((s64)calib->P4) * var3) >> 5; + var4 = (((s64) calib->P3) * var1) << 2; + var5 = (((s64) calib->P2) - ((s64) 1<<14)) * + (((s64)data->t_fine) << 21); + sensitivity = ((((s64) calib->P1) - ((s64) 1 << 14)) << 46) + + var2 + var4 + var5; + var1 = (sensitivity >> 24) * ((s64)adc_press); + var2 = ((s64)calib->P10) * ((s64) data->t_fine); + var3 = var2 + (((s64) calib->P9) << 16); + var4 = (var3 * ((s64)adc_press)) >> 13; + + /* dividing by 10 followed by multiplying by 10 + * To avoid overflow caused by (uncomp_data->pressure * partial_data4) + */ + var5 = (((s64)adc_press) * (var4 / 10)) >> 9; + var5 *= 10; + var6 = ((s64)adc_press) * ((s64)adc_press); + var2 = (((s64)calib->P11) * var6) >> 16; + var3 = (var2 * ((s64)adc_press)) >> 7; + var4 = (offset >> 2) + var1 + var5 + var3; + comp_press = ((u64)var4 * 25) >> 40; + + comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES); + return (u32)comp_press; +} + +static int bmp380_read_temp(struct bmp280_data *data, int *val) +{ + int ret; + u8 tmp[3]; + u32 adc_temp; + s32 comp_temp; + + ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB, tmp, 3); + if (ret < 0) { + dev_err(data->dev, "failed to read temperature\n"); + return ret; + } + + adc_temp = get_unaligned_le24(tmp); + if (adc_temp == BMP380_TEMP_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading temperature skipped\n"); + return -EIO; + } + comp_temp = bmp380_compensate_temp(data, adc_temp); + + /* + * val might be NULL if we're called by the read_press routine, + * who only cares about the carry over t_fine value. + */ + if (val) { + /* IIO reports temperatures in mC */ + *val = comp_temp * 10; + return IIO_VAL_INT; + } + + return 0; +} + +static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) +{ + int ret; + u8 tmp[3]; + u32 adc_press; + s32 comp_press; + + /* Read and compensate temperature so we get a reading of t_fine. */ + ret = bmp380_read_temp(data, NULL); + if (ret < 0) + return ret; + + ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, tmp, 3); + if (ret < 0) { + dev_err(data->dev, "failed to read pressure\n"); + return ret; + } + + adc_press = get_unaligned_le24(tmp); + if (adc_press == BMP380_PRESS_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading pressure skipped\n"); + return -EIO; + } + comp_press = bmp380_compensate_press(data, adc_press); + + *val = comp_press; + /* Compensated pressure is in cPa (centipascals) */ + *val2 = 100000; + + return IIO_VAL_FRACTIONAL; +} + +static int bmp380_read_calib(struct bmp280_data *data, unsigned int chip) +{ + int ret; + struct bmp380_calib *calib = &data->calib.bmp380; + u8 buf[BMP380_CALIB_REG_COUNT]; + + /* Read temperature calibration values. */ + ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START, buf, + BMP380_CALIB_REG_COUNT); + if (ret < 0) { + dev_err(data->dev, + "failed to read temperature calibration parameters\n"); + return ret; + } + + /* Toss the temperature calibration data into the entropy pool */ + add_device_randomness(buf, sizeof(buf)); + + /* Parse calibration data */ + calib->T1 = get_unaligned_le16(&buf[BMP380_T1]); + calib->T2 = get_unaligned_le16(&buf[BMP380_T2]); + calib->T3 = buf[BMP380_T3]; + calib->P1 = get_unaligned_le16(&buf[BMP380_P1]); + calib->P2 = get_unaligned_le16(&buf[BMP380_P2]); + calib->P3 = buf[BMP380_P3]; + calib->P4 = buf[BMP380_P4]; + calib->P5 = get_unaligned_le16(&buf[BMP380_P5]); + calib->P6 = get_unaligned_le16(&buf[BMP380_P6]); + calib->P7 = buf[BMP380_P7]; + calib->P8 = buf[BMP380_P8]; + calib->P9 = get_unaligned_le16(&buf[BMP380_P9]); + calib->P10 = buf[BMP380_P10]; + calib->P11 = buf[BMP380_P11]; + + return 0; +} + +static int bmp380_chip_config(struct bmp280_data *data) +{ + u8 osrs; + unsigned int tmp; + int ret; + + /* configure power control register */ + ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, + BMP380_CTRL_SENSORS_MASK | BMP380_MODE_MASK, + BMP380_CTRL_SENSORS_PRESS_EN | + BMP380_CTRL_SENSORS_TEMP_EN | + FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL)); + if (ret < 0) { + dev_err(data->dev, + "failed to write operation control register\n"); + return ret; + } + + /* configure oversampling */ + osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) | + FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press); + + ret = regmap_write_bits(data->regmap, BMP380_REG_OSR, + BMP380_OSRS_TEMP_MASK | BMP380_OSRS_PRESS_MASK, + osrs); + if (ret < 0) { + dev_err(data->dev, "failed to write oversampling register\n"); + return ret; + } + + /* configure output data rate */ + ret = regmap_write_bits(data->regmap, BMP380_REG_ODR, + BMP380_ODRS_MASK, BMP380_ODRS_50HZ); + if (ret < 0) { + dev_err(data->dev, "failed to write ODR selection register\n"); + return ret; + } + + /* set filter data */ + ret = regmap_update_bits(data->regmap, BMP380_REG_CONFIG, + BMP380_FILTER_MASK, + FIELD_PREP(BMP380_FILTER_MASK, BMP380_FILTER_3X)); + if (ret < 0) { + dev_err(data->dev, "failed to write config register\n"); + return ret; + } + + /* wait startup_time before verifying config changes */ + usleep_range(data->start_up_time, data->start_up_time + 100); + + /* check config error flag */ + ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); + if (ret < 0) { + dev_err(data->dev, + "failed to read error register\n"); + return ret; + } + if (tmp & BMP380_ERR_CONF_MASK) { + dev_warn(data->dev, + "sensor flagged configuration as incompatible\n"); + return -EINVAL; + } + + return 0; +} + +static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; + +static const struct bmp280_chip_info bmp380_chip_info = { + .id_reg = BMP380_REG_ID, + .start_up_time = 2000, + .num_channels = 2, + + .oversampling_temp_avail = bmp380_oversampling_avail, + .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail), + .oversampling_temp_default = ilog2(1), + + .oversampling_press_avail = bmp380_oversampling_avail, + .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail), + .oversampling_press_default = ilog2(4), + + .chip_config = bmp380_chip_config, + .read_temp = bmp380_read_temp, + .read_press = bmp380_read_press, + .read_calib = bmp380_read_calib, +}; + static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) { int ret; @@ -1050,6 +1394,9 @@ int bmp280_common_probe(struct device *dev, case BME280_CHIP_ID: chip_info = &bme280_chip_info; break; + case BMP380_CHIP_ID: + chip_info = &bmp380_chip_info; + break; default: return -EINVAL; } @@ -1107,6 +1454,13 @@ int bmp280_common_probe(struct device *dev, return -EINVAL; } + /* BMP3xx requires soft-reset as part of initialization */ + if (chip_id == BMP380_CHIP_ID) { + ret = bmp380_cmd(data, BMP380_CMD_SOFT_RESET); + if (ret < 0) + return ret; + } + ret = data->chip_info->chip_config(data); if (ret < 0) return ret; diff --git a/drivers/iio/pressure/bmp280-i2c.c b/drivers/iio/pressure/bmp280-i2c.c index 35045bd92846..31a8a0daa39a 100644 --- a/drivers/iio/pressure/bmp280-i2c.c +++ b/drivers/iio/pressure/bmp280-i2c.c @@ -19,6 +19,9 @@ static int bmp280_i2c_probe(struct i2c_client *client, case BME280_CHIP_ID: regmap_config = &bmp280_regmap_config; break; + case BMP380_CHIP_ID: + regmap_config = &bmp380_regmap_config; + break; default: return -EINVAL; } @@ -37,6 +40,7 @@ static int bmp280_i2c_probe(struct i2c_client *client, } static const struct of_device_id bmp280_of_i2c_match[] = { + { .compatible = "bosch,bmp380", .data = (void *)BMP380_CHIP_ID }, { .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID }, { .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID }, { .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID }, @@ -46,6 +50,7 @@ static const struct of_device_id bmp280_of_i2c_match[] = { MODULE_DEVICE_TABLE(of, bmp280_of_i2c_match); static const struct i2c_device_id bmp280_i2c_id[] = { + {"bmp380", BMP380_CHIP_ID }, {"bmp280", BMP280_CHIP_ID }, {"bmp180", BMP180_CHIP_ID }, {"bmp085", BMP180_CHIP_ID }, diff --git a/drivers/iio/pressure/bmp280-regmap.c b/drivers/iio/pressure/bmp280-regmap.c index da136dbadc8f..b440fa41bf12 100644 --- a/drivers/iio/pressure/bmp280-regmap.c +++ b/drivers/iio/pressure/bmp280-regmap.c @@ -72,6 +72,49 @@ static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg) } } +static bool bmp380_is_writeable_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case BMP380_REG_CMD: + case BMP380_REG_CONFIG: + case BMP380_REG_FIFO_CONFIG_1: + case BMP380_REG_FIFO_CONFIG_2: + case BMP380_REG_FIFO_WATERMARK_LSB: + case BMP380_REG_FIFO_WATERMARK_MSB: + case BMP380_REG_POWER_CONTROL: + case BMP380_REG_INT_CONTROL: + case BMP380_REG_IF_CONFIG: + case BMP380_REG_ODR: + case BMP380_REG_OSR: + return true; + default: + return false; + } +} + +static bool bmp380_is_volatile_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case BMP380_REG_TEMP_XLSB: + case BMP380_REG_TEMP_LSB: + case BMP380_REG_TEMP_MSB: + case BMP380_REG_PRESS_XLSB: + case BMP380_REG_PRESS_LSB: + case BMP380_REG_PRESS_MSB: + case BMP380_REG_SENSOR_TIME_XLSB: + case BMP380_REG_SENSOR_TIME_LSB: + case BMP380_REG_SENSOR_TIME_MSB: + case BMP380_REG_INT_STATUS: + case BMP380_REG_FIFO_DATA: + case BMP380_REG_STATUS: + case BMP380_REG_ERROR: + case BMP380_REG_EVENT: + return true; + default: + return false; + } +} + const struct regmap_config bmp280_regmap_config = { .reg_bits = 8, .val_bits = 8, @@ -83,3 +126,15 @@ const struct regmap_config bmp280_regmap_config = { .volatile_reg = bmp280_is_volatile_reg, }; EXPORT_SYMBOL(bmp280_regmap_config); + +const struct regmap_config bmp380_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = BMP380_REG_CMD, + .cache_type = REGCACHE_RBTREE, + + .writeable_reg = bmp380_is_writeable_reg, + .volatile_reg = bmp380_is_volatile_reg, +}; +EXPORT_SYMBOL(bmp380_regmap_config); diff --git a/drivers/iio/pressure/bmp280-spi.c b/drivers/iio/pressure/bmp280-spi.c index 41f6cc56d229..303c41130343 100644 --- a/drivers/iio/pressure/bmp280-spi.c +++ b/drivers/iio/pressure/bmp280-spi.c @@ -66,6 +66,9 @@ static int bmp280_spi_probe(struct spi_device *spi) case BME280_CHIP_ID: regmap_config = &bmp280_regmap_config; break; + case BMP380_CHIP_ID: + regmap_config = &bmp380_regmap_config; + break; default: return -EINVAL; } @@ -92,6 +95,7 @@ static const struct of_device_id bmp280_of_spi_match[] = { { .compatible = "bosch,bmp181", }, { .compatible = "bosch,bmp280", }, { .compatible = "bosch,bme280", }, + { .compatible = "bosch,bmp380", }, { }, }; MODULE_DEVICE_TABLE(of, bmp280_of_spi_match); @@ -101,6 +105,7 @@ static const struct spi_device_id bmp280_spi_id[] = { { "bmp181", BMP180_CHIP_ID }, { "bmp280", BMP280_CHIP_ID }, { "bme280", BME280_CHIP_ID }, + { "bmp380", BMP380_CHIP_ID }, { } }; MODULE_DEVICE_TABLE(spi, bmp280_spi_id); diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 57ba0e85db91..fd38906c889c 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -3,6 +3,105 @@ #include #include +/* BMP380 specific registers */ +#define BMP380_REG_CMD 0x7E +#define BMP380_REG_CONFIG 0x1F +#define BMP380_REG_ODR 0X1D +#define BMP380_REG_OSR 0X1C +#define BMP380_REG_POWER_CONTROL 0X1B +#define BMP380_REG_IF_CONFIG 0X1A +#define BMP380_REG_INT_CONTROL 0X19 +#define BMP380_REG_INT_STATUS 0X11 +#define BMP380_REG_EVENT 0X10 +#define BMP380_REG_STATUS 0X03 +#define BMP380_REG_ERROR 0X02 +#define BMP380_REG_ID 0X00 + +#define BMP380_REG_FIFO_CONFIG_1 0X18 +#define BMP380_REG_FIFO_CONFIG_2 0X17 +#define BMP380_REG_FIFO_WATERMARK_MSB 0X16 +#define BMP380_REG_FIFO_WATERMARK_LSB 0X15 +#define BMP380_REG_FIFO_DATA 0X14 +#define BMP380_REG_FIFO_LENGTH_MSB 0X13 +#define BMP380_REG_FIFO_LENGTH_LSB 0X12 + +#define BMP380_REG_SENSOR_TIME_MSB 0X0E +#define BMP380_REG_SENSOR_TIME_LSB 0X0D +#define BMP380_REG_SENSOR_TIME_XLSB 0X0C + +#define BMP380_REG_TEMP_MSB 0X09 +#define BMP380_REG_TEMP_LSB 0X08 +#define BMP380_REG_TEMP_XLSB 0X07 + +#define BMP380_REG_PRESS_MSB 0X06 +#define BMP380_REG_PRESS_LSB 0X05 +#define BMP380_REG_PRESS_XLSB 0X04 + +#define BMP380_REG_CALIB_TEMP_START 0x31 +#define BMP380_CALIB_REG_COUNT 21 + +#define BMP380_FILTER_MASK GENMASK(3, 1) +#define BMP380_FILTER_OFF 0 +#define BMP380_FILTER_1X 1 +#define BMP380_FILTER_3X 2 +#define BMP380_FILTER_7X 3 +#define BMP380_FILTER_15X 4 +#define BMP380_FILTER_31X 5 +#define BMP380_FILTER_63X 6 +#define BMP380_FILTER_127X 7 + +#define BMP380_OSRS_TEMP_MASK GENMASK(5, 3) +#define BMP380_OSRS_PRESS_MASK GENMASK(2, 0) + +#define BMP380_ODRS_MASK GENMASK(4, 0) +#define BMP380_ODRS_200HZ 0x00 +#define BMP380_ODRS_100HZ 0x01 +#define BMP380_ODRS_50HZ 0x02 +#define BMP380_ODRS_25HZ 0x03 +#define BMP380_ODRS_12_5HZ 0x04 +#define BMP380_ODRS_6_25HZ 0x05 +#define BMP380_ODRS_3_1HZ 0x06 +#define BMP380_ODRS_1_5HZ 0x07 +#define BMP380_ODRS_0_78HZ 0x08 +#define BMP380_ODRS_0_39HZ 0x09 +#define BMP380_ODRS_0_2HZ 0x0A +#define BMP380_ODRS_0_1HZ 0x0B +#define BMP380_ODRS_0_05HZ 0x0C +#define BMP380_ODRS_0_02HZ 0x0D +#define BMP380_ODRS_0_01HZ 0x0E +#define BMP380_ODRS_0_006HZ 0x0F +#define BMP380_ODRS_0_003HZ 0x10 +#define BMP380_ODRS_0_0015HZ 0x11 + +#define BMP380_CTRL_SENSORS_MASK GENMASK(1, 0) +#define BMP380_CTRL_SENSORS_PRESS_EN BIT(0) +#define BMP380_CTRL_SENSORS_TEMP_EN BIT(1) +#define BMP380_MODE_MASK GENMASK(5, 4) +#define BMP380_MODE_SLEEP 0 +#define BMP380_MODE_FORCED 1 +#define BMP380_MODE_NORMAL 3 + +#define BMP380_MIN_TEMP -4000 +#define BMP380_MAX_TEMP 8500 +#define BMP380_MIN_PRES 3000000 +#define BMP380_MAX_PRES 12500000 + +#define BMP380_CMD_NOOP 0X00 +#define BMP380_CMD_EXTMODE_EN_MID 0x34 +#define BMP380_CMD_FIFO_FLUSH 0XB0 +#define BMP380_CMD_SOFT_RESET 0xB6 + +#define BMP380_STATUS_CMD_RDY_MASK BIT(4) +#define BMP380_STATUS_DRDY_PRESS_MASK BIT(5) +#define BMP380_STATUS_DRDY_TEMP_MASK BIT(6) + +#define BMP380_ERR_FATAL_MASK BIT(0) +#define BMP380_ERR_CMD_MASK BIT(1) +#define BMP380_ERR_CONF_MASK BIT(2) + +#define BMP380_TEMP_SKIPPED 0x800000 +#define BMP380_PRESS_SKIPPED 0x800000 + /* BMP280 specific registers */ #define BMP280_REG_HUMIDITY_LSB 0xFE #define BMP280_REG_HUMIDITY_MSB 0xFD @@ -92,6 +191,7 @@ #define BMP280_REG_RESET 0xE0 #define BMP280_REG_ID 0xD0 +#define BMP380_CHIP_ID 0x50 #define BMP180_CHIP_ID 0x55 #define BMP280_CHIP_ID 0x58 #define BME280_CHIP_ID 0x60 @@ -105,6 +205,7 @@ /* Regmap configurations */ extern const struct regmap_config bmp180_regmap_config; extern const struct regmap_config bmp280_regmap_config; +extern const struct regmap_config bmp380_regmap_config; /* Probe called from different transports */ int bmp280_common_probe(struct device *dev, From patchwork Mon Jul 4 00:33:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angel Iglesias X-Patchwork-Id: 12904534 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 32CB0C433EF for ; Mon, 4 Jul 2022 00:33:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229471AbiGDAdr (ORCPT ); Sun, 3 Jul 2022 20:33:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbiGDAdq (ORCPT ); Sun, 3 Jul 2022 20:33:46 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B0C16146; Sun, 3 Jul 2022 17:33:45 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id b26so11198185wrc.2; Sun, 03 Jul 2022 17:33:45 -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:mime-version :content-transfer-encoding; bh=P4iMzV5q1Bszb1Le7bggIfrAtHC4PWgCOOkJ4nY/9is=; b=hJOCbFbu6z/DhqDssnsA9Si0sFSGNdYGwgPjwgfOEFWjGPyooLZal48rBz0VHi1cOi NI9CkSjs4qId8cZNo77X38xx8hCOajU674SJFScO/C6btn6UkM6zTBx4utDgGoqNkb/A znFO/SiWV9QM0n3CZvWLXPdTUkWk3aaoIGfpcJWWAytj+iicoY5eiPZTI14a6MyvWjg9 TgV4CMWWVB43NJ1XRdupLMEVHLkizXOKnuEEIVDMasyfVhmZ6XTqx/Ytg7S0bXsGWh44 LtOGVqk4ejkMhFjwOG0BM+iwcYmnM6gqWvwRqCeQivIAL4dhK2zUmmcjYOBV4vj5ASQf D8lg== 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:mime-version :content-transfer-encoding; bh=P4iMzV5q1Bszb1Le7bggIfrAtHC4PWgCOOkJ4nY/9is=; b=wLAjznr2b+/dnGOiZBgpYyb+jVMYgRvZaDtpgBd4WQ4DF5dy6V900jEof2Wppu2L8U SXF9MLabNuQRsuk9hW5JagSwJRadbDw+E2vbIL0/cfG73/2zCIw7I7tf6Q4DyLIcTKuf FYJ6oeVpaEjFkdCsRmJlJvR4aFjxlPu1F4u1JlrfSMf21oEAO3d+O+cmG3+y5fWocyyY AiPaUFJMZriNeXwbud7jQLHxfYkZl7/WuwwC7RgF0nCN/juQs6nfxBl9t8TJ76dQzl+E WnFHuw887jScA7wKPuib3fKI5IVH3YcEV5ZbfqKKk/WyE796oK6V765zJ1HqVzNg+ti5 PiaQ== X-Gm-Message-State: AJIora/alP3VvYE6K7PJHsCSZ3Sr+kpMAOE/ZIyVg50zI/h2mc142CAa TnbUIQTsYWut5dRfYXp39Qc= X-Google-Smtp-Source: AGRyM1srSO9Ejd3m5KbU4bXgQH8gWnlUsoJFn6ESCG7tpeZfk3l1GrgzwXnW7qzzMypHDXU9+mqdjQ== X-Received: by 2002:adf:f48e:0:b0:21d:6562:4717 with SMTP id l14-20020adff48e000000b0021d65624717mr5508345wro.428.1656894823807; Sun, 03 Jul 2022 17:33:43 -0700 (PDT) Received: from xps-work.lan (214.red-83-37-4.dynamicip.rima-tde.net. [83.37.4.214]) by smtp.gmail.com with ESMTPSA id q5-20020adff945000000b0021b9585276dsm28078126wrr.101.2022.07.03.17.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 17:33:43 -0700 (PDT) From: Angel Iglesias To: Jonathan Cameron Cc: Angel Iglesias , Lars-Peter Clausen , "Rafael J. Wysocki" , Paul Cercueil , Ulf Hansson , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/5] iio: pressure: bmp280: Adds more tunable config parameters for BMP380 Date: Mon, 4 Jul 2022 02:33:33 +0200 Message-Id: <20220704003337.208696-1-ang.iglesiasg@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Allows to configure the IIR filter coefficient and the sampling frequency The IIR filter coefficient is exposed using the sysfs attribute "filter_low_pass_3db_frequency" Signed-off-by: Angel Iglesias Reported-by: kernel test robot --- drivers/iio/pressure/bmp280-core.c | 338 ++++++++++++++++++++++++++--- drivers/iio/pressure/bmp280.h | 18 -- 2 files changed, 308 insertions(+), 48 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index 0d2395a28df8..fb321419bff8 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -99,6 +99,27 @@ static const char *const bmp280_supply_names[] = { "vddd", "vdda" }; +enum bmp380_odr { + BMP380_ODR_200HZ, + BMP380_ODR_100HZ, + BMP380_ODR_50HZ, + BMP380_ODR_25HZ, + BMP380_ODR_12_5HZ, + BMP380_ODR_6_25HZ, + BMP380_ODR_3_125HZ, + BMP380_ODR_1_5625HZ, + BMP380_ODR_0_78HZ, + BMP380_ODR_0_39HZ, + BMP380_ODR_0_2HZ, + BMP380_ODR_0_1HZ, + BMP380_ODR_0_05HZ, + BMP380_ODR_0_02HZ, + BMP380_ODR_0_01HZ, + BMP380_ODR_0_006HZ, + BMP380_ODR_0_003HZ, + BMP380_ODR_0_0015HZ +}; + #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names) struct bmp280_data { @@ -120,6 +141,16 @@ struct bmp280_data { u8 oversampling_press; u8 oversampling_temp; u8 oversampling_humid; + u8 iir_filter_coeff; + + /* BMP380 devices introduce sampling frequecy configuration. See + * datasheet sections 3.3.3. and 4.3.19. + * + * BMx280 devices allowed indirect configuration of sampling frequency + * changing the t_standby duration between measurements. See datasheet + * section 3.6.3 + */ + int sampling_freq; /* * Carryover value from temperature conversion, used in pressure @@ -131,6 +162,7 @@ struct bmp280_data { struct bmp280_chip_info { unsigned int id_reg; + const struct iio_chan_spec *channels; int num_channels; unsigned int start_up_time; @@ -146,6 +178,14 @@ struct bmp280_chip_info { int num_oversampling_humid_avail; int oversampling_humid_default; + const int *iir_filter_coeffs_avail; + int num_iir_filter_coeffs_avail; + int iir_filter_coeff_default; + + const int (*sampling_freq_avail)[2]; + int num_sampling_freq_avail; + int sampling_freq_default; + int (*chip_config)(struct bmp280_data *); int (*read_temp)(struct bmp280_data *, int *); int (*read_press)(struct bmp280_data *, int *, int *); @@ -197,6 +237,30 @@ static const struct iio_chan_spec bmp280_channels[] = { }, }; +static const struct iio_chan_spec bmp380_channels[] = { + { + .type = IIO_PRESSURE, + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), + }, + { + .type = IIO_TEMP, + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), + }, + { + .type = IIO_HUMIDITYRELATIVE, + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), + }, +}; + static int bmp280_read_calib(struct bmp280_data *data, unsigned int chip) { int ret; @@ -524,6 +588,25 @@ static int bmp280_read_raw(struct iio_dev *indio_dev, break; } break; + case IIO_CHAN_INFO_SAMP_FREQ: + if (unlikely(!data->chip_info->sampling_freq_avail)) { + ret = -EINVAL; + break; + } + + *val = data->chip_info->sampling_freq_avail[data->sampling_freq][0]; + *val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1]; + ret = IIO_VAL_INT_PLUS_MICRO; + break; + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: + if (unlikely(!data->chip_info->iir_filter_coeffs_avail)) { + ret = -EINVAL; + break; + } + + *val = data->chip_info->iir_filter_coeffs_avail[data->iir_filter_coeff]; + ret = IIO_VAL_INT; + break; default: ret = -EINVAL; break; @@ -540,14 +623,27 @@ static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data, int val) { int i; + int ret, prev; const int *avail = data->chip_info->oversampling_humid_avail; const int n = data->chip_info->num_oversampling_humid_avail; for (i = 0; i < n; i++) { if (avail[i] == val) { + prev = data->oversampling_humid; data->oversampling_humid = ilog2(val); - return data->chip_info->chip_config(data); + ret = data->chip_info->chip_config(data); + if (ret) { + /* + * Error applying new configuration. Might be + * an invalid configuration, will try to + * restore previous value just to be sure + */ + data->oversampling_humid = prev; + data->chip_info->chip_config(data); + return ret; + } + return 0; } } return -EINVAL; @@ -557,14 +653,27 @@ static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, int val) { int i; + int ret, prev; const int *avail = data->chip_info->oversampling_temp_avail; const int n = data->chip_info->num_oversampling_temp_avail; for (i = 0; i < n; i++) { if (avail[i] == val) { + prev = data->oversampling_temp; data->oversampling_temp = ilog2(val); - return data->chip_info->chip_config(data); + ret = data->chip_info->chip_config(data); + if (ret) { + /* + * Error applying new configuration. Might be + * an invalid configuration, will try to + * restore previous value just to be sure + */ + data->oversampling_temp = prev; + data->chip_info->chip_config(data); + return ret; + } + return 0; } } return -EINVAL; @@ -574,14 +683,87 @@ static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, int val) { int i; + int ret, prev; const int *avail = data->chip_info->oversampling_press_avail; const int n = data->chip_info->num_oversampling_press_avail; for (i = 0; i < n; i++) { if (avail[i] == val) { + prev = data->oversampling_press; data->oversampling_press = ilog2(val); - return data->chip_info->chip_config(data); + ret = data->chip_info->chip_config(data); + if (ret) { + /* + * Error applying new configuration. Might be + * an invalid configuration, will try to + * restore previous value just to be sure + */ + data->oversampling_press = prev; + data->chip_info->chip_config(data); + return ret; + } + return 0; + } + } + return -EINVAL; +} + +static int bmp280_write_sampling_frequency(struct bmp280_data *data, + int val, int val2) +{ + int i; + int ret, prev; + const int (*avail)[2] = data->chip_info->sampling_freq_avail; + const int n = data->chip_info->num_sampling_freq_avail; + + for (i = 0; i < n; i++) { + if (avail[i][0] == val && avail[i][1] == val2) { + prev = data->sampling_freq; + data->sampling_freq = i; + + ret = data->chip_info->chip_config(data); + if (ret) { + /* + * Error applying new configuration. Might be + * an invalid configuration, will try to + * restore previous value just to be sure + */ + data->sampling_freq = prev; + data->chip_info->chip_config(data); + return ret; + } + return 0; + } + } + return -EINVAL; +} + +static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val) +{ + int i; + int ret, prev; + const int *avail = data->chip_info->iir_filter_coeffs_avail; + const int n = data->chip_info->num_iir_filter_coeffs_avail; + + for (i = 0; i < n; i++) { + if (avail[i] == val) { + prev = data->iir_filter_coeff; + data->iir_filter_coeff = i; + + ret = data->chip_info->chip_config(data); + if (ret) { + /* + * Error applying new configuration. Might be + * an invalid configuration, will try to + * restore previous value just to be sure + */ + data->iir_filter_coeff = prev; + data->chip_info->chip_config(data); + return ret; + + } + return 0; } } return -EINVAL; @@ -616,6 +798,22 @@ static int bmp280_write_raw(struct iio_dev *indio_dev, pm_runtime_mark_last_busy(data->dev); pm_runtime_put_autosuspend(data->dev); break; + case IIO_CHAN_INFO_SAMP_FREQ: + pm_runtime_get_sync(data->dev); + mutex_lock(&data->lock); + ret = bmp280_write_sampling_frequency(data, val, val2); + mutex_unlock(&data->lock); + pm_runtime_mark_last_busy(data->dev); + pm_runtime_put_autosuspend(data->dev); + break; + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: + pm_runtime_get_sync(data->dev); + mutex_lock(&data->lock); + ret = bmp280_write_iir_filter_coeffs(data, val); + mutex_unlock(&data->lock); + pm_runtime_mark_last_busy(data->dev); + pm_runtime_put_autosuspend(data->dev); + break; default: return -EINVAL; } @@ -646,6 +844,17 @@ static int bmp280_read_avail(struct iio_dev *indio_dev, } *type = IIO_VAL_INT; return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_SAMP_FREQ: + *vals = (const int *)data->chip_info->sampling_freq_avail; + *type = IIO_VAL_INT_PLUS_MICRO; + /* Values are stored in a 2D matrix */ + *length = data->chip_info->num_sampling_freq_avail; + return IIO_AVAIL_LIST; + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: + *vals = data->chip_info->iir_filter_coeffs_avail; + *type = IIO_VAL_INT; + *length = data->chip_info->num_iir_filter_coeffs_avail; + return IIO_AVAIL_LIST; default: return -EINVAL; } @@ -691,6 +900,7 @@ static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; static const struct bmp280_chip_info bmp280_chip_info = { .id_reg = BMP280_REG_ID, .start_up_time = 2000, + .channels = bmp280_channels, .num_channels = 2, .oversampling_temp_avail = bmp280_oversampling_avail, @@ -728,6 +938,7 @@ static int bme280_chip_config(struct bmp280_data *data) static const struct bmp280_chip_info bme280_chip_info = { .id_reg = BMP280_REG_ID, .start_up_time = 2000, + .channels = bmp280_channels, .num_channels = 3, .oversampling_temp_avail = bmp280_oversampling_avail, @@ -965,18 +1176,39 @@ static int bmp380_read_calib(struct bmp280_data *data, unsigned int chip) return 0; } +static const int bmp380_odr_table[][2] = { + [BMP380_ODR_200HZ] = {200, 0}, + [BMP380_ODR_100HZ] = {100, 0}, + [BMP380_ODR_50HZ] = {50, 0}, + [BMP380_ODR_25HZ] = {25, 0}, + [BMP380_ODR_12_5HZ] = {12, 500000}, + [BMP380_ODR_6_25HZ] = {6, 250000}, + [BMP380_ODR_3_125HZ] = {3, 125000}, + [BMP380_ODR_1_5625HZ] = {1, 562500}, + [BMP380_ODR_0_78HZ] = {0, 781250}, + [BMP380_ODR_0_39HZ] = {0, 390625}, + [BMP380_ODR_0_2HZ] = {0, 195313}, + [BMP380_ODR_0_1HZ] = {0, 97656}, + [BMP380_ODR_0_05HZ] = {0, 48828}, + [BMP380_ODR_0_02HZ] = {0, 24414}, + [BMP380_ODR_0_01HZ] = {0, 12207}, + [BMP380_ODR_0_006HZ] = {0, 6104}, + [BMP380_ODR_0_003HZ] = {0, 3052}, + [BMP380_ODR_0_0015HZ] = {0, 1526}, +}; + static int bmp380_chip_config(struct bmp280_data *data) { u8 osrs; unsigned int tmp; int ret; + bool change, aux; /* configure power control register */ - ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, - BMP380_CTRL_SENSORS_MASK | BMP380_MODE_MASK, - BMP380_CTRL_SENSORS_PRESS_EN | - BMP380_CTRL_SENSORS_TEMP_EN | - FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL)); + ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL, + BMP380_CTRL_SENSORS_MASK, + BMP380_CTRL_SENSORS_PRESS_EN | + BMP380_CTRL_SENSORS_TEMP_EN); if (ret < 0) { dev_err(data->dev, "failed to write operation control register\n"); @@ -987,55 +1219,90 @@ static int bmp380_chip_config(struct bmp280_data *data) osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) | FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press); - ret = regmap_write_bits(data->regmap, BMP380_REG_OSR, - BMP380_OSRS_TEMP_MASK | BMP380_OSRS_PRESS_MASK, - osrs); + ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR, + BMP380_OSRS_TEMP_MASK | + BMP380_OSRS_PRESS_MASK, + osrs, &aux); if (ret < 0) { dev_err(data->dev, "failed to write oversampling register\n"); return ret; } + change = change || aux; /* configure output data rate */ - ret = regmap_write_bits(data->regmap, BMP380_REG_ODR, - BMP380_ODRS_MASK, BMP380_ODRS_50HZ); + ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR, + BMP380_ODRS_MASK, data->sampling_freq, + &aux); if (ret < 0) { dev_err(data->dev, "failed to write ODR selection register\n"); return ret; } + change = change || aux; /* set filter data */ - ret = regmap_update_bits(data->regmap, BMP380_REG_CONFIG, - BMP380_FILTER_MASK, - FIELD_PREP(BMP380_FILTER_MASK, BMP380_FILTER_3X)); + ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, + BMP380_FILTER_MASK, + FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff), + &aux); if (ret < 0) { dev_err(data->dev, "failed to write config register\n"); return ret; } + change = change || aux; - /* wait startup_time before verifying config changes */ - usleep_range(data->start_up_time, data->start_up_time + 100); + if (change) { + /* cycle sensor state machine to reset any measurement in progress + * configuration errors are detected in a measurment cycle. + * If the sampling frequency is too low, it is faster to reset + * measurement cycle and restart measurements + */ + ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, + BMP380_MODE_MASK, + FIELD_PREP(BMP380_MODE_MASK, + BMP380_MODE_SLEEP)); + if (ret < 0) { + dev_err(data->dev, "failed to set sleep mode\n"); + return ret; + } + usleep_range(2000, 2500); + ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL, + BMP380_MODE_MASK, + FIELD_PREP(BMP380_MODE_MASK, + BMP380_MODE_NORMAL)); + if (ret < 0) { + dev_err(data->dev, "failed to set normal mode\n"); + return ret; + } + /* wait before checking the configuration error flag. + * Worst case value for measure time indicated in the datashhet + * in section 3.9.1 is used. + */ + msleep(80); - /* check config error flag */ - ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); - if (ret < 0) { - dev_err(data->dev, - "failed to read error register\n"); - return ret; - } - if (tmp & BMP380_ERR_CONF_MASK) { - dev_warn(data->dev, - "sensor flagged configuration as incompatible\n"); - return -EINVAL; + /* check config error flag */ + ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); + if (ret < 0) { + dev_err(data->dev, + "failed to read error register\n"); + return ret; + } + if (tmp & BMP380_ERR_CONF_MASK) { + dev_warn(data->dev, + "sensor flagged configuration as incompatible\n"); + return -EINVAL; + } } return 0; } static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; +static const int bmp380_iir_filter_coeffs_avail[] = { 0, 1, 3, 7, 15, 31, 63, 127 }; static const struct bmp280_chip_info bmp380_chip_info = { .id_reg = BMP380_REG_ID, .start_up_time = 2000, + .channels = bmp380_channels, .num_channels = 2, .oversampling_temp_avail = bmp380_oversampling_avail, @@ -1046,6 +1313,14 @@ static const struct bmp280_chip_info bmp380_chip_info = { .num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail), .oversampling_press_default = ilog2(4), + .sampling_freq_avail = bmp380_odr_table, + .num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2, + .sampling_freq_default = BMP380_ODR_50HZ, + + .iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail, + .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), + .iir_filter_coeff_default = 2, + .chip_config = bmp380_chip_config, .read_temp = bmp380_read_temp, .read_press = bmp380_read_press, @@ -1282,6 +1557,7 @@ static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; static const struct bmp280_chip_info bmp180_chip_info = { .id_reg = BMP280_REG_ID, .start_up_time = 2000, + .channels = bmp280_channels, .num_channels = 2, .oversampling_temp_avail = bmp180_oversampling_temp_avail, @@ -1380,7 +1656,6 @@ int bmp280_common_probe(struct device *dev, data->dev = dev; indio_dev->name = name; - indio_dev->channels = bmp280_channels; indio_dev->info = &bmp280_info; indio_dev->modes = INDIO_DIRECT_MODE; @@ -1403,10 +1678,13 @@ int bmp280_common_probe(struct device *dev, data->chip_info = chip_info; /* apply initial values from chip info structure */ + indio_dev->channels = chip_info->channels; indio_dev->num_channels = chip_info->num_channels; data->oversampling_press = chip_info->oversampling_press_default; data->oversampling_humid = chip_info->oversampling_humid_default; data->oversampling_temp = chip_info->oversampling_temp_default; + data->iir_filter_coeff = chip_info->iir_filter_coeff_default; + data->sampling_freq = chip_info->sampling_freq_default; data->start_up_time = chip_info->start_up_time; /* Bring up regulators */ diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index fd38906c889c..1314d5059c53 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -54,24 +54,6 @@ #define BMP380_OSRS_PRESS_MASK GENMASK(2, 0) #define BMP380_ODRS_MASK GENMASK(4, 0) -#define BMP380_ODRS_200HZ 0x00 -#define BMP380_ODRS_100HZ 0x01 -#define BMP380_ODRS_50HZ 0x02 -#define BMP380_ODRS_25HZ 0x03 -#define BMP380_ODRS_12_5HZ 0x04 -#define BMP380_ODRS_6_25HZ 0x05 -#define BMP380_ODRS_3_1HZ 0x06 -#define BMP380_ODRS_1_5HZ 0x07 -#define BMP380_ODRS_0_78HZ 0x08 -#define BMP380_ODRS_0_39HZ 0x09 -#define BMP380_ODRS_0_2HZ 0x0A -#define BMP380_ODRS_0_1HZ 0x0B -#define BMP380_ODRS_0_05HZ 0x0C -#define BMP380_ODRS_0_02HZ 0x0D -#define BMP380_ODRS_0_01HZ 0x0E -#define BMP380_ODRS_0_006HZ 0x0F -#define BMP380_ODRS_0_003HZ 0x10 -#define BMP380_ODRS_0_0015HZ 0x11 #define BMP380_CTRL_SENSORS_MASK GENMASK(1, 0) #define BMP380_CTRL_SENSORS_PRESS_EN BIT(0)