Commit 1de9c3c7 authored by Krzysztof Kozlowski's avatar Krzysztof Kozlowski Committed by Jonathan Cameron

iio: adc: ti-tsc2046: simplify with cleanup.h

Allocate the memory with scoped/cleanup.h to reduce error handling and
make the code a bit simpler.
Signed-off-by: default avatarKrzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Reviewed-by: default avatarOleksij Rempel <o.rempel@pengutronix.de>
Link: https://patch.msgid.link/20240705-cleanup-h-iio-v1-5-77114c7e84c5@linaro.orgSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent adcba9cd
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
#include <linux/bitfield.h> #include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/regulator/consumer.h> #include <linux/regulator/consumer.h>
...@@ -273,7 +274,6 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx, ...@@ -273,7 +274,6 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
u32 *effective_speed_hz) u32 *effective_speed_hz)
{ {
struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx]; struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
struct tsc2046_adc_atom *rx_buf, *tx_buf;
unsigned int val, val_normalized = 0; unsigned int val, val_normalized = 0;
int ret, i, count_skip = 0, max_count; int ret, i, count_skip = 0, max_count;
struct spi_transfer xfer; struct spi_transfer xfer;
...@@ -287,18 +287,20 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx, ...@@ -287,18 +287,20 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
max_count = 1; max_count = 1;
} }
if (sizeof(*tx_buf) * max_count > PAGE_SIZE) if (sizeof(struct tsc2046_adc_atom) * max_count > PAGE_SIZE)
return -ENOSPC; return -ENOSPC;
tx_buf = kcalloc(max_count, sizeof(*tx_buf), GFP_KERNEL); struct tsc2046_adc_atom *tx_buf __free(kfree) = kcalloc(max_count,
sizeof(*tx_buf),
GFP_KERNEL);
if (!tx_buf) if (!tx_buf)
return -ENOMEM; return -ENOMEM;
rx_buf = kcalloc(max_count, sizeof(*rx_buf), GFP_KERNEL); struct tsc2046_adc_atom *rx_buf __free(kfree) = kcalloc(max_count,
if (!rx_buf) { sizeof(*rx_buf),
ret = -ENOMEM; GFP_KERNEL);
goto free_tx; if (!rx_buf)
} return -ENOMEM;
/* /*
* Do not enable automatic power down on working samples. Otherwise the * Do not enable automatic power down on working samples. Otherwise the
...@@ -326,7 +328,7 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx, ...@@ -326,7 +328,7 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
if (ret) { if (ret) {
dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n", dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n",
ERR_PTR(ret)); ERR_PTR(ret));
goto free_bufs; return ret;
} }
if (effective_speed_hz) if (effective_speed_hz)
...@@ -337,14 +339,7 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx, ...@@ -337,14 +339,7 @@ static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
val_normalized += val; val_normalized += val;
} }
ret = DIV_ROUND_UP(val_normalized, max_count - count_skip); return DIV_ROUND_UP(val_normalized, max_count - count_skip);
free_bufs:
kfree(rx_buf);
free_tx:
kfree(tx_buf);
return ret;
} }
static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv, static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv,
......
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