From nobody Thu Nov 28 00:41:10 2024 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 376251DE4E0; Mon, 7 Oct 2024 19:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728330612; cv=none; b=pE4hE1tGxPS+ya5+MWa90Q2S64CPaciqqyvgwp2hLRa4o/z7ECMeXhESsRjruaNakPdT9RT/YSDdw7/dsz0ttIozfUg0tTsTCtC9Uc2t7fVYjSSlK/DpIJNy2MSdhSJyoSSLXr5kQ4VHNBjxSI6JcOITdTxhF8B/qYRgMz18gYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728330612; c=relaxed/simple; bh=sZyzGi1cyTzr8klwJt3qPpLxlDEsa7RmlbPZNoDLGWY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oNxh5/eebZa4JvmvcIJSJTtDo0lYlCPHLkJ6OkrvXeobQsAbjFGPYJXAIfCgKDuzn/QC2QqM8fjFddamh81L1fyX5DVn3DmsSpGEmwujRUctNGHssmLqbYt3DTCh3BRxg8GLK9r4ak8wwsjQN/PTbZQCTFrxa1aH+y4jOVsMN4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fOBSdc3N; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fOBSdc3N" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-37ccfba5df5so3208719f8f.0; Mon, 07 Oct 2024 12:50:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728330607; x=1728935407; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2acOvDBhhKlNlrzh8C1bE3Xl0WYWldRt0wAyp0DbI5U=; b=fOBSdc3NyDq9aiqPxb8gk3/0dcsOtKpzvvzNrACswfRikwDsZl9oIVUg16RWuhU5XI P/NV+QRpbrM4FfciBnWuMFqlmjyr2uXYHfE3MdreORm98fQ0I2IP+Rw6xcV80qhGQJ60 1IAHE8/5YxSTkLOX8fb1vrD9ftlDCDhAPwRJGAq6MhuiLSgGWqgzELGKQr0Yw79LwhWC xO209Ei+kWe7nvy8nMUTbVDLD2kM527acsgNFpXGsZcDajvL9mjj+rJJNbYXwWXlk85R 04rNivLrTx562ae0lO6ofHkGlOGvZS7e8InZJU5C4XtVkoxEXTEwrAxE9A9s1eKcRDhf AKCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728330607; x=1728935407; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2acOvDBhhKlNlrzh8C1bE3Xl0WYWldRt0wAyp0DbI5U=; b=AN3orCJhUZJUyckC/P5u+8ZDg6/2Z3SWA12AT2Y1MaqHDTFUgrpoE3HKsgjSNQWtYm 2kRTt3dpg+Om2+XyGn3jB3csOjaf1bhIsePXUAoR0NkeYxXR0WGdz6jOYiRc3sU1hyoJ p587GzqgYIl21CIPA4vSRXnTJ7Nd9KvXrnMc1IzhOMDV+EdaV5qU3NdsH1lCBcAYMutA Gwng69YR+NSt+1PkwouMpRPUdUEKGY7xq2MESW/XhD/YbX6Yc1xTaqxGALXgcc4f4x7i 9Ipa4PoS9YpY/Jo09waT7I9dkbOcq4Q6tLTVB1y8Ik9sfBt0YFZNspYNCo2G/7eRy8DV W13A== X-Forwarded-Encrypted: i=1; AJvYcCUh/GeFarH9D6swTO4OH7TvKrfWcjbpvFNAgDI1ntAgg9QVThV9WNME9lXPjDGvEFLVffekbqSv2ytP@vger.kernel.org, AJvYcCVWOT41yL7lmWcXsGJeIJdMzNeMyd7ERskrZrzjXCxl+EDKDPXgkBWPJPwC2ZaRKIf36SZ2nIEvS2y9@vger.kernel.org, AJvYcCXHJdcZ6qlbvntrIZN2Wv00yWqZZEE7IhrJMQ1+Gu/WR36ZXPqHJfKQvnTxqdv45xxHz5x2TIl8uU75X8ho@vger.kernel.org X-Gm-Message-State: AOJu0Yx+VeVif499AvsjF1CFedWZApzaHiR8pfkXPCki0tbqOgRrLQcH Jx+ha3ysizDhW/Va2DRHgza8m3a6bqMUsZFTxe6hPlCl8Mp3mlayIyhGfIgA X-Google-Smtp-Source: AGHT+IGdM2EhsbqRUD0/0cV+Qkozm5OOKo4AQ70YYNlw8wYxBb+B/RyPEkx58sJh0Am5vS7iX83xhw== X-Received: by 2002:a5d:43cc:0:b0:376:37e:2729 with SMTP id ffacd0b85a97d-37d0e7373f6mr8130185f8f.31.1728330607231; Mon, 07 Oct 2024 12:50:07 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:b658:eac0:bb05:9042]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42f89ed922esm84939655e9.39.2024.10.07.12.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 12:50:06 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org, lars@metafoo.de, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, andriy.shevchenko@linux.intel.com Cc: vassilisamir@gmail.com, ang.iglesiasg@gmail.com, linus.walleij@linaro.org, biju.das.jz@bp.renesas.com, javier.carrasco.cruz@gmail.com, semen.protsenko@linaro.org, 579lpy@gmail.com, ak@it-klinger.de, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, christophe.jaillet@wanadoo.fr Subject: [PATCH v8 3/4] iio: pressure: bmp280: Add data ready trigger support Date: Mon, 7 Oct 2024 21:49:44 +0200 Message-Id: <20241007194945.66192-4-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241007194945.66192-1-vassilisamir@gmail.com> References: <20241007194945.66192-1-vassilisamir@gmail.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" The BMP3xx and BMP5xx sensors have an interrupt pin which can be used as a trigger for when there are data ready in the sensor for pick up. This use case is used along with NORMAL_MODE in the sensor, which allows the sensor to do consecutive measurements depending on the ODR rate value. The trigger pin can be configured to be open-drain or push-pull and either rising or falling edge. No support is added yet for interrupts for FIFO, WATERMARK and out of range values. Reviewed-by: Andy Shevchenko Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/bmp280-core.c | 225 ++++++++++++++++++++++++++++- drivers/iio/pressure/bmp280.h | 21 +++ 2 files changed, 244 insertions(+), 2 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp2= 80-core.c index 9ad29cf4c2ac..5779e1977a7a 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -42,12 +42,14 @@ #include #include #include +#include #include #include #include =20 #include #include +#include #include #include =20 @@ -1284,6 +1286,63 @@ static irqreturn_t bme280_trigger_handler(int irq, v= oid *p) return IRQ_HANDLED; } =20 +static int __bmp280_trigger_probe(struct iio_dev *indio_dev, + const struct iio_trigger_ops *trigger_ops, + int (*int_pin_config)(struct bmp280_data *data), + irq_handler_t irq_thread_handler) +{ + struct bmp280_data *data =3D iio_priv(indio_dev); + struct device *dev =3D data->dev; + u32 irq_type; + int ret, irq; + + irq =3D fwnode_irq_get(dev_fwnode(dev), 0); + if (irq < 0) + return dev_err_probe(dev, irq, "No interrupt found.\n"); + + irq_type =3D irq_get_trigger_type(irq); + switch (irq_type) { + case IRQF_TRIGGER_RISING: + data->trig_active_high =3D true; + break; + case IRQF_TRIGGER_FALLING: + data->trig_active_high =3D false; + break; + default: + return dev_err_probe(dev, -EINVAL, "Invalid interrupt type specified.\n"= ); + } + + data->trig_open_drain =3D + fwnode_property_read_bool(dev_fwnode(dev), "int-open-drain"); + + ret =3D int_pin_config(data); + if (ret) + return ret; + + data->trig =3D devm_iio_trigger_alloc(data->dev, "%s-dev%d", + indio_dev->name, + iio_device_id(indio_dev)); + if (!data->trig) + return -ENOMEM; + + data->trig->ops =3D trigger_ops; + iio_trigger_set_drvdata(data->trig, data); + + ret =3D devm_request_threaded_irq(data->dev, irq, NULL, + irq_thread_handler, IRQF_ONESHOT, + indio_dev->name, indio_dev); + if (ret) + return dev_err_probe(dev, ret, "request IRQ failed.\n"); + + ret =3D devm_iio_trigger_register(data->dev, data->trig); + if (ret) + return dev_err_probe(dev, ret, "iio trigger register failed.\n"); + + indio_dev->trig =3D iio_trigger_get(data->trig); + + return 0; +} + static const u8 bme280_chip_ids[] =3D { BME280_CHIP_ID }; static const int bme280_humid_coeffs[] =3D { 1000, 1024 }; =20 @@ -1785,6 +1844,81 @@ static int bmp380_chip_config(struct bmp280_data *da= ta) return ret; } =20 +static void bmp380_trigger_reenable(struct iio_trigger *trig) +{ + struct bmp280_data *data =3D iio_trigger_get_drvdata(trig); + unsigned int tmp; + int ret; + + ret =3D regmap_read(data->regmap, BMP380_REG_INT_STATUS, &tmp); + if (ret) + dev_err(data->dev, "Failed to reset interrupt.\n"); +} + +static int bmp380_data_rdy_trigger_set_state(struct iio_trigger *trig, + bool state) +{ + struct bmp280_data *data =3D iio_trigger_get_drvdata(trig); + int ret; + + guard(mutex)(&data->lock); + + ret =3D regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL, + BMP380_INT_CTRL_DRDY_EN, + FIELD_PREP(BMP380_INT_CTRL_DRDY_EN, !!state)); + if (ret) + dev_err(data->dev, + "Could not %s interrupt.\n", str_enable_disable(state)); + return ret; +} + +static const struct iio_trigger_ops bmp380_trigger_ops =3D { + .set_trigger_state =3D &bmp380_data_rdy_trigger_set_state, + .reenable =3D &bmp380_trigger_reenable, +}; + +static int bmp380_int_pin_config(struct bmp280_data *data) +{ + int pin_drive_cfg =3D FIELD_PREP(BMP380_INT_CTRL_OPEN_DRAIN, + data->trig_open_drain); + int pin_level_cfg =3D FIELD_PREP(BMP380_INT_CTRL_LEVEL, + data->trig_active_high); + int ret, int_pin_cfg =3D pin_drive_cfg | pin_level_cfg; + + ret =3D regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL, + BMP380_INT_CTRL_SETTINGS_MASK, int_pin_cfg); + if (ret) + dev_err(data->dev, "Could not set interrupt settings.\n"); + + return ret; +} + +static irqreturn_t bmp380_irq_thread_handler(int irq, void *p) +{ + struct iio_dev *indio_dev =3D p; + struct bmp280_data *data =3D iio_priv(indio_dev); + unsigned int int_ctrl; + int ret; + + scoped_guard(mutex, &data->lock) { + ret =3D regmap_read(data->regmap, BMP380_REG_INT_STATUS, &int_ctrl); + if (ret) + return IRQ_NONE; + } + + if (FIELD_GET(BMP380_INT_STATUS_DRDY, int_ctrl)) + iio_trigger_poll_nested(data->trig); + + return IRQ_HANDLED; +} + +static int bmp380_trigger_probe(struct iio_dev *indio_dev) +{ + return __bmp280_trigger_probe(indio_dev, &bmp380_trigger_ops, + bmp380_int_pin_config, + bmp380_irq_thread_handler); +} + static irqreturn_t bmp380_trigger_handler(int irq, void *p) { struct iio_poll_func *pf =3D p; @@ -1882,6 +2016,7 @@ const struct bmp280_chip_info bmp380_chip_info =3D { .wait_conv =3D bmp380_wait_conv, .preinit =3D bmp380_preinit, =20 + .trigger_probe =3D bmp380_trigger_probe, .trigger_handler =3D bmp380_trigger_handler, }; EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280); @@ -2429,6 +2564,88 @@ static int bmp580_chip_config(struct bmp280_data *da= ta) return 0; } =20 +static void bmp580_trigger_reenable(struct iio_trigger *trig) +{ + struct bmp280_data *data =3D iio_trigger_get_drvdata(trig); + unsigned int tmp; + int ret; + + ret =3D regmap_read(data->regmap, BMP580_REG_INT_STATUS, &tmp); + if (ret) + dev_err(data->dev, "Failed to reset interrupt.\n"); +} + +static int bmp580_data_rdy_trigger_set_state(struct iio_trigger *trig, + bool state) +{ + struct bmp280_data *data =3D iio_trigger_get_drvdata(trig); + int ret; + + guard(mutex)(&data->lock); + + ret =3D regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG, + BMP580_INT_CONFIG_INT_EN, + FIELD_PREP(BMP580_INT_CONFIG_INT_EN, !!state)); + if (ret) + dev_err(data->dev, + "Could not %s interrupt.\n", str_enable_disable(state)); + return ret; +} + +static const struct iio_trigger_ops bmp580_trigger_ops =3D { + .set_trigger_state =3D &bmp580_data_rdy_trigger_set_state, + .reenable =3D &bmp580_trigger_reenable, +}; + +static int bmp580_int_pin_config(struct bmp280_data *data) +{ + int pin_drive_cfg =3D FIELD_PREP(BMP580_INT_CONFIG_OPEN_DRAIN, + data->trig_open_drain); + int pin_level_cfg =3D FIELD_PREP(BMP580_INT_CONFIG_LEVEL, + data->trig_active_high); + int ret, int_pin_cfg =3D pin_drive_cfg | pin_level_cfg; + + ret =3D regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG, + BMP580_INT_CONFIG_MASK, int_pin_cfg); + if (ret) { + dev_err(data->dev, "Could not set interrupt settings.\n"); + return ret; + } + + ret =3D regmap_set_bits(data->regmap, BMP580_REG_INT_SOURCE, + BMP580_INT_SOURCE_DRDY); + if (ret) + dev_err(data->dev, "Could not set interrupt source.\n"); + + return ret; +} + +static irqreturn_t bmp580_irq_thread_handler(int irq, void *p) +{ + struct iio_dev *indio_dev =3D p; + struct bmp280_data *data =3D iio_priv(indio_dev); + unsigned int int_ctrl; + int ret; + + scoped_guard(mutex, &data->lock) { + ret =3D regmap_read(data->regmap, BMP580_REG_INT_STATUS, &int_ctrl); + if (ret) + return IRQ_NONE; + } + + if (FIELD_GET(BMP580_INT_STATUS_DRDY_MASK, int_ctrl)) + iio_trigger_poll_nested(data->trig); + + return IRQ_HANDLED; +} + +static int bmp580_trigger_probe(struct iio_dev *indio_dev) +{ + return __bmp280_trigger_probe(indio_dev, &bmp580_trigger_ops, + bmp580_int_pin_config, + bmp580_irq_thread_handler); +} + static irqreturn_t bmp580_trigger_handler(int irq, void *p) { struct iio_poll_func *pf =3D p; @@ -2507,6 +2724,7 @@ const struct bmp280_chip_info bmp580_chip_info =3D { .wait_conv =3D bmp580_wait_conv, .preinit =3D bmp580_preinit, =20 + .trigger_probe =3D bmp580_trigger_probe, .trigger_handler =3D bmp580_trigger_handler, }; EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280); @@ -3055,8 +3273,11 @@ int bmp280_common_probe(struct device *dev, * however as it happens, the BMP085 shares the chip ID of BMP180 * so we look for an IRQ if we have that. */ - if (irq > 0 && (chip_id =3D=3D BMP180_CHIP_ID)) { - ret =3D bmp085_fetch_eoc_irq(dev, name, irq, data); + if (irq > 0) { + if (chip_id =3D=3D BMP180_CHIP_ID) + ret =3D bmp085_fetch_eoc_irq(dev, name, irq, data); + if (data->chip_info->trigger_probe) + ret =3D data->chip_info->trigger_probe(indio_dev); if (ret) return ret; } diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 3babf90ce73c..12f6e90b3728 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -55,8 +55,17 @@ #define BMP580_CMD_NVM_WRITE_SEQ_1 0xA0 #define BMP580_CMD_SOFT_RESET 0xB6 =20 +#define BMP580_INT_STATUS_DRDY_MASK BIT(0) #define BMP580_INT_STATUS_POR_MASK BIT(4) =20 +#define BMP580_INT_SOURCE_DRDY BIT(0) + +#define BMP580_INT_CONFIG_MASK GENMASK(3, 0) +#define BMP580_INT_CONFIG_LATCH BIT(0) +#define BMP580_INT_CONFIG_LEVEL BIT(1) +#define BMP580_INT_CONFIG_OPEN_DRAIN BIT(2) +#define BMP580_INT_CONFIG_INT_EN BIT(3) + #define BMP580_STATUS_CORE_RDY_MASK BIT(0) #define BMP580_STATUS_NVM_RDY_MASK BIT(1) #define BMP580_STATUS_NVM_ERR_MASK BIT(2) @@ -175,6 +184,14 @@ #define BMP380_TEMP_MEAS_OFFSET 163 #define BMP380_PRESS_MEAS_OFFSET 392 =20 +#define BMP380_INT_STATUS_DRDY BIT(3) + +#define BMP380_INT_CTRL_SETTINGS_MASK GENMASK(2, 0) +#define BMP380_INT_CTRL_OPEN_DRAIN BIT(0) +#define BMP380_INT_CTRL_LEVEL BIT(1) +#define BMP380_INT_CTRL_LATCH BIT(2) +#define BMP380_INT_CTRL_DRDY_EN BIT(6) + #define BMP380_MIN_TEMP -4000 #define BMP380_MAX_TEMP 8500 #define BMP380_MIN_PRES 3000000 @@ -407,6 +424,9 @@ struct bmp280_data { struct regmap *regmap; struct completion done; bool use_eoc; + bool trig_open_drain; + bool trig_active_high; + struct iio_trigger *trig; const struct bmp280_chip_info *chip_info; union { struct bmp180_calib bmp180; @@ -509,6 +529,7 @@ struct bmp280_chip_info { int (*set_mode)(struct bmp280_data *data, enum bmp280_op_mode mode); int (*wait_conv)(struct bmp280_data *data); =20 + int (*trigger_probe)(struct iio_dev *indio_dev); irqreturn_t (*trigger_handler)(int irq, void *p); }; =20 --=20 2.25.1