Commit d174ebd4 authored by Vasileios Amoiridis's avatar Vasileios Amoiridis Committed by Jonathan Cameron

iio: pressure: bmp280: Introduce new cleanup routines

Introduce new linux/cleanup.h with the guard(mutex) functionality.
Suggested-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Suggested-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: default avatarVasileios Amoiridis <vassilisamir@gmail.com>
Link: https://lore.kernel.org/r/20240512230524.53990-3-vassilisamir@gmail.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 5d6e6c6e
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/bitfield.h> #include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/device.h> #include <linux/device.h>
...@@ -505,77 +506,67 @@ static int bme280_read_humid(struct bmp280_data *data, int *val, int *val2) ...@@ -505,77 +506,67 @@ static int bme280_read_humid(struct bmp280_data *data, int *val, int *val2)
return IIO_VAL_INT; return IIO_VAL_INT;
} }
static int bmp280_read_raw(struct iio_dev *indio_dev, static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
int *val, int *val2, long mask) int *val, int *val2, long mask)
{ {
struct bmp280_data *data = iio_priv(indio_dev); struct bmp280_data *data = iio_priv(indio_dev);
int ret;
pm_runtime_get_sync(data->dev); guard(mutex)(&data->lock);
mutex_lock(&data->lock);
switch (mask) { switch (mask) {
case IIO_CHAN_INFO_PROCESSED: case IIO_CHAN_INFO_PROCESSED:
switch (chan->type) { switch (chan->type) {
case IIO_HUMIDITYRELATIVE: case IIO_HUMIDITYRELATIVE:
ret = data->chip_info->read_humid(data, val, val2); return data->chip_info->read_humid(data, val, val2);
break;
case IIO_PRESSURE: case IIO_PRESSURE:
ret = data->chip_info->read_press(data, val, val2); return data->chip_info->read_press(data, val, val2);
break;
case IIO_TEMP: case IIO_TEMP:
ret = data->chip_info->read_temp(data, val, val2); return data->chip_info->read_temp(data, val, val2);
break;
default: default:
ret = -EINVAL; return -EINVAL;
break;
} }
break;
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
switch (chan->type) { switch (chan->type) {
case IIO_HUMIDITYRELATIVE: case IIO_HUMIDITYRELATIVE:
*val = 1 << data->oversampling_humid; *val = 1 << data->oversampling_humid;
ret = IIO_VAL_INT; return IIO_VAL_INT;
break;
case IIO_PRESSURE: case IIO_PRESSURE:
*val = 1 << data->oversampling_press; *val = 1 << data->oversampling_press;
ret = IIO_VAL_INT; return IIO_VAL_INT;
break;
case IIO_TEMP: case IIO_TEMP:
*val = 1 << data->oversampling_temp; *val = 1 << data->oversampling_temp;
ret = IIO_VAL_INT; return IIO_VAL_INT;
break;
default: default:
ret = -EINVAL; return -EINVAL;
break;
} }
break;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
if (!data->chip_info->sampling_freq_avail) { if (!data->chip_info->sampling_freq_avail)
ret = -EINVAL; return -EINVAL;
break;
}
*val = data->chip_info->sampling_freq_avail[data->sampling_freq][0]; *val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
*val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1]; *val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
ret = IIO_VAL_INT_PLUS_MICRO; return IIO_VAL_INT_PLUS_MICRO;
break;
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
if (!data->chip_info->iir_filter_coeffs_avail) { if (!data->chip_info->iir_filter_coeffs_avail)
ret = -EINVAL; return -EINVAL;
break;
}
*val = (1 << data->iir_filter_coeff) - 1; *val = (1 << data->iir_filter_coeff) - 1;
ret = IIO_VAL_INT; return IIO_VAL_INT;
break;
default: default:
ret = -EINVAL; return -EINVAL;
break;
} }
}
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;
mutex_unlock(&data->lock); pm_runtime_get_sync(data->dev);
ret = bmp280_read_raw_impl(indio_dev, chan, val, val2, mask);
pm_runtime_mark_last_busy(data->dev); pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev); pm_runtime_put_autosuspend(data->dev);
...@@ -707,12 +698,13 @@ static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val) ...@@ -707,12 +698,13 @@ static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
return -EINVAL; return -EINVAL;
} }
static int bmp280_write_raw(struct iio_dev *indio_dev, static int bmp280_write_raw_impl(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, struct iio_chan_spec const *chan,
int val, int val2, long mask) int val, int val2, long mask)
{ {
struct bmp280_data *data = iio_priv(indio_dev); struct bmp280_data *data = iio_priv(indio_dev);
int ret = 0;
guard(mutex)(&data->lock);
/* /*
* Helper functions to update sensor running configuration. * Helper functions to update sensor running configuration.
...@@ -722,45 +714,36 @@ static int bmp280_write_raw(struct iio_dev *indio_dev, ...@@ -722,45 +714,36 @@ static int bmp280_write_raw(struct iio_dev *indio_dev,
*/ */
switch (mask) { switch (mask) {
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
pm_runtime_get_sync(data->dev);
mutex_lock(&data->lock);
switch (chan->type) { switch (chan->type) {
case IIO_HUMIDITYRELATIVE: case IIO_HUMIDITYRELATIVE:
ret = bme280_write_oversampling_ratio_humid(data, val); return bme280_write_oversampling_ratio_humid(data, val);
break;
case IIO_PRESSURE: case IIO_PRESSURE:
ret = bmp280_write_oversampling_ratio_press(data, val); return bmp280_write_oversampling_ratio_press(data, val);
break;
case IIO_TEMP: case IIO_TEMP:
ret = bmp280_write_oversampling_ratio_temp(data, val); return bmp280_write_oversampling_ratio_temp(data, val);
break;
default: default:
ret = -EINVAL; return -EINVAL;
break;
} }
mutex_unlock(&data->lock);
pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev);
break;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
pm_runtime_get_sync(data->dev); return bmp280_write_sampling_frequency(data, val, val2);
mutex_lock(&data->lock);
ret = bmp280_write_sampling_frequency(data, val, val2);
mutex_unlock(&data->lock);
pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev);
break;
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
pm_runtime_get_sync(data->dev); return bmp280_write_iir_filter_coeffs(data, val);
mutex_lock(&data->lock);
ret = bmp280_write_iir_filter_coeffs(data, val);
mutex_unlock(&data->lock);
pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev);
break;
default: default:
return -EINVAL; return -EINVAL;
} }
}
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;
pm_runtime_get_sync(data->dev);
ret = bmp280_write_raw_impl(indio_dev, chan, val, val2, mask);
pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev);
return ret; return ret;
} }
...@@ -1551,15 +1534,14 @@ static const int bmp580_odr_table[][2] = { ...@@ -1551,15 +1534,14 @@ static const int bmp580_odr_table[][2] = {
static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 }; static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val, static int bmp580_nvmem_read_impl(void *priv, unsigned int offset, void *val,
size_t bytes) size_t bytes)
{ {
struct bmp280_data *data = priv; struct bmp280_data *data = priv;
u16 *dst = val; u16 *dst = val;
int ret, addr; int ret, addr;
pm_runtime_get_sync(data->dev); guard(mutex)(&data->lock);
mutex_lock(&data->lock);
/* Set sensor in standby mode */ /* Set sensor in standby mode */
ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
...@@ -1601,21 +1583,31 @@ static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val, ...@@ -1601,21 +1583,31 @@ static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
exit: exit:
/* Restore chip config */ /* Restore chip config */
data->chip_info->chip_config(data); data->chip_info->chip_config(data);
mutex_unlock(&data->lock); return ret;
}
static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
size_t bytes)
{
struct bmp280_data *data = priv;
int ret;
pm_runtime_get_sync(data->dev);
ret = bmp580_nvmem_read_impl(priv, offset, val, bytes);
pm_runtime_mark_last_busy(data->dev); pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev); pm_runtime_put_autosuspend(data->dev);
return ret; return ret;
} }
static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val, static int bmp580_nvmem_write_impl(void *priv, unsigned int offset, void *val,
size_t bytes) size_t bytes)
{ {
struct bmp280_data *data = priv; struct bmp280_data *data = priv;
u16 *buf = val; u16 *buf = val;
int ret, addr; int ret, addr;
pm_runtime_get_sync(data->dev); guard(mutex)(&data->lock);
mutex_lock(&data->lock);
/* Set sensor in standby mode */ /* Set sensor in standby mode */
ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG, ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
...@@ -1666,9 +1658,20 @@ static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val, ...@@ -1666,9 +1658,20 @@ static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
exit: exit:
/* Restore chip config */ /* Restore chip config */
data->chip_info->chip_config(data); data->chip_info->chip_config(data);
mutex_unlock(&data->lock); return ret;
}
static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
size_t bytes)
{
struct bmp280_data *data = priv;
int ret;
pm_runtime_get_sync(data->dev);
ret = bmp580_nvmem_write_impl(priv, offset, val, bytes);
pm_runtime_mark_last_busy(data->dev); pm_runtime_mark_last_busy(data->dev);
pm_runtime_put_autosuspend(data->dev); pm_runtime_put_autosuspend(data->dev);
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment