[PATCH 06/11] net: dsa: ksz9477: create regmap cdev for switch registers

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 11 01:33:06 PST 2023


Hi,

On 11.01.23 09:56, Sascha Hauer wrote:
> On Wed, Jan 11, 2023 at 09:01:14AM +0100, Ahmad Fatoum wrote:
>> Now that we use regmap for the KSZ9477 driver, we can make the register
>> map available for introspection as a device file. As the KSZ driver has
>> a separate regmap for each of the three access sizes, we add a new
>> regmap_multi_register_cdev abstraction that multiplexes device file
>> access to the regmap with the best matching alignment.
>>
>> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
>> ---
>>  drivers/base/regmap/Makefile       |  1 +
>>  drivers/base/regmap/regmap-multi.c | 81 ++++++++++++++++++++++++++++++
>>  drivers/net/ksz9477.c              |  6 ++-
>>  include/regmap.h                   |  2 +
>>  4 files changed, 89 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/base/regmap/regmap-multi.c
>>
>> diff --git a/drivers/base/regmap/Makefile b/drivers/base/regmap/Makefile
>> index d99db4277149..fe5beaaaa382 100644
>> --- a/drivers/base/regmap/Makefile
>> +++ b/drivers/base/regmap/Makefile
>> @@ -1,5 +1,6 @@
>>  # SPDX-License-Identifier: GPL-2.0-only
>>  obj-y	+= regmap.o
>> +obj-y	+= regmap-multi.o
>>  obj-y	+= regmap-mmio.o
>>  obj-$(CONFIG_REGMAP_FORMATTED)	+= regmap-fmt.o
>>  obj-$(CONFIG_I2C)	+= regmap-i2c.o
>> diff --git a/drivers/base/regmap/regmap-multi.c b/drivers/base/regmap/regmap-multi.c
>> new file mode 100644
>> index 000000000000..79dd1ba9bd95
>> --- /dev/null
>> +++ b/drivers/base/regmap/regmap-multi.c
>> @@ -0,0 +1,81 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * Copyright 2022 Ahmad Fatoum <a.fatoum at pengutronix.de>
>> + */
>> +
>> +#include <common.h>
>> +#include <regmap.h>
>> +#include <linux/export.h>
>> +
>> +#include "internal.h"
>> +
>> +enum { MULTI_MAP_8, MULTI_MAP_16, MULTI_MAP_32, MULTI_MAP_COUNT };
>> +struct regmap_multi {
>> +	struct cdev cdev;
>> +	struct regmap *map[MULTI_MAP_COUNT];
>> +};
>> +
>> +static struct regmap *regmap_multi_cdev_get_map(struct cdev *cdev, size_t count)
>> +{
>> +	struct regmap_multi *multi = container_of(cdev, struct regmap_multi, cdev);
>> +	struct regmap *map = NULL;
>> +
>> +	if (count % 4 == 0)
>> +		map = multi->map[MULTI_MAP_32];
>> +	if (!map && count % 2 == 0)
>> +		map = multi->map[MULTI_MAP_16];
>> +	return map ?: multi->map[MULTI_MAP_8];
>> +}
> 
> You pick the regmap only based on 'count'. Shouldn't you take the offset
> into account as well?

I don't think so. This is a bit similar to mem_copy(), which underlies
the mw command. There we only use count as well and if offset is unaligned,
so be it.

> 
>> +
>> +static ssize_t regmap_multi_cdev_read(struct cdev *cdev, void *buf, size_t count,
>> +				      loff_t offset, unsigned long flags)
>> +{
>> +	struct regmap *map = regmap_multi_cdev_get_map(cdev, count);
>> +
>> +	return regmap_bulk_read(map, offset, buf, count) ?: count;
>> +}
>> +
>> +static ssize_t regmap_multi_cdev_write(struct cdev *cdev, const void *buf, size_t count,
>> +				       loff_t offset, unsigned long flags)
>> +{
>> +	struct regmap *map = regmap_multi_cdev_get_map(cdev, count);
>> +
>> +	return regmap_bulk_write(map, offset, buf, count) ?: count;
>> +}
>> +
>> +static struct cdev_operations regmap_multi_fops = {
>> +	.read	= regmap_multi_cdev_read,
>> +	.write	= regmap_multi_cdev_write,
>> +};
>> +
>> +int regmap_multi_register_cdev(struct regmap *maps[3])
> 
> MULTI_MAP_COUNT instead of 3?

I didn't want to define the symbol in the header, so kept this for
symmetry.

Cheers,
Ahmad

> 
> Sascha
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list