From patchwork Tue Mar 19 00:29:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596051 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.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 618A7368; Tue, 19 Mar 2024 00:29:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808178; cv=none; b=MvfuEhA5tAsry4LgFWgB5447JorRv/b3rOffywV4G/eCNVSYmLSt75c7EjkfIk0JJGUt5CAmKfghLlr3u6o40JsEA/iq7R5c5EPXWr7VTqO7No+4TnOFO6FemqzwpWqHaywZWICn55jO3r9rzQYecOdHaX5Z8FBclN+I57McATQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808178; c=relaxed/simple; bh=6Ouxyk1MFE8oPL5EtYMsLRqOrNWjyubaatGPHGKRHrs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gHnuEE8EuhRJfUaLIjtfwIWkSuyl+Ml5HsXOYJZW33ICCPgoYKsNWX9N+2BeHGX8vtys85j5JDpkNtcKqdQEAdb8LvwXss1/9VZ5nuM7UQETxC4FXNXG/5kC4b6GesFf3SMtNrvfHQoIZCIOp/OrUWZy2qZfK8xdh6fUTwQj0D0= 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=MpnpzWOy; arc=none smtp.client-ip=209.85.218.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="MpnpzWOy" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a465ddc2c09so341981666b.2; Mon, 18 Mar 2024 17:29:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808175; x=1711412975; 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=6laUTApqd01ANpCuz0jrMfAwGjaFU1xJS3mFm+uw6r0=; b=MpnpzWOyh9LrXoELJrwGZQDn7WunxM1jXoDxLOHQ1ypxRN2CiHOurDzruJoHWpzlqx SHovbWwmQ0ik4sh1CncbtMLB7Uae/2jDCStau0MNbv6dlu57y+Lr7QPspEJ1k+3/6iMu rpQvaGVXYe67UgLkBPgHNSv+KjyMqHO6y7LT6t8p5Fz6V/WiRinBasrMeAxjovkElVjP og5taLeNYzGOgLxEOzDCpFnTqaW8WWZMBPdQpcq882Wag8omI137edJcvLVZoTeZxCpA sqcGtqtj43v4r9DdkN74B8He/GAuyLdn6P+t5lABKe8jBWdoxOMpz/jKjvtQJ8CH5rRx fD/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808175; x=1711412975; 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=6laUTApqd01ANpCuz0jrMfAwGjaFU1xJS3mFm+uw6r0=; b=mTvp85T7CTzk1eI1M2We96We9fmMAaIkCGUakpLFt3xfQTdXvQktjUWjpZIVdls46k 2m2zQuRc9uNGr2P9UIYPkbYmpGpv5pYZJVcZ9XTkT6ckiyIg/InrWwrQqypXfaexx2fH PRHl/fgkgtOAP8g9x70Wn/h2QhB54KZ3rZIlBkLSHTjgIo/8B1262YcglJLY8qW6bN5W MeOR3R+zE62buLDx26SBIMHSu8sPWHn8360heLfFB1yfpHyj52fcqJSYnfUIbc+usY9n h0CQ2NhLRbG+9Y0FHUlzqSe+XsF0XA8jIGAag7uqZmJfyqUT3+/ceWqNjZJmdT+zMguZ QW7Q== X-Forwarded-Encrypted: i=1; AJvYcCXwNQnplkmHtIFohDDlkTD8agdy6k+sh/akXG5GUh8F4iMQbwY3p30KGRQLmjl1Hf2fQ0jyIhF1OgT34RSBoGNkszOIfI1QBppOrv4vut8AzGkuuku/A2rXYlbILn0Lbj6OYNRM0KIw X-Gm-Message-State: AOJu0YxBbs/XR51DqhxnIk74H/Tg8flEzbdw/l+b55RQ9UO4NL0qilVJ U0lZa+hlEttNMorQcoDkWanixXvFFRvZWnCUXmPyErMDegPTYw2tw/EKp5eFUYWicug0 X-Google-Smtp-Source: AGHT+IEngGGUNsKhXEFBVV+MUi3yyf+vCtFDkIwVlSbHJ/mp+l7HrM2+CHFIE8wUj93ibSoJjoIGpQ== X-Received: by 2002:a05:6402:4146:b0:568:abe3:52b2 with SMTP id x6-20020a056402414600b00568abe352b2mr8218087eda.23.1710808174727; Mon, 18 Mar 2024 17:29:34 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:34 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis Subject: [PATCH v3 1/6] iio: pressure: BMP280 core driver headers sorting Date: Tue, 19 Mar 2024 01:29:20 +0100 Message-Id: <20240319002925.2121016-2-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Sort headers in alphabetical order. Signed-off-by: Vasileios Amoiridis Reviewed-by: Andy Shevchenko --- drivers/iio/pressure/bmp280-core.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index fe8734468ed3..871b2214121b 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -27,20 +27,20 @@ #include #include -#include -#include -#include -#include +#include #include -#include -#include +#include #include -#include #include #include /* For irq_get_irq_data() */ -#include +#include +#include #include #include +#include +#include + +#include #include From patchwork Tue Mar 19 00:29:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596052 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 A5F2B816; Tue, 19 Mar 2024 00:29:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808179; cv=none; b=fN9BR9DSDdJgCRY08T6UtLIXL4zxaE7MRus30C5i9ujfKGVAWksFWQwlzyKcUTLFbMtxhGzRq/BawxMUC/9elogtz/BT4k1UNOEC1iMd3dDjOXhyPEQkHDfRbZrdxsVFCWHO0qyOj2+A3cQjieA1vVYXvLR9tL36sjmSOs4ZEcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808179; c=relaxed/simple; bh=GDXxRCQUXzRN1ga8hPMGsDuymxwy19feZsEsXaV5plU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OXtk+nZ9D1bxerwbua2lKhRSvXZfxYjekwEyRuE45C0zkKNoz/24mqE1e0kiw6Q3wA2i09wq7I7v19UO8LzKge6cSHlGZ8UEKEgoLRH6zQfUgcNBGfc4cUckdBm8FoE37czPCZT0UDES0LwlTWbJ8BZnQitJonhsHasZpzxmQHQ= 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=d2+hrSPU; arc=none smtp.client-ip=209.85.208.41 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="d2+hrSPU" Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-56b857bac38so515258a12.0; Mon, 18 Mar 2024 17:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808176; x=1711412976; 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=5bcqw3pjUKDeXJIPJrkT7cyeEObF+uyQDSXsN/wtA9Y=; b=d2+hrSPU8LllxBC9r2RJOmHrGrrYIZ/wuU8Yq3sVjxoJ3/1EpwfWgIngJBYM4A+qI9 LVYhCAEe9v8bKZo2+V4hPyH0CxBifj0Q6rTRplZtPG6Qa7gzxJt6flKTHgBXlgZW6naN YwgF9Ueo9K+gPln/FImJ1n9J9ho2vNN2Ka4qK6YA5s7+Izun0rJmqB/0d8MfA1TawvOv 11IeNqDh2liyvEQG+5VJHDtpl646o2BvzTZRrODlu8G0aIncmTVr3dpTcEAhjeZahP0H 9XPLuKbUhY355NCGm1Z8kzhFsvQhxcoVDmHeoy7ictv/V84M9aN+c9cqIRWEvMWAkjak QEYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808176; x=1711412976; 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=5bcqw3pjUKDeXJIPJrkT7cyeEObF+uyQDSXsN/wtA9Y=; b=AMZSZqA0IHslF8hjSf17VMZGjQlqqmbcjLE66fuGw+4spZ/AG0IHenNBTdQDTbvbzz jGSwjcnQtNmogvLzZ9sG+3Ikje2AkeJDz2xeAFMm3FtnMLmj8tKVlewc0nbPrZV9h+ij W2hxlkRMqREhb9DdngSjr3eLL0gfbebTj5ngxLdzAPGTRGwKBpSIW/EJc+k9NKcSHZQc aJX+x/plxO0PMf4VKJOMRVGImWm6jZ2oPVqkiinvz12SxA6J3GdgSqyqGQd+ay+glGuT NOz7VCSJOAi5zXab+wZmlwJL8ltAga0NDWy5Z2Lvyo3iTPph/L3XX7uc3B3DQLfL4LVS MBoA== X-Forwarded-Encrypted: i=1; AJvYcCXcILvDpPna2VHmE3T6UTgyzL6nSDxwf+1ON5yZlLGy16vhWTnPpeaJ/6oJrJQTE5PMQ9GScBF71HT+dObRr+AxxmHQkajQjEvKK3gxjU22+Z+oK/qT+nl9Z/lJdcHOkLzWoxmbSmDI X-Gm-Message-State: AOJu0YzabsCZiF/swRYI10Ye6KwwQswfi2zRpej5V+5gHYfhHeffcCDr ntbLvdMl8uqAozVJvhJnu8cFjxmY1VP71arnZt8JEzHeP732Qw74 X-Google-Smtp-Source: AGHT+IHM8MuacX6G3Ak+84rJE51NTny1+mKzSzluC385ybhxP1KDFoKBQBOsW+2y1yZLRIZj28vp+g== X-Received: by 2002:a05:6402:1d34:b0:568:29f8:1d79 with SMTP id dh20-20020a0564021d3400b0056829f81d79mr5691526edb.38.1710808175734; Mon, 18 Mar 2024 17:29:35 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:35 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis , Jonathan Cameron Subject: [PATCH v3 2/6] iio: pressure: Introduce new cleanup routines to BMP280 driver *_raw() functions Date: Tue, 19 Mar 2024 01:29:21 +0100 Message-Id: <20240319002925.2121016-3-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce the new linux/cleanup.h with the guard(mutex) functionality in the {read/write}_raw() functions Suggested-by: Andy Shevchenko Suggested-by: Jonathan Cameron Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/bmp280-core.c | 125 +++++++++++++---------------- 1 file changed, 58 insertions(+), 67 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index 871b2214121b..f7a13ff6f26c 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -460,77 +460,74 @@ static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) return IIO_VAL_INT; } -static int bmp280_read_raw(struct iio_dev *indio_dev, - struct iio_chan_spec const *chan, - int *val, int *val2, long mask) +static int bmp280_read_raw_guarded(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) { struct bmp280_data *data = iio_priv(indio_dev); - int ret; - pm_runtime_get_sync(data->dev); - mutex_lock(&data->lock); + guard(mutex)(&data->lock); switch (mask) { case IIO_CHAN_INFO_PROCESSED: switch (chan->type) { case IIO_HUMIDITYRELATIVE: - ret = data->chip_info->read_humid(data, val, val2); - break; + return data->chip_info->read_humid(data, val, val2); case IIO_PRESSURE: - ret = data->chip_info->read_press(data, val, val2); - break; + return data->chip_info->read_press(data, val, val2); case IIO_TEMP: - ret = data->chip_info->read_temp(data, val, val2); - break; + return data->chip_info->read_temp(data, val, val2); default: - ret = -EINVAL; - break; + return -EINVAL; } - break; + return 0; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: switch (chan->type) { case IIO_HUMIDITYRELATIVE: *val = 1 << data->oversampling_humid; - ret = IIO_VAL_INT; - break; + return IIO_VAL_INT; case IIO_PRESSURE: *val = 1 << data->oversampling_press; - ret = IIO_VAL_INT; - break; + return IIO_VAL_INT; case IIO_TEMP: *val = 1 << data->oversampling_temp; - ret = IIO_VAL_INT; - break; + return IIO_VAL_INT; default: - ret = -EINVAL; - break; + return -EINVAL; } - break; + return 0; case IIO_CHAN_INFO_SAMP_FREQ: if (!data->chip_info->sampling_freq_avail) { - ret = -EINVAL; - break; + return -EINVAL; } *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; + return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: if (!data->chip_info->iir_filter_coeffs_avail) { - ret = -EINVAL; - break; + return -EINVAL; } *val = (1 << data->iir_filter_coeff) - 1; - ret = IIO_VAL_INT; - break; + return IIO_VAL_INT; default: - ret = -EINVAL; - break; + return -EINVAL; } - mutex_unlock(&data->lock); + return 0; +} + + +static int bmp280_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct bmp280_data *data = iio_priv(indio_dev); + int ret; + + pm_runtime_get_sync(data->dev); + ret = bmp280_read_raw_guarded(indio_dev, chan, val, val2, mask); pm_runtime_mark_last_busy(data->dev); pm_runtime_put_autosuspend(data->dev); @@ -662,13 +659,13 @@ static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val) return -EINVAL; } -static int bmp280_write_raw(struct iio_dev *indio_dev, - struct iio_chan_spec const *chan, - int val, int val2, long mask) +static int bmp280_write_raw_guarded(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) { struct bmp280_data *data = iio_priv(indio_dev); - int ret = 0; + guard(mutex)(&data->lock); /* * Helper functions to update sensor running configuration. * If an error happens applying new settings, will try restore @@ -677,46 +674,40 @@ static int bmp280_write_raw(struct iio_dev *indio_dev, */ switch (mask) { case IIO_CHAN_INFO_OVERSAMPLING_RATIO: - pm_runtime_get_sync(data->dev); - mutex_lock(&data->lock); switch (chan->type) { case IIO_HUMIDITYRELATIVE: - ret = bmp280_write_oversampling_ratio_humid(data, val); - break; + return bmp280_write_oversampling_ratio_humid(data, val); case IIO_PRESSURE: - ret = bmp280_write_oversampling_ratio_press(data, val); - break; + return bmp280_write_oversampling_ratio_press(data, val); case IIO_TEMP: - ret = bmp280_write_oversampling_ratio_temp(data, val); - break; + return bmp280_write_oversampling_ratio_temp(data, val); default: - ret = -EINVAL; - break; + return -EINVAL; } - mutex_unlock(&data->lock); - pm_runtime_mark_last_busy(data->dev); - pm_runtime_put_autosuspend(data->dev); - break; + return 0; 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; + return bmp280_write_sampling_frequency(data, val, val2); 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; + return bmp280_write_iir_filter_coeffs(data, val); default: return -EINVAL; } + return 0; +} + +static int bmp280_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct bmp280_data *data = iio_priv(indio_dev); + int ret = 0; + + pm_runtime_get_sync(data->dev); + ret = bmp280_write_raw_guarded(indio_dev, chan, val, val2, mask); + pm_runtime_mark_last_busy(data->dev); + pm_runtime_put_autosuspend(data->dev); + return ret; } From patchwork Tue Mar 19 00:29:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596053 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 DEF231C3E; Tue, 19 Mar 2024 00:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808181; cv=none; b=iCCYC2Knk1+DI0KxBkPQNMPlyfpuaP9jJS1dsAr7jUZeeL+EQQs2iLJrUkCo9pJUkGn8EJ1LGHXQsBIMootUd3BQpPlBusH4hFljwKSCh6bfvHH/EPmASLMGnEfT/SuznHO+GC8UVakaJT+0qFlV2R1bhtZarYJnav7wwMotONg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808181; c=relaxed/simple; bh=SMjamXsghAruaajF6BqKqPaV4kuSE+ultexCBLkmd4Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cd0HOeJuzU0boXSYvFXiVhVhhb589JxkOHfh41mFqAq3EInS2ZcZdcJpqGwoPH6MazxPko7NGNZ2ksXzfWW/u9HtCFMQxZbHc6UjbDC7KgrE4o2+kHV4phdwjhhGPpQCRJ8wkzmb6p3seY41+GD1944VRGdBqF66SoXx5rsJf+g= 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=SaarPMbT; arc=none smtp.client-ip=209.85.208.54 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="SaarPMbT" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-56899d9bf52so6434523a12.2; Mon, 18 Mar 2024 17:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808177; x=1711412977; 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=hMzVP/ZU7ElAGknHYusFFOwN83HKOtk44T/24Gy6kBs=; b=SaarPMbTj+zIpvInorMHwQY4t9qiJhj63A6Xza4B3L296AsiXF7kUuNDEL+hki/x1Z xQzHDQZmZ5m7QRnG0U0uWSLUzvwsdx5dtZfTPq3z7dCkxoI2wfQjzfA6qvrTTTDXzr1v DhGlQ+JDVj4He+laAdVFwbsZQ66gpK+EpYVagBuq8iQftAZ9wdnNL+C+3ZzALUhSZjp3 jlnvcwiPTZJjIaXxTTG8On5qVHe2bjfrwPM/WN9kVIAt5e/TVrvzGQL902H/JD06f8+A hzdXhM1egW2VA8VNZUU2aPfQwlwjVyYx2K8y4QlEKrSEM3IrgRjzQJwlkUdU4An1wN4e T/rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808177; x=1711412977; 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=hMzVP/ZU7ElAGknHYusFFOwN83HKOtk44T/24Gy6kBs=; b=isYIQM/RCOYc6Jr4BXPRGp3OJgqgtF+bpFfpMGvm/UOJbnauGB/RPloOYTtgRcYDNG 9r8zVN28n6BF7LxYv0CDOblvkMjawXjjzU25wwXq7quscrinmPPQD28yAd+tnrVRGIuj IBG1gOY1jLg0mFw3QD4pPnb7LGMFCjanosCZuxeFxQnQLxC5b0vTcZsEqWfGkvauIN6J iY7myB9H3Ye1/5ph+/lZr07STB6/6vLgn/jPWCj/szJD6VZ3lX9GBfFQs7jpmoE4Gge/ wY10CG7KMT+p3FqE/L6QoIIPTH6ch6QAF8NC0wkf5IxtCAYoOQGjAsJs+dnJo/gkZBU2 nC9g== X-Forwarded-Encrypted: i=1; AJvYcCUAxPP15uyagaIwTXo5WF45KHEqxYZ/VUOlKJq/BOn/1iaPEESKdUZqZGvdYjAH2T5bAtI4mOokspaZcof/b0z7LZVV4tmbBv9ybwjZVl3Adc47Fl1XBJ4dDOeZiy8WnTmo7gWcCowX X-Gm-Message-State: AOJu0YzPV5eWAq2yVgbHdGgZvZZpQMXoaXZM+zOhxQ91e+psMlnIQ6qv EyeNnkxbGIlhpmv3skPSf+J34kHITqXyQtyLCLwYSKq3IqJP0aWC X-Google-Smtp-Source: AGHT+IF1wVCIj9x9kfGmOlbhoujL4cDxeeZCo9Gnx1Mq3n80p5S5rQeU0bSMC8a0ATF/jc1UnTUt/Q== X-Received: by 2002:a05:6402:4581:b0:56b:7f52:d56a with SMTP id ig1-20020a056402458100b0056b7f52d56amr661735edb.7.1710808176993; Mon, 18 Mar 2024 17:29:36 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:36 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis Subject: [PATCH v3 3/6] iio: pressure: Generalize read_{temp/press/humid}() functions Date: Tue, 19 Mar 2024 01:29:22 +0100 Message-Id: <20240319002925.2121016-4-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add the coefficients for the IIO standard units and the return IIO value inside the chip_info structure. Remove the calculations with the coefficients for the IIO unit compatibility from inside the read_{temp/press/humid}() functions and move it to the general read_raw() function. Execute the calculations with the coefficients inside the read_raw() oneshot capture function. In this way, all the data for the calculation of the value are located in the chip_info structure of the respective sensor. Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/bmp280-core.c | 189 +++++++++++++++-------------- drivers/iio/pressure/bmp280.h | 13 +- 2 files changed, 106 insertions(+), 96 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index f7a13ff6f26c..6d6173c4b744 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -363,10 +363,9 @@ static u32 bmp280_compensate_press(struct bmp280_data *data, return (u32)p; } -static int bmp280_read_temp(struct bmp280_data *data, - int *val, int *val2) +static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp) { - s32 adc_temp, comp_temp; + s32 adc_temp; int ret; ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, @@ -382,29 +381,20 @@ static int bmp280_read_temp(struct bmp280_data *data, dev_err(data->dev, "reading temperature skipped\n"); return -EIO; } - comp_temp = bmp280_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) { - *val = comp_temp * 10; - return IIO_VAL_INT; - } + if (comp_temp) + *comp_temp = bmp280_compensate_temp(data, adc_temp); return 0; } -static int bmp280_read_press(struct bmp280_data *data, - int *val, int *val2) +static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press) { - u32 comp_press; s32 adc_press; int ret; /* Read and compensate temperature so we get a reading of t_fine. */ - ret = bmp280_read_temp(data, NULL, NULL); + ret = bmp280_read_temp(data, NULL); if (ret < 0) return ret; @@ -421,22 +411,19 @@ static int bmp280_read_press(struct bmp280_data *data, dev_err(data->dev, "reading pressure skipped\n"); return -EIO; } - comp_press = bmp280_compensate_press(data, adc_press); - *val = comp_press; - *val2 = 256000; + *comp_press = bmp280_compensate_press(data, adc_press); - return IIO_VAL_FRACTIONAL; + return 0; } -static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) +static int bmp280_read_humid(struct bmp280_data *data, u32 *comp_humidity) { - u32 comp_humidity; s32 adc_humidity; int ret; /* Read and compensate temperature so we get a reading of t_fine. */ - ret = bmp280_read_temp(data, NULL, NULL); + ret = bmp280_read_temp(data, NULL); if (ret < 0) return ret; @@ -453,11 +440,10 @@ static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2) dev_err(data->dev, "reading humidity skipped\n"); return -EIO; } - comp_humidity = bmp280_compensate_humidity(data, adc_humidity); - *val = comp_humidity * 1000 / 1024; + *comp_humidity = bmp280_compensate_humidity(data, adc_humidity); - return IIO_VAL_INT; + return 0; } static int bmp280_read_raw_guarded(struct iio_dev *indio_dev, @@ -465,6 +451,8 @@ static int bmp280_read_raw_guarded(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct bmp280_data *data = iio_priv(indio_dev); + int chan_value; + int ret; guard(mutex)(&data->lock); @@ -472,11 +460,29 @@ static int bmp280_read_raw_guarded(struct iio_dev *indio_dev, case IIO_CHAN_INFO_PROCESSED: switch (chan->type) { case IIO_HUMIDITYRELATIVE: - return data->chip_info->read_humid(data, val, val2); + ret = data->chip_info->read_humid(data, &chan_value); + if (ret) + return ret; + + *val = data->chip_info->humid_coeffs[0] * chan_value; + *val2 = data->chip_info->humid_coeffs[1]; + return data->chip_info->humid_coeffs_type; case IIO_PRESSURE: - return data->chip_info->read_press(data, val, val2); + ret = data->chip_info->read_press(data, &chan_value); + if (ret) + return ret; + + *val = data->chip_info->press_coeffs[0] * chan_value; + *val2 = data->chip_info->press_coeffs[1]; + return data->chip_info->press_coeffs_type; case IIO_TEMP: - return data->chip_info->read_temp(data, val, val2); + ret = data->chip_info->read_temp(data, &chan_value); + if (ret) + return ret; + + *val = data->chip_info->temp_coeffs[0] * chan_value; + *val2 = data->chip_info->temp_coeffs[1]; + return data->chip_info->temp_coeffs_type; default: return -EINVAL; } @@ -787,6 +793,8 @@ static int bmp280_chip_config(struct bmp280_data *data) static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID }; +static const int bmp280_temp_coeffs[] = { 10, 1 }; +static const int bmp280_press_coeffs[] = { 1, 256000 }; const struct bmp280_chip_info bmp280_chip_info = { .id_reg = BMP280_REG_ID, @@ -815,6 +823,11 @@ const struct bmp280_chip_info bmp280_chip_info = { .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, + .temp_coeffs = bmp280_temp_coeffs, + .temp_coeffs_type = IIO_VAL_FRACTIONAL, + .press_coeffs = bmp280_press_coeffs, + .press_coeffs_type = IIO_VAL_FRACTIONAL, + .chip_config = bmp280_chip_config, .read_temp = bmp280_read_temp, .read_press = bmp280_read_press, @@ -841,6 +854,7 @@ static int bme280_chip_config(struct bmp280_data *data) } static const u8 bme280_chip_ids[] = { BME280_CHIP_ID }; +static const int bme280_humid_coeffs[] = { 1000, 1024 }; const struct bmp280_chip_info bme280_chip_info = { .id_reg = BMP280_REG_ID, @@ -863,6 +877,14 @@ const struct bmp280_chip_info bme280_chip_info = { .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), .oversampling_humid_default = BMP280_OSRS_HUMIDITY_16X - 1, + .temp_coeffs = bmp280_temp_coeffs, + .temp_coeffs_type = IIO_VAL_FRACTIONAL, + .press_coeffs = bmp280_press_coeffs, + .press_coeffs_type = IIO_VAL_FRACTIONAL, + .humid_coeffs = bme280_humid_coeffs, + .humid_coeffs_type = IIO_VAL_FRACTIONAL, + + .chip_config = bme280_chip_config, .read_temp = bmp280_read_temp, .read_press = bmp280_read_press, @@ -988,9 +1010,8 @@ static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press) return comp_press; } -static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2) +static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp) { - s32 comp_temp; u32 adc_temp; int ret; @@ -1006,29 +1027,20 @@ static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2) 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 milli Celsius */ - *val = comp_temp * 10; - return IIO_VAL_INT; - } + if (comp_temp) + *comp_temp = bmp380_compensate_temp(data, adc_temp); return 0; } -static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) +static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press) { - s32 comp_press; u32 adc_press; int ret; /* Read and compensate for temperature so we get a reading of t_fine */ - ret = bmp380_read_temp(data, NULL, NULL); + ret = bmp380_read_temp(data, NULL); if (ret) return ret; @@ -1044,13 +1056,10 @@ static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) 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; + *comp_press = bmp380_compensate_press(data, adc_press); - return IIO_VAL_FRACTIONAL; + return 0; } static int bmp380_read_calib(struct bmp280_data *data) @@ -1218,6 +1227,8 @@ static int bmp380_chip_config(struct bmp280_data *data) static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID }; +static const int bmp380_temp_coeffs[] = { 10, 1 }; +static const int bmp380_press_coeffs[] = { 1, 100000 }; const struct bmp280_chip_info bmp380_chip_info = { .id_reg = BMP380_REG_ID, @@ -1244,6 +1255,11 @@ const struct bmp280_chip_info bmp380_chip_info = { .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), .iir_filter_coeff_default = 2, + .temp_coeffs = bmp380_temp_coeffs, + .temp_coeffs_type = IIO_VAL_FRACTIONAL, + .press_coeffs = bmp380_press_coeffs, + .press_coeffs_type = IIO_VAL_FRACTIONAL, + .chip_config = bmp380_chip_config, .read_temp = bmp380_read_temp, .read_press = bmp380_read_press, @@ -1364,9 +1380,8 @@ static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write) * for what is expected on IIO ABI. */ -static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2) +static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp) { - s32 raw_temp; int ret; ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf, @@ -1376,25 +1391,17 @@ static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2) return ret; } - raw_temp = get_unaligned_le24(data->buf); - if (raw_temp == BMP580_TEMP_SKIPPED) { + *raw_temp = get_unaligned_le24(data->buf); + if (*raw_temp == BMP580_TEMP_SKIPPED) { dev_err(data->dev, "reading temperature skipped\n"); return -EIO; } - /* - * Temperature is returned in Celsius degrees in fractional - * form down 2^16. We rescale by x1000 to return milli Celsius - * to respect IIO ABI. - */ - *val = raw_temp * 1000; - *val2 = 16; - return IIO_VAL_FRACTIONAL_LOG2; + return 0; } -static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2) +static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press) { - u32 raw_press; int ret; ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf, @@ -1404,18 +1411,13 @@ static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2) return ret; } - raw_press = get_unaligned_le24(data->buf); - if (raw_press == BMP580_PRESS_SKIPPED) { + *raw_press = get_unaligned_le24(data->buf); + if (*raw_press == BMP580_PRESS_SKIPPED) { dev_err(data->dev, "reading pressure skipped\n"); return -EIO; } - /* - * Pressure is returned in Pascals in fractional form down 2^16. - * We rescale /1000 to convert to kilopascal to respect IIO ABI. - */ - *val = raw_press; - *val2 = 64000; /* 2^6 * 1000 */ - return IIO_VAL_FRACTIONAL; + + return 0; } static const int bmp580_odr_table[][2] = { @@ -1720,6 +1722,8 @@ static int bmp580_chip_config(struct bmp280_data *data) static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT }; +static const int bmp580_temp_coeffs[] = { 1000, 16 }; +static const int bmp580_press_coeffs[] = { 1, 64000 }; const struct bmp280_chip_info bmp580_chip_info = { .id_reg = BMP580_REG_CHIP_ID, @@ -1746,6 +1750,11 @@ const struct bmp280_chip_info bmp580_chip_info = { .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), .iir_filter_coeff_default = 2, + .temp_coeffs = bmp280_temp_coeffs, + .temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2, + .press_coeffs = bmp280_press_coeffs, + .press_coeffs_type = IIO_VAL_FRACTIONAL, + .chip_config = bmp580_chip_config, .read_temp = bmp580_read_temp, .read_press = bmp580_read_press, @@ -1873,25 +1882,17 @@ static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) return (data->t_fine + 8) >> 4; } -static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2) +static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp) { - s32 adc_temp, comp_temp; + s32 adc_temp; int ret; ret = bmp180_read_adc_temp(data, &adc_temp); if (ret) return ret; - comp_temp = bmp180_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) { - *val = comp_temp * 100; - return IIO_VAL_INT; - } + if (comp_temp) + *comp_temp = bmp180_compensate_temp(data, adc_temp); return 0; } @@ -1953,15 +1954,13 @@ static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) return p + ((x1 + x2 + 3791) >> 4); } -static int bmp180_read_press(struct bmp280_data *data, - int *val, int *val2) +static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press) { - u32 comp_press; s32 adc_press; int ret; /* Read and compensate temperature so we get a reading of t_fine. */ - ret = bmp180_read_temp(data, NULL, NULL); + ret = bmp180_read_temp(data, NULL); if (ret) return ret; @@ -1969,12 +1968,9 @@ static int bmp180_read_press(struct bmp280_data *data, if (ret) return ret; - comp_press = bmp180_compensate_press(data, adc_press); - - *val = comp_press; - *val2 = 1000; + *comp_press = bmp180_compensate_press(data, adc_press); - return IIO_VAL_FRACTIONAL; + return 0; } static int bmp180_chip_config(struct bmp280_data *data) @@ -1985,6 +1981,8 @@ static int bmp180_chip_config(struct bmp280_data *data) static const int bmp180_oversampling_temp_avail[] = { 1 }; static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID }; +static const int bmp180_temp_coeffs[] = { 100, 1 }; +static const int bmp180_press_coeffs[] = { 1, 1000 }; const struct bmp280_chip_info bmp180_chip_info = { .id_reg = BMP280_REG_ID, @@ -2005,6 +2003,11 @@ const struct bmp280_chip_info bmp180_chip_info = { ARRAY_SIZE(bmp180_oversampling_press_avail), .oversampling_press_default = BMP180_MEAS_PRESS_8X, + .temp_coeffs = bmp180_temp_coeffs, + .temp_coeffs_type = IIO_VAL_FRACTIONAL, + .press_coeffs = bmp180_press_coeffs, + .press_coeffs_type = IIO_VAL_FRACTIONAL, + .chip_config = bmp180_chip_config, .read_temp = bmp180_read_temp, .read_press = bmp180_read_press, diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 4012387d7956..6d1dca31dd52 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -448,10 +448,17 @@ struct bmp280_chip_info { int num_sampling_freq_avail; int sampling_freq_default; + const int *temp_coeffs; + const int temp_coeffs_type; + const int *press_coeffs; + const int press_coeffs_type; + const int *humid_coeffs; + const int humid_coeffs_type; + int (*chip_config)(struct bmp280_data *); - int (*read_temp)(struct bmp280_data *, int *, int *); - int (*read_press)(struct bmp280_data *, int *, int *); - int (*read_humid)(struct bmp280_data *, int *, int *); + int (*read_temp)(struct bmp280_data *, s32 *); + int (*read_press)(struct bmp280_data *, u32 *); + int (*read_humid)(struct bmp280_data *, u32 *); int (*read_calib)(struct bmp280_data *); int (*preinit)(struct bmp280_data *); }; From patchwork Tue Mar 19 00:29:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596054 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (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 B937433FE; Tue, 19 Mar 2024 00:29:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808181; cv=none; b=TRw0Nrw0qfAIB55tkLLIPGhk7pFlvjtEyfQwl7GOGSNp06RkQncVuqDQK2/KXRkiJ6BjXKHH0dh8oXoPYWXqSu8mrxVWTjaM9J8dysBBImh7NrQHf1xAxw4uR2FG0cth13eKx8Ovmqpw+dI6ftWK0tNrXcUY/ebpHk4CwZFzi/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808181; c=relaxed/simple; bh=bckAFWelMQTYHoBmPhmyx86sCP8tz+XGH+4phCRTEO8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=unJlEfQvZ+t6/eryU+jLLrTGdsfUGAaINh22ThGqEHruVbAp9qCA0Zy4fcmY7pGR5ZC9Me8Ry/uFSupWmKeOwd0R6Egc5YrC7OHcB5roLpaJJd6Atz2Uyqf2gfrGkgXeXH9f8ECHboDGx3tDPy4L/Kq/AKMNddtO89GR0nBDZ4s= 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=TnzDgy6G; arc=none smtp.client-ip=209.85.208.51 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="TnzDgy6G" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-5688eaf1165so7225369a12.1; Mon, 18 Mar 2024 17:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808178; x=1711412978; 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=+sF/Ulgh9y8JBAdDd8JKsJdaWhYesUGo5m0d2yPND/g=; b=TnzDgy6G8rkkQy4FAzdXqMQgKBooBS2d7ESsp80Z5z8h6CNspj0ZymgjkBQZstYEp0 hAHJ7X17HbN1sXTZc+IluLlksMvhZ7E/h8bJe+HErsLnEgBxaNe7UKY7SMDey7mt4tsa 2u9SKF9i1kSEHvlsdjO3MAMworcflfOXDS7N8ujri64TD50qQN/T6mKYPf6uqQb/v3u6 wExfDuOSdO6BOLyL6Zl+DBb5yqjaGowqKwATV5WidMZn4HbigY4kJFs2sinLCkN+UvsW ovMIpsPumQG4qi6/Z2keoS4O4b+lQy80RRVm1bl/NC5PrS1779jJLk4s/ACxZSiPqkno qckQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808178; x=1711412978; 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=+sF/Ulgh9y8JBAdDd8JKsJdaWhYesUGo5m0d2yPND/g=; b=mMdG+U6lFT5N9K1VHkCK2i7d/U/bAoiO15hwTDylXHXnwfNWs2tR6F4nP89FlyjvwF 0x4EYAcfph7RJGSq633nsJG+FFxJLSlNmecGWC8AGE2uQv2FlvOuX76PFx5RZ3sNBNuc U94uu0KICI0ehc4JtREPRBZ+bWTvT7JvuRDGfkqp5ORJVi7dBTT3yH/suq0y7CtOCWTh ywq6NaikQX8pRDrif4ioS2lhlrfC/3eY+/jTbcRZWqdtnCatjCRnyXEpsoqjvNzT7IT2 gFS5n3gXOHAVlZR2vHjW9NGOubaa9He5gJ/1EUQKvdIbj3nLp4H2u/vNE8x3/SHjb8vh R90A== X-Forwarded-Encrypted: i=1; AJvYcCWdgfsGHLEWPTgPOFw6cBttBgDVnE4RdLGRZZ6I7eWpAvKZeGt1PSLfsTtDFHR2HzKFHxzPMKKA+J7MaHywoYtImjVXp2KKVtjWGlwRz8ZaB2m6M126P9JASpSudpSHxBQ8sTynbdzD X-Gm-Message-State: AOJu0Ywa7i/yZrs4itkVh5E7d4iGyrEx5wVgrTCanFJoD6MHcP73WnrD RUGjrilYeDmq3aF/nvXaBvCDoZ7StMLTHcN9dtKAkD5GH7y/omCr X-Google-Smtp-Source: AGHT+IGwVvHI2gzfPhnPi4wLWqKeWoB92ryibjKIfkbKXF5HRd0VFSbsdg2PBXCzkaywISNVlOY2PA== X-Received: by 2002:a05:6402:3645:b0:568:af3d:4a5f with SMTP id em5-20020a056402364500b00568af3d4a5fmr875014edb.22.1710808178170; Mon, 18 Mar 2024 17:29:38 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:37 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis Subject: [PATCH v3 4/6] iio: pressure: Add SCALE and RAW values for channels Date: Tue, 19 Mar 2024 01:29:23 +0100 Message-Id: <20240319002925.2121016-5-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add extra IIO_CHAN_INFO_SCALE and IIO_CHAN_INFO_RAW in order to be able to calculate the processed value with standard userspace IIO tools. Can be used for triggered buffers as well. Even though it is not a good design choice to have SCALE, RAW and PROCESSED together, the PROCESSED channel is kept for ABI compatibility. Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/bmp280-core.c | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index 6d6173c4b744..312bc2617583 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -137,17 +137,26 @@ enum { static const struct iio_chan_spec bmp280_channels[] = { { .type = IIO_PRESSURE, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), }, { .type = IIO_TEMP, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), }, { .type = IIO_HUMIDITYRELATIVE, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), }, }; @@ -155,21 +164,30 @@ static const struct iio_chan_spec bmp280_channels[] = { static const struct iio_chan_spec bmp380_channels[] = { { .type = IIO_PRESSURE, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | 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, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | 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, + /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | 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), @@ -487,6 +505,51 @@ static int bmp280_read_raw_guarded(struct iio_dev *indio_dev, return -EINVAL; } return 0; + case IIO_CHAN_INFO_RAW: + switch (chan->type) { + case IIO_HUMIDITYRELATIVE: + ret = data->chip_info->read_humid(data, &chan_value); + if (ret) + return ret; + + *val = chan_value; + return IIO_VAL_INT; + case IIO_PRESSURE: + ret = data->chip_info->read_press(data, &chan_value); + if (ret) + return ret; + + *val = chan_value; + return IIO_VAL_INT; + case IIO_TEMP: + ret = data->chip_info->read_press(data, &chan_value); + if (ret) + return ret; + + *val = chan_value; + return IIO_VAL_INT; + default: + return -EINVAL; + } + return 0; + case IIO_CHAN_INFO_SCALE: + switch (chan->type) { + case IIO_HUMIDITYRELATIVE: + *val = data->chip_info->humid_coeffs[0]; + *val2 = data->chip_info->humid_coeffs[1]; + return data->chip_info->humid_coeffs_type; + case IIO_PRESSURE: + *val = data->chip_info->press_coeffs[0]; + *val2 = data->chip_info->press_coeffs[1]; + return data->chip_info->press_coeffs_type; + case IIO_TEMP: + *val = data->chip_info->temp_coeffs[0]; + *val2 = data->chip_info->temp_coeffs[1]; + return data->chip_info->temp_coeffs_type; + default: + return -EINVAL; + } + return 0; case IIO_CHAN_INFO_OVERSAMPLING_RATIO: switch (chan->type) { case IIO_HUMIDITYRELATIVE: From patchwork Tue Mar 19 00:29:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596055 Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 5E4978BE2; Tue, 19 Mar 2024 00:29:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808183; cv=none; b=QtmMm6bdzgoXcXUZQ7TlBegxAdPY2UkI8+YKNODGDUwGHvBd2uaCvGiQQI9OvRleW/diKuT5a6O8RhBI0Q7ps2wHjkNiKGeJgYrV+WKdP1e5Rd+n2i1nWqlKRXeBZ3tAv2Qj/QbA0I7LIEDQIj8BGpwsH7EjBU5FnRsjR2k+7JE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808183; c=relaxed/simple; bh=l7f6Z0XDhtNxCIB1P4UObxHpCLmdQD4EPCI2L4JO+xM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RCcQti6MULUdm6m9NhIFAsxp61im6LGsHJLTMGZ9Kr9+PDnVUj45zf63eLMY+6KrvfsyZPATpC3zbYcbu7ePlJduuOtSq1pjJwGXmYsK6E4ZKfRB1wUcfnTbw5cQgwUjy+8eRjQay+FZk1spMTqA+9m03lGWAHX6FrnZAaiu+SU= 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=ipWecq1T; arc=none smtp.client-ip=209.85.208.173 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="ipWecq1T" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d220e39907so72166201fa.1; Mon, 18 Mar 2024 17:29:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808179; x=1711412979; 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=xfvj86vKtlcJm2UnyEfOzzQiI5fSWunqj8tDd9RzXJI=; b=ipWecq1TvO63Tk6GBzGY37lkI7ztFZeG8Yg6+G+F1o2qE3tDIesa2c3bHro60acz+d oWVwwDhg4is55sf4utmWdaiE4JjJMdAxK3ZuUIwbg9MFpuwHCpeeIgME8K8IXT7zoQ0G xMBbLlmV1gr0DYE6LWSsdhvkBICDJl6Ny2ow3+TGRPIadD+cRyaZjXklz8S/BxJZDds7 CxdXd9vAuYHim09YoQfmnbkZUdYvDTZhXZxDC69I01u3TnKxMHiJohXK+GTey/uL4JWV CavMGZGUf6s6Xv6eA5gg5Yj7pScWI/fPA3e89TeekPjnm0ioB3LtK6hu4QGfG+O8p7hm 3udA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808179; x=1711412979; 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=xfvj86vKtlcJm2UnyEfOzzQiI5fSWunqj8tDd9RzXJI=; b=KA0ppD75Br/mjXICzW7za6EeeDOoCtBXqXKTHQr9wsLsym/mqZvr51JZBTN8Jdv4Ft 0dVI056OlPNKqe7XvkXqEK1R2Hi3FYi402m5haHoyCjLJlzURA4MWXcvjbpSitPYJpSQ xitOyBHYaljxSJHHmBtjQJ+ym4TvgjBnk5drBFlSNO4CtOY2OGnhgdARZsGG2Y8Ul5zT ndC1GLx3rw6kn3HpyhMiZEkjJDvH0FK4O9glqUK8FIXHyOFBU52STD4zdLL9cmJNjrRQ zNkCfYs3GQQW0/HTibL0xb+R5QR0MLY8ChaWT2qSDidNewBcSWTxZqBk0TV8CwTI/UqF R/Qw== X-Forwarded-Encrypted: i=1; AJvYcCU+PZf+0NQWWDV4eKjdAh6xkCQIiTr7rNGrMV9SQmYbIDjJJ+se643hT4SH/yg1TQO1SsxA5v3kZ2LbBWbAWzVf7ym8Ff13ViWQRun249Y78jiX8NzDLvpiX/ilrThLtHvlzjukkmQS X-Gm-Message-State: AOJu0YwK82UmghRc6y6Fg26Ta0+as30Et2f8Db/pqohdp4r8k5mKpxZv XmASxPatOp6sFwbD+Ey0a1qCihq1JDFyeJQYnJqHUrJkqgDwnPwW X-Google-Smtp-Source: AGHT+IHtR9z2o+YMCMEKWpxs/XoUaMWovrA1ziNOe8ZNTiJ/92HpfzqQFvC9qkyqLPe+MFmd1cVEOA== X-Received: by 2002:a2e:3508:0:b0:2d4:96ae:2d9d with SMTP id z8-20020a2e3508000000b002d496ae2d9dmr6002145ljz.33.1710808179196; Mon, 18 Mar 2024 17:29:39 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:38 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis Subject: [PATCH v3 5/6] iio: pressure: Add timestamp and scan_masks for BMP280 driver Date: Tue, 19 Mar 2024 01:29:24 +0100 Message-Id: <20240319002925.2121016-6-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The scan mask for the BME280 supports humidity measurement and needs to be distinguished from the rest in order for the timestamp to be able to work. Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/bmp280-core.c | 111 +++++++++++++++++++++++++---- drivers/iio/pressure/bmp280.h | 1 + 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index 312bc2617583..ddfcd23f29a0 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -134,7 +134,28 @@ enum { BMP380_P11 = 20, }; +enum bmp280_scan { + BMP280_TEMP, + BMP280_PRESS, + BME280_HUMID +}; + static const struct iio_chan_spec bmp280_channels[] = { + { + .type = IIO_TEMP, + /* PROCESSED maintained for ABI backwards compatibility */ + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | + BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .scan_index = 0, + .scan_type = { + .sign = 's', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, + }, { .type = IIO_PRESSURE, /* PROCESSED maintained for ABI backwards compatibility */ @@ -142,7 +163,18 @@ static const struct iio_chan_spec bmp280_channels[] = { BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .scan_index = 1, + .scan_type = { + .sign = 'u', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(2), +}; + +static const struct iio_chan_spec bme280_channels[] = { { .type = IIO_TEMP, /* PROCESSED maintained for ABI backwards compatibility */ @@ -150,28 +182,48 @@ static const struct iio_chan_spec bmp280_channels[] = { BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .scan_index = 0, + .scan_type = { + .sign = 's', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, { - .type = IIO_HUMIDITYRELATIVE, + .type = IIO_PRESSURE, /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), + .scan_index = 1, + .scan_type = { + .sign = 'u', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, -}; - -static const struct iio_chan_spec bmp380_channels[] = { { - .type = IIO_PRESSURE, + .type = IIO_HUMIDITYRELATIVE, /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | 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), + .scan_index = 2, + .scan_type = { + .sign = 'u', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(3), +}; + +static const struct iio_chan_spec bmp380_channels[] = { { .type = IIO_TEMP, /* PROCESSED maintained for ABI backwards compatibility */ @@ -181,9 +233,16 @@ static const struct iio_chan_spec bmp380_channels[] = { 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), + .scan_index = 0, + .scan_type = { + .sign = 's', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, { - .type = IIO_HUMIDITYRELATIVE, + .type = IIO_PRESSURE, /* PROCESSED maintained for ABI backwards compatibility */ .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | BIT(IIO_CHAN_INFO_RAW) | @@ -191,7 +250,15 @@ static const struct iio_chan_spec bmp380_channels[] = { 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), + .scan_index = 1, + .scan_type = { + .sign = 'u', + .realbits = 32, + .storagebits = 32, + .endianness = IIO_CPU, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(2), }; static int bmp280_read_calib(struct bmp280_data *data) @@ -825,6 +892,16 @@ static const struct iio_info bmp280_info = { .write_raw = &bmp280_write_raw, }; +static const unsigned long bmp280_avail_scan_masks[] = { + BIT(BMP280_PRESS) | BIT(BMP280_TEMP), + 0 +}; + +static const unsigned long bme280_avail_scan_masks[] = { + BIT(BME280_HUMID) | BIT(BMP280_PRESS) | BIT(BMP280_TEMP), + 0 +}; + static int bmp280_chip_config(struct bmp280_data *data) { u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) | @@ -866,7 +943,8 @@ const struct bmp280_chip_info bmp280_chip_info = { .regmap_config = &bmp280_regmap_config, .start_up_time = 2000, .channels = bmp280_channels, - .num_channels = 2, + .num_channels = 3, + .avail_scan_masks = bmp280_avail_scan_masks, .oversampling_temp_avail = bmp280_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), @@ -925,8 +1003,9 @@ const struct bmp280_chip_info bme280_chip_info = { .num_chip_id = ARRAY_SIZE(bme280_chip_ids), .regmap_config = &bmp280_regmap_config, .start_up_time = 2000, - .channels = bmp280_channels, - .num_channels = 3, + .channels = bme280_channels, + .num_channels = 4, + .avail_scan_masks = bme280_avail_scan_masks, .oversampling_temp_avail = bmp280_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), @@ -1300,7 +1379,8 @@ const struct bmp280_chip_info bmp380_chip_info = { .regmap_config = &bmp380_regmap_config, .start_up_time = 2000, .channels = bmp380_channels, - .num_channels = 2, + .num_channels = 3, + .avail_scan_masks = bmp280_avail_scan_masks, .oversampling_temp_avail = bmp380_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail), @@ -1795,7 +1875,8 @@ const struct bmp280_chip_info bmp580_chip_info = { .regmap_config = &bmp580_regmap_config, .start_up_time = 2000, .channels = bmp380_channels, - .num_channels = 2, + .num_channels = 3, + .avail_scan_masks = bmp280_avail_scan_masks, .oversampling_temp_avail = bmp580_oversampling_avail, .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail), @@ -2054,7 +2135,8 @@ const struct bmp280_chip_info bmp180_chip_info = { .regmap_config = &bmp180_regmap_config, .start_up_time = 2000, .channels = bmp280_channels, - .num_channels = 2, + .num_channels = 3, + .avail_scan_masks = bmp280_avail_scan_masks, .oversampling_temp_avail = bmp180_oversampling_temp_avail, .num_oversampling_temp_avail = @@ -2166,6 +2248,7 @@ int bmp280_common_probe(struct device *dev, /* Apply initial values from chip info structure */ indio_dev->channels = chip_info->channels; indio_dev->num_channels = chip_info->num_channels; + indio_dev->available_scan_masks = chip_info->avail_scan_masks; data->oversampling_press = chip_info->oversampling_press_default; data->oversampling_humid = chip_info->oversampling_humid_default; data->oversampling_temp = chip_info->oversampling_temp_default; diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 6d1dca31dd52..8cc3eed70c18 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -427,6 +427,7 @@ struct bmp280_chip_info { const struct iio_chan_spec *channels; int num_channels; unsigned int start_up_time; + const unsigned long *avail_scan_masks; const int *oversampling_temp_avail; int num_oversampling_temp_avail; From patchwork Tue Mar 19 00:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasileios Amoiridis X-Patchwork-Id: 13596056 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 46F61F503; Tue, 19 Mar 2024 00:29:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808184; cv=none; b=peimUnh4MgZemrDR0D0hw682cFTkgUob3J2iqH23zSq1dCsGva+XVo1bZF6lESHvlhF8VO6h6TXfRXDVwJG2fnqkxGZFhtTfnF7F/EeiHXhSgOUrsPp5Fp3+IOZEDVxi1Xm/6THGin0tNHUMOLrLEAPJQfp3D9MuBgIVIQvBRmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808184; c=relaxed/simple; bh=LtN4ZB+WVs9lNVCnzOsWcqArQwLuyWCeQxbL4WvR9A4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BXRa0wLqt5c7avP1S3tSBGTbbuAOxdm14VgU7Ej0/zWlDELIaTlhzPFy7d+cMT0JD1u4ckoigo58XZffO7CYcT0gC+TLX8VGvjgoqhpYwlK4GybvOSqUJ84qQV9xL99FA2emVnzi8DXubBtahtihZjs/23AQHMvfyZnZVPXEBXI= 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=GQjgz2Ca; arc=none smtp.client-ip=209.85.208.41 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="GQjgz2Ca" Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-56a2b881911so1760518a12.3; Mon, 18 Mar 2024 17:29:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710808180; x=1711412980; 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=4LiyjhF7+GyLLHMZmqQqv+Lvr6yzCKpFrf7i6pLySqY=; b=GQjgz2Ca6q7j9d3WRso2sVFNUPq+wnXMzMEOa8Bhl3x1JCc0NopTZbIgXir055bwzh FHxSk8VMKB/goixZkvpB0aeTkfb9WF6ZBX20BCW4rNDYk5m+NJEW5Y7fNVOwryMmiXOQ b/tbUH13MtytDRaBIGUqYZGNXD0HZOckb4nNv/QsMtbEUaX01Sxf6w6mCIXAQm3gKDTj fgMl1wuktlFLAj/4BIL4EtZFG+lY0+C5BtvObA8fwI5JCRWUl5WAnUS3eU46N14rzldp iLvcmNxdY8jysVuepoWCWC32cSHnm7TRTArHxucjzb8Sby0gwiqUcKIuNm3TPMEL9owp UzAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808180; x=1711412980; 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=4LiyjhF7+GyLLHMZmqQqv+Lvr6yzCKpFrf7i6pLySqY=; b=wwLC4GmRSZTMC3oKsjVDT89E5RRt/r5RhXuPQTfScADyU3u6T0juAijkzq2XbpXOBm 9GzDu1Hon7w1Bd8O9kFGnf8ZmA5ji7gpE7jAJa/dCEU1hLwodN0q2eW+WI7FxG04dbSh +zcZZBfroBOVmTFQB+lDlYzbqxA/0hU0sAl9VcZt6JOkM8fR+c4J+EuYlC6M6V9qOgEV 8emRogpjr20OqNOVl55AD7NUNpxsoxP+bRlymthJ9e67vJCCfC+RQDgPFuFtkyIZM88H wvOtN9jtQ7U7wDNJmFF6MnbYPTMmjL9UTsfB1DWOTd4sMeldWCAL0jaA7q87eLpnt6K9 ZnkA== X-Forwarded-Encrypted: i=1; AJvYcCXr3QlPZdO0bY82muJl5Z9XrvfaZ5BHTwR+DVYmegnSdHckTwkIFA4AXmwRLA78pvBiA0x5eCDyNAFpjloJZD+i7H6Wg59GWHJwuA0uzemaJ8HcVsR90ItpRy3zBJJ3kGD0scjGsUrI X-Gm-Message-State: AOJu0YwX1PYPRjsj4J7amcODUuaN13e+Q9uQ5yatQSN7nLqrlrSuLIvi 0mL7XW6yszLPAGqVUam5R4dcuU9cpy8zIXaSfOOgi4JzwIpcEjV9 X-Google-Smtp-Source: AGHT+IGakkM11RqQhsiODsvj+HB4tZNOsfRjEgpBpl88u8OSNfoCnxj8naoVFHH4upHTsHRq9/CtJQ== X-Received: by 2002:a05:6402:3711:b0:568:b498:ce49 with SMTP id ek17-20020a056402371100b00568b498ce49mr630008edb.3.1710808180588; Mon, 18 Mar 2024 17:29:40 -0700 (PDT) Received: from localhost.localdomain ([2a04:ee41:82:7577:8c96:c55f:eab0:860]) by smtp.gmail.com with ESMTPSA id co24-20020a0564020c1800b00568c613570dsm2700227edb.79.2024.03.18.17.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:29:39 -0700 (PDT) From: Vasileios Amoiridis To: jic23@kernel.org Cc: lars@metafoo.de, andriy.shevchenko@linux.intel.com, ang.iglesiasg@gmail.com, mazziesaccount@gmail.com, ak@it-klinger.de, petre.rodan@subdimension.ro, phil@raspberrypi.com, 579lpy@gmail.com, linus.walleij@linaro.org, semen.protsenko@linaro.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Vasileios Amoiridis Subject: [PATCH v3 6/6] iio: pressure: Add triggered buffer support for BMP280 driver Date: Tue, 19 Mar 2024 01:29:25 +0100 Message-Id: <20240319002925.2121016-7-vassilisamir@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240319002925.2121016-1-vassilisamir@gmail.com> References: <20240319002925.2121016-1-vassilisamir@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 BMP2xx, BMP3xx, and BMP5xx use consecutive buffers for their temperature, pressure and humidity readings. This facilitates the use of burst reads in order to acquire data much faster and in a different way from the one used in oneshot captures. BMP085 and BMP180 use a completely different measurement process that is well defined and is used in their buffer_handler(). Suggested-by: Angel Iglesias Signed-off-by: Vasileios Amoiridis --- drivers/iio/pressure/Kconfig | 2 + drivers/iio/pressure/bmp280-core.c | 231 +++++++++++++++++++++++++++-- drivers/iio/pressure/bmp280-spi.c | 8 +- drivers/iio/pressure/bmp280.h | 14 +- 4 files changed, 241 insertions(+), 14 deletions(-) diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig index 79adfd059c3a..5145b94b4679 100644 --- a/drivers/iio/pressure/Kconfig +++ b/drivers/iio/pressure/Kconfig @@ -31,6 +31,8 @@ config BMP280 select REGMAP select BMP280_I2C if (I2C) select BMP280_SPI if (SPI_MASTER) + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER help Say yes here to build support for Bosch Sensortec BMP180, BMP280, BMP380 and BMP580 pressure and temperature sensors. Also supports the BME280 with diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c index ddfcd23f29a0..ffcd17807cf5 100644 --- a/drivers/iio/pressure/bmp280-core.c +++ b/drivers/iio/pressure/bmp280-core.c @@ -40,7 +40,10 @@ #include #include +#include #include +#include +#include #include @@ -454,7 +457,7 @@ static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp) int ret; ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, - data->buf, sizeof(data->buf)); + data->buf, BMP280_NUM_TEMP_BYTES); if (ret < 0) { dev_err(data->dev, "failed to read temperature\n"); return ret; @@ -484,7 +487,7 @@ static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press) return ret; ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, - data->buf, sizeof(data->buf)); + data->buf, BMP280_NUM_PRESS_BYTES); if (ret < 0) { dev_err(data->dev, "failed to read pressure\n"); return ret; @@ -513,13 +516,13 @@ static int bmp280_read_humid(struct bmp280_data *data, u32 *comp_humidity) return ret; ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, - &data->be16, sizeof(data->be16)); + &data->be16, BME280_NUM_HUMIDITY_BYTES); if (ret < 0) { dev_err(data->dev, "failed to read humidity\n"); return ret; } - adc_humidity = be16_to_cpu(data->be16); + adc_humidity = get_unaligned_be16(&data->be16); if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { /* reading was skipped */ dev_err(data->dev, "reading humidity skipped\n"); @@ -931,6 +934,73 @@ static int bmp280_chip_config(struct bmp280_data *data) return ret; } +static irqreturn_t bmp280_buffer_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct bmp280_data *data = iio_priv(indio_dev); + s32 adc_temp, adc_press, adc_humidity; + u8 size_of_burst_read; + int ret, chan_value; + + guard(mutex)(&data->lock); + + if (test_bit(BME280_HUMID, indio_dev->active_scan_mask)) + size_of_burst_read = 8; + else + size_of_burst_read = 6; + + /* Burst read data registers */ + ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, + data->buf, 8); + if (ret) { + dev_err(data->dev, "failed to read sensor data\n"); + return ret; + } + + /* Temperature calculations */ + memcpy(&chan_value, &data->buf[3], 3); + + adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&chan_value)); + if (adc_temp == BMP280_TEMP_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading temperature skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[0] = bmp280_compensate_temp(data, adc_temp); + + /* Pressure calculations */ + memcpy(&chan_value, &data->buf[0], 3); + + adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&chan_value)); + if (adc_press == BMP280_PRESS_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading pressure skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[1] = bmp280_compensate_press(data, adc_press); + + /* Humidity calculations */ + if (test_bit(BME280_HUMID, indio_dev->active_scan_mask)) { + memcpy(&chan_value, &data->buf[6], 2); + + adc_humidity = get_unaligned_be16(&chan_value); + if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading humidity skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[2] = bmp280_compensate_humidity(data, adc_humidity); + } + + iio_push_to_buffers_with_timestamp(indio_dev, &data->iio_buf, + iio_get_time_ns(indio_dev)); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID }; static const int bmp280_temp_coeffs[] = { 10, 1 }; @@ -973,6 +1043,8 @@ const struct bmp280_chip_info bmp280_chip_info = { .read_temp = bmp280_read_temp, .read_press = bmp280_read_press, .read_calib = bmp280_read_calib, + + .buffer_handler = bmp280_buffer_handler }; EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280); @@ -1032,6 +1104,8 @@ const struct bmp280_chip_info bme280_chip_info = { .read_press = bmp280_read_press, .read_humid = bmp280_read_humid, .read_calib = bme280_read_calib, + + .buffer_handler = bmp280_buffer_handler }; EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280); @@ -1158,7 +1232,7 @@ static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp) int ret; ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB, - data->buf, sizeof(data->buf)); + data->buf, BMP280_NUM_TEMP_BYTES); if (ret) { dev_err(data->dev, "failed to read temperature\n"); return ret; @@ -1187,7 +1261,7 @@ static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press) return ret; ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, - data->buf, sizeof(data->buf)); + data->buf, BMP280_NUM_PRESS_BYTES); if (ret) { dev_err(data->dev, "failed to read pressure\n"); return ret; @@ -1366,6 +1440,54 @@ static int bmp380_chip_config(struct bmp280_data *data) return 0; } +static irqreturn_t bmp380_buffer_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct bmp280_data *data = iio_priv(indio_dev); + s32 adc_temp, adc_press; + int ret, chan_value; + + guard(mutex)(&data->lock); + + /* Burst read data registers */ + ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, + data->buf, 6); + if (ret) { + dev_err(data->dev, "failed to read sensor data\n"); + return ret; + } + + /* Temperature calculations */ + memcpy(&chan_value, &data->buf[3], 3); + + adc_temp = get_unaligned_le24(&chan_value); + if (adc_temp == BMP380_TEMP_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading temperature skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[0] = bmp380_compensate_temp(data, adc_temp); + + /* Pressure calculations */ + memcpy(&chan_value, &data->buf[0], 3); + + adc_press = get_unaligned_le24(&chan_value); + if (adc_press == BMP380_PRESS_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading pressure skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[1] = bmp380_compensate_press(data, adc_press); + + iio_push_to_buffers_with_timestamp(indio_dev, &data->iio_buf, + iio_get_time_ns(indio_dev)); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID }; @@ -1408,6 +1530,8 @@ const struct bmp280_chip_info bmp380_chip_info = { .read_press = bmp380_read_press, .read_calib = bmp380_read_calib, .preinit = bmp380_preinit, + + .buffer_handler = bmp380_buffer_handler }; EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280); @@ -1528,7 +1652,7 @@ static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp) int ret; ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf, - sizeof(data->buf)); + BMP280_NUM_TEMP_BYTES); if (ret) { dev_err(data->dev, "failed to read temperature\n"); return ret; @@ -1548,7 +1672,7 @@ static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press) int ret; ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf, - sizeof(data->buf)); + BMP280_NUM_PRESS_BYTES); if (ret) { dev_err(data->dev, "failed to read pressure\n"); return ret; @@ -1863,6 +1987,54 @@ static int bmp580_chip_config(struct bmp280_data *data) return 0; } +static irqreturn_t bmp580_buffer_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct bmp280_data *data = iio_priv(indio_dev); + s32 adc_temp, adc_press; + int ret, chan_value; + + guard(mutex)(&data->lock); + + /* Burst read data registers */ + ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, + data->buf, 6); + if (ret) { + dev_err(data->dev, "failed to read sensor data\n"); + return ret; + } + + /* Temperature calculations */ + memcpy(&chan_value, &data->buf[3], 3); + + adc_temp = get_unaligned_le24(&chan_value); + if (adc_temp == BMP580_TEMP_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading temperature skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[0] = adc_temp; + + /* Pressure calculations */ + memcpy(&chan_value, &data->buf[0], 3); + + adc_press = get_unaligned_le24(&chan_value); + if (adc_press == BMP580_PRESS_SKIPPED) { + /* reading was skipped */ + dev_err(data->dev, "reading pressure skipped\n"); + return -EIO; + } + data->iio_buf.sensor_data[1] = adc_press; + + iio_push_to_buffers_with_timestamp(indio_dev, &data->iio_buf, + iio_get_time_ns(indio_dev)); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT }; static const int bmp580_temp_coeffs[] = { 1000, 16 }; @@ -1903,6 +2075,8 @@ const struct bmp280_chip_info bmp580_chip_info = { .read_temp = bmp580_read_temp, .read_press = bmp580_read_press, .preinit = bmp580_preinit, + + .buffer_handler = bmp580_buffer_handler }; EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280); @@ -2054,7 +2228,7 @@ static int bmp180_read_adc_press(struct bmp280_data *data, int *val) return ret; ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, - data->buf, sizeof(data->buf)); + data->buf, BMP280_NUM_PRESS_BYTES); if (ret) return ret; @@ -2122,6 +2296,35 @@ static int bmp180_chip_config(struct bmp280_data *data) return 0; } +static irqreturn_t bmp180_buffer_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct bmp280_data *data = iio_priv(indio_dev); + int ret, chan_value; + + guard(mutex)(&data->lock); + + ret = data->chip_info->read_temp(data, &chan_value); + if (ret) + return ret; + + data->iio_buf.sensor_data[0] = chan_value; + + ret = data->chip_info->read_press(data, &chan_value); + if (ret) + return ret; + + data->iio_buf.sensor_data[1] = chan_value; + + iio_push_to_buffers_with_timestamp(indio_dev, &data->iio_buf, + iio_get_time_ns(indio_dev)); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static const int bmp180_oversampling_temp_avail[] = { 1 }; static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID }; @@ -2157,6 +2360,8 @@ const struct bmp280_chip_info bmp180_chip_info = { .read_temp = bmp180_read_temp, .read_press = bmp180_read_press, .read_calib = bmp180_read_calib, + + .buffer_handler = bmp180_buffer_handler }; EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280); @@ -2332,6 +2537,14 @@ int bmp280_common_probe(struct device *dev, "failed to read calibration coefficients\n"); } + ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev, + iio_pollfunc_store_time, + data->chip_info->buffer_handler, + NULL); + if (ret) + return dev_err_probe(data->dev, ret, + "iio triggered buffer setup failed\n"); + /* * Attempt to grab an optional EOC IRQ - only the BMP085 has this * however as it happens, the BMP085 shares the chip ID of BMP180 diff --git a/drivers/iio/pressure/bmp280-spi.c b/drivers/iio/pressure/bmp280-spi.c index a444d4b2978b..dc297583cac1 100644 --- a/drivers/iio/pressure/bmp280-spi.c +++ b/drivers/iio/pressure/bmp280-spi.c @@ -40,14 +40,14 @@ static int bmp380_regmap_spi_read(void *context, const void *reg, size_t reg_size, void *val, size_t val_size) { struct spi_device *spi = to_spi_device(context); - u8 rx_buf[4]; + u8 rx_buf[9]; ssize_t status; /* - * Maximum number of consecutive bytes read for a temperature or - * pressure measurement is 3. + * Maximum number of a burst read for temperature, pressure, humidity + * is 8 bytes. */ - if (val_size > 3) + if (val_size > 8) return -EINVAL; /* diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h index 8cc3eed70c18..32155567faf6 100644 --- a/drivers/iio/pressure/bmp280.h +++ b/drivers/iio/pressure/bmp280.h @@ -301,6 +301,10 @@ #define BMP280_PRESS_SKIPPED 0x80000 #define BMP280_HUMIDITY_SKIPPED 0x8000 +#define BMP280_NUM_TEMP_BYTES 3 +#define BMP280_NUM_PRESS_BYTES 3 +#define BME280_NUM_HUMIDITY_BYTES 2 + /* Core exported structs */ static const char *const bmp280_supply_names[] = { @@ -400,13 +404,19 @@ struct bmp280_data { */ s32 t_fine; + /* Data to push to userspace */ + struct { + s32 sensor_data[3]; + s64 timestamp __aligned(8); + } iio_buf; + /* * DMA (thus cache coherency maintenance) may require the * transfer buffers to live in their own cache lines. */ union { /* Sensor data buffer */ - u8 buf[3]; + u8 buf[8]; /* Calibration data buffers */ __le16 bmp280_cal_buf[BMP280_CONTIGUOUS_CALIB_REGS / 2]; __be16 bmp180_cal_buf[BMP180_REG_CALIB_COUNT / 2]; @@ -462,6 +472,8 @@ struct bmp280_chip_info { int (*read_humid)(struct bmp280_data *, u32 *); int (*read_calib)(struct bmp280_data *); int (*preinit)(struct bmp280_data *); + + irqreturn_t (*buffer_handler)(int, void *); }; /* Chip infos for each variant */