Mailing List Archive

[RFC PATCH 1/2] ASoC: soc-component: add snd_soc_component_read/write_field()
It's often the case that we would write or read a particular field
in register. With the current soc_component apis, reading a particular
field in register would involve first read the register and then
perform shift operations.

Ex:
to read from a field mask of 0xf0

val = snd_soc_component_read(component, reg);
field = ((val & 0xf0) >> 0x4);

This is sometimes prone to errors and code become less readable!

With this new api we could just do
field = snd_soc_component_read_field(component, reg, 0xf0);

this makes it bit simple, easy to write and less error prone!

This also applies to writing!

There are various places in kernel which provides such field interfaces
however soc_component seems to be missing this.

This patch is inspired by FIELD_GET/FIELD_PREP macros in include/linux/bitfield.h

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
include/sound/soc-component.h | 6 ++++
sound/soc/soc-component.c | 62 +++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+)

diff --git a/include/sound/soc-component.h b/include/sound/soc-component.h
index 0bce41fefd30..5b47768222b7 100644
--- a/include/sound/soc-component.h
+++ b/include/sound/soc-component.h
@@ -353,6 +353,12 @@ int snd_soc_component_test_bits(struct snd_soc_component *component,
unsigned int reg, unsigned int mask,
unsigned int value);

+unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
+ unsigned int reg, unsigned int mask);
+int snd_soc_component_write_field(struct snd_soc_component *component,
+ unsigned int reg, unsigned int mask,
+ unsigned int val);
+
/* component wide operations */
int snd_soc_component_set_sysclk(struct snd_soc_component *component,
int clk_id, int source,
diff --git a/sound/soc/soc-component.c b/sound/soc/soc-component.c
index 760523382f3c..6bdfc6f61ed6 100644
--- a/sound/soc/soc-component.c
+++ b/sound/soc/soc-component.c
@@ -12,6 +12,7 @@
#include <linux/pm_runtime.h>
#include <sound/soc.h>

+#define __soc_component_field_shift(x) (__builtin_ffs(x) - 1)
#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
static inline int _soc_component_ret(struct snd_soc_component *component,
const char *func, int ret)
@@ -839,6 +840,67 @@ int snd_soc_component_update_bits_async(struct snd_soc_component *component,
}
EXPORT_SYMBOL_GPL(snd_soc_component_update_bits_async);

+/**
+ * snd_soc_component_read_field() - Read register field value
+ * @component: Component to read from
+ * @reg: Register to read
+ * @mask: mask of the register field
+ *
+ * Return: read value of register field.
+ */
+unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
+ unsigned int reg, unsigned int mask)
+{
+ unsigned int val;
+
+ mutex_lock(&component->io_mutex);
+ val = soc_component_read_no_lock(component, reg);
+ if (mask)
+ val = (val & mask) >> __soc_component_field_shift(mask);
+ mutex_unlock(&component->io_mutex);
+
+ return val;
+}
+EXPORT_SYMBOL_GPL(snd_soc_component_read_field);
+
+/**
+ * snd_soc_component_write_field() - write to register field
+ * @component: Component to write to
+ * @reg: Register to write
+ * @mask: mask of the register field to update
+ * @val: value of the field to write
+ *
+ * Return: 1 for change, otherwise 0.
+ */
+int snd_soc_component_write_field(struct snd_soc_component *component,
+ unsigned int reg, unsigned int mask,
+ unsigned int val)
+{
+ unsigned int old, new;
+ int ret = 0;
+ bool change;
+
+ if (!mask)
+ return false;
+
+ mutex_lock(&component->io_mutex);
+
+ old = soc_component_read_no_lock(component, reg);
+
+ val = val << __soc_component_field_shift(mask);
+
+ new = (old & ~mask) | (val & mask);
+
+ change = old != new;
+ if (change)
+ ret = soc_component_write_no_lock(component, reg, new);
+
+ mutex_unlock(&component->io_mutex);
+
+ return change;
+}
+EXPORT_SYMBOL_GPL(snd_soc_component_write_field);
+
/**
* snd_soc_component_async_complete() - Ensure asynchronous I/O has completed
* @component: Component for which to wait
--
2.21.0
Re: [RFC PATCH 1/2] ASoC: soc-component: add snd_soc_component_read/write_field() [ In reply to ]
On Tue, Jan 26, 2021 at 12:20:19PM +0000, Srinivas Kandagatla wrote:

> +#define __soc_component_field_shift(x) (__builtin_ffs(x) - 1)

Why not have this be a static inline?

> +unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
> + unsigned int reg, unsigned int mask)
> +{
> + unsigned int val;
> +
> + mutex_lock(&component->io_mutex);
> + val = soc_component_read_no_lock(component, reg);
> + if (mask)
> + val = (val & mask) >> __soc_component_field_shift(mask);

I don't understand why this is open coding the locking when it's just a
simple read and then shift?

> + mutex_lock(&component->io_mutex);
> +
> + old = soc_component_read_no_lock(component, reg);
> +
> + val = val << __soc_component_field_shift(mask);
> +
> + new = (old & ~mask) | (val & mask);
> +
> + change = old != new;
> + if (change)
> + ret = soc_component_write_no_lock(component, reg, new);
> +
> + mutex_unlock(&component->io_mutex);

This needs the lock as it's a read/modify/write but could also be
implemented in terms of the existing update_bits() operation rather than
open coding it.
Re: [RFC PATCH 1/2] ASoC: soc-component: add snd_soc_component_read/write_field() [ In reply to ]
On 26/01/2021 13:36, Mark Brown wrote:
> On Tue, Jan 26, 2021 at 12:20:19PM +0000, Srinivas Kandagatla wrote:
>
>> +#define __soc_component_field_shift(x) (__builtin_ffs(x) - 1)
>
> Why not have this be a static inline?

Sure, that makes it even better to validate the mask aswell!

>
>> +unsigned int snd_soc_component_read_field(struct snd_soc_component *component,
>> + unsigned int reg, unsigned int mask)
>> +{
>> + unsigned int val;
>> +
>> + mutex_lock(&component->io_mutex);
>> + val = soc_component_read_no_lock(component, reg);
>> + if (mask)
>> + val = (val & mask) >> __soc_component_field_shift(mask);
>
> I don't understand why this is open coding the locking when it's just a
> simple read and then shift?

I agree! something like this should be good I guess:

unsigned int snd_soc_component_read_field(...)
{
unsigned int val;

val = snd_soc_component_read(component, reg);

val = (val & mask) >> __soc_component_field_shift(mask);

return val;
}

>
>> + mutex_lock(&component->io_mutex);
>> +
>> + old = soc_component_read_no_lock(component, reg);
>> +
>> + val = val << __soc_component_field_shift(mask);
>> +
>> + new = (old & ~mask) | (val & mask);
>> +
>> + change = old != new;
>> + if (change)
>> + ret = soc_component_write_no_lock(component, reg, new);
>> +
>> + mutex_unlock(&component->io_mutex);
>
> This needs the lock as it's a read/modify/write but could also be
> implemented in terms of the existing update_bits() operation rather than
> open coding it.
True!, we could simplify this to :

int snd_soc_component_write_field(struct snd_soc_component *component,
unsigned int reg, unsigned int mask,
unsigned int val)
{
val = (val << __soc_component_field_shift(mask)) & mask;

return snd_soc_component_update_bits(component, reg, mask, val);
}

Does that look okay to you?


--srini
>
Re: [RFC PATCH 1/2] ASoC: soc-component: add snd_soc_component_read/write_field() [ In reply to ]
On Tue, Jan 26, 2021 at 02:03:29PM +0000, Srinivas Kandagatla wrote:

> Does that look okay to you?

Yes, your proposed versions look good.