Skip to content

Commit 493ea0c

Browse files
committed
Merge remote-tracking branch 'regmap/topic/bulk' into regmap-next
2 parents f981c6c + fb44f3c commit 493ea0c

1 file changed

Lines changed: 119 additions & 181 deletions

File tree

‎drivers/base/regmap/regmap.c‎

Lines changed: 119 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,8 @@ static void regmap_set_work_buf_flag_mask(struct regmap *map, int max_bytes,
14381438
buf[i] |= (mask >> (8 * i)) & 0xff;
14391439
}
14401440

1441-
int _regmap_raw_write(struct regmap *map, unsigned int reg,
1442-
const void *val, size_t val_len)
1441+
static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
1442+
const void *val, size_t val_len)
14431443
{
14441444
struct regmap_range_node *range;
14451445
unsigned long flags;
@@ -1490,8 +1490,9 @@ int _regmap_raw_write(struct regmap *map, unsigned int reg,
14901490
while (val_num > win_residue) {
14911491
dev_dbg(map->dev, "Writing window %d/%zu\n",
14921492
win_residue, val_len / map->format.val_bytes);
1493-
ret = _regmap_raw_write(map, reg, val, win_residue *
1494-
map->format.val_bytes);
1493+
ret = _regmap_raw_write_impl(map, reg, val,
1494+
win_residue *
1495+
map->format.val_bytes);
14951496
if (ret != 0)
14961497
return ret;
14971498

@@ -1707,11 +1708,11 @@ static int _regmap_bus_raw_write(void *context, unsigned int reg,
17071708

17081709
map->format.format_val(map->work_buf + map->format.reg_bytes
17091710
+ map->format.pad_bytes, val, 0);
1710-
return _regmap_raw_write(map, reg,
1711-
map->work_buf +
1712-
map->format.reg_bytes +
1713-
map->format.pad_bytes,
1714-
map->format.val_bytes);
1711+
return _regmap_raw_write_impl(map, reg,
1712+
map->work_buf +
1713+
map->format.reg_bytes +
1714+
map->format.pad_bytes,
1715+
map->format.val_bytes);
17151716
}
17161717

17171718
static inline void *_regmap_map_get_context(struct regmap *map)
@@ -1806,6 +1807,44 @@ int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
18061807
}
18071808
EXPORT_SYMBOL_GPL(regmap_write_async);
18081809

1810+
int _regmap_raw_write(struct regmap *map, unsigned int reg,
1811+
const void *val, size_t val_len)
1812+
{
1813+
size_t val_bytes = map->format.val_bytes;
1814+
size_t val_count = val_len / val_bytes;
1815+
size_t chunk_count, chunk_bytes;
1816+
size_t chunk_regs = val_count;
1817+
int ret, i;
1818+
1819+
if (!val_count)
1820+
return -EINVAL;
1821+
1822+
if (map->use_single_write)
1823+
chunk_regs = 1;
1824+
else if (map->max_raw_write && val_len > map->max_raw_write)
1825+
chunk_regs = map->max_raw_write / val_bytes;
1826+
1827+
chunk_count = val_count / chunk_regs;
1828+
chunk_bytes = chunk_regs * val_bytes;
1829+
1830+
/* Write as many bytes as possible with chunk_size */
1831+
for (i = 0; i < chunk_count; i++) {
1832+
ret = _regmap_raw_write_impl(map, reg, val, chunk_bytes);
1833+
if (ret)
1834+
return ret;
1835+
1836+
reg += regmap_get_offset(map, chunk_regs);
1837+
val += chunk_bytes;
1838+
val_len -= chunk_bytes;
1839+
}
1840+
1841+
/* Write remaining bytes */
1842+
if (val_len)
1843+
ret = _regmap_raw_write_impl(map, reg, val, val_len);
1844+
1845+
return ret;
1846+
}
1847+
18091848
/**
18101849
* regmap_raw_write() - Write raw values to one or more registers
18111850
*
@@ -1831,8 +1870,6 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
18311870
return -EINVAL;
18321871
if (val_len % map->format.val_bytes)
18331872
return -EINVAL;
1834-
if (map->max_raw_write && map->max_raw_write < val_len)
1835-
return -E2BIG;
18361873

18371874
map->lock(map->lock_arg);
18381875

@@ -1923,23 +1960,15 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
19231960
{
19241961
int ret = 0, i;
19251962
size_t val_bytes = map->format.val_bytes;
1926-
size_t total_size = val_bytes * val_count;
19271963

19281964
if (!IS_ALIGNED(reg, map->reg_stride))
19291965
return -EINVAL;
19301966

19311967
/*
1932-
* Some devices don't support bulk write, for
1933-
* them we have a series of single write operations in the first two if
1934-
* blocks.
1935-
*
1936-
* The first if block is used for memory mapped io. It does not allow
1937-
* val_bytes of 3 for example.
1938-
* The second one is for busses that do not provide raw I/O.
1939-
* The third one is used for busses which do not have these limitations
1940-
* and can write arbitrary value lengths.
1968+
* Some devices don't support bulk write, for them we have a series of
1969+
* single write operations.
19411970
*/
1942-
if (!map->bus) {
1971+
if (!map->bus || !map->format.parse_inplace) {
19431972
map->lock(map->lock_arg);
19441973
for (i = 0; i < val_count; i++) {
19451974
unsigned int ival;
@@ -1972,81 +2001,17 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
19722001
}
19732002
out:
19742003
map->unlock(map->lock_arg);
1975-
} else if (map->bus && !map->format.parse_inplace) {
1976-
const u8 *u8 = val;
1977-
const u16 *u16 = val;
1978-
const u32 *u32 = val;
1979-
unsigned int ival;
1980-
1981-
for (i = 0; i < val_count; i++) {
1982-
switch (map->format.val_bytes) {
1983-
case 4:
1984-
ival = u32[i];
1985-
break;
1986-
case 2:
1987-
ival = u16[i];
1988-
break;
1989-
case 1:
1990-
ival = u8[i];
1991-
break;
1992-
default:
1993-
return -EINVAL;
1994-
}
1995-
1996-
ret = regmap_write(map, reg + (i * map->reg_stride),
1997-
ival);
1998-
if (ret)
1999-
return ret;
2000-
}
2001-
} else if (map->use_single_write ||
2002-
(map->max_raw_write && map->max_raw_write < total_size)) {
2003-
int chunk_stride = map->reg_stride;
2004-
size_t chunk_size = val_bytes;
2005-
size_t chunk_count = val_count;
2006-
2007-
if (!map->use_single_write) {
2008-
chunk_size = map->max_raw_write;
2009-
if (chunk_size % val_bytes)
2010-
chunk_size -= chunk_size % val_bytes;
2011-
chunk_count = total_size / chunk_size;
2012-
chunk_stride *= chunk_size / val_bytes;
2013-
}
2014-
2015-
map->lock(map->lock_arg);
2016-
/* Write as many bytes as possible with chunk_size */
2017-
for (i = 0; i < chunk_count; i++) {
2018-
ret = _regmap_raw_write(map,
2019-
reg + (i * chunk_stride),
2020-
val + (i * chunk_size),
2021-
chunk_size);
2022-
if (ret)
2023-
break;
2024-
}
2025-
2026-
/* Write remaining bytes */
2027-
if (!ret && chunk_size * i < total_size) {
2028-
ret = _regmap_raw_write(map, reg + (i * chunk_stride),
2029-
val + (i * chunk_size),
2030-
total_size - i * chunk_size);
2031-
}
2032-
map->unlock(map->lock_arg);
20332004
} else {
20342005
void *wval;
20352006

2036-
if (!val_count)
2037-
return -EINVAL;
2038-
20392007
wval = kmemdup(val, val_count * val_bytes, map->alloc_flags);
2040-
if (!wval) {
2041-
dev_err(map->dev, "Error in memory allocation\n");
2008+
if (!wval)
20422009
return -ENOMEM;
2043-
}
2010+
20442011
for (i = 0; i < val_count * val_bytes; i += val_bytes)
20452012
map->format.parse_inplace(wval + i);
20462013

2047-
map->lock(map->lock_arg);
2048-
ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
2049-
map->unlock(map->lock_arg);
2014+
ret = regmap_raw_write(map, reg, wval, val_bytes * val_count);
20502015

20512016
kfree(wval);
20522017
}
@@ -2542,18 +2507,39 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
25422507

25432508
if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
25442509
map->cache_type == REGCACHE_NONE) {
2510+
size_t chunk_count, chunk_bytes;
2511+
size_t chunk_regs = val_count;
2512+
25452513
if (!map->bus->read) {
25462514
ret = -ENOTSUPP;
25472515
goto out;
25482516
}
2549-
if (map->max_raw_read && map->max_raw_read < val_len) {
2550-
ret = -E2BIG;
2551-
goto out;
2552-
}
25532517

2554-
/* Physical block read if there's no cache involved */
2555-
ret = _regmap_raw_read(map, reg, val, val_len);
2518+
if (map->use_single_read)
2519+
chunk_regs = 1;
2520+
else if (map->max_raw_read && val_len > map->max_raw_read)
2521+
chunk_regs = map->max_raw_read / val_bytes;
2522+
2523+
chunk_count = val_count / chunk_regs;
2524+
chunk_bytes = chunk_regs * val_bytes;
2525+
2526+
/* Read bytes that fit into whole chunks */
2527+
for (i = 0; i < chunk_count; i++) {
2528+
ret = _regmap_raw_read(map, reg, val, chunk_bytes);
2529+
if (ret != 0)
2530+
goto out;
2531+
2532+
reg += regmap_get_offset(map, chunk_regs);
2533+
val += chunk_bytes;
2534+
val_len -= chunk_bytes;
2535+
}
25562536

2537+
/* Read remaining bytes */
2538+
if (val_len) {
2539+
ret = _regmap_raw_read(map, reg, val, val_len);
2540+
if (ret != 0)
2541+
goto out;
2542+
}
25572543
} else {
25582544
/* Otherwise go word by word for the cache; should be low
25592545
* cost as we expect to hit the cache.
@@ -2653,108 +2639,60 @@ int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
26532639

26542640
if (!IS_ALIGNED(reg, map->reg_stride))
26552641
return -EINVAL;
2642+
if (val_count == 0)
2643+
return -EINVAL;
26562644

26572645
if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
2658-
/*
2659-
* Some devices does not support bulk read, for
2660-
* them we have a series of single read operations.
2661-
*/
2662-
size_t total_size = val_bytes * val_count;
2663-
2664-
if (!map->use_single_read &&
2665-
(!map->max_raw_read || map->max_raw_read > total_size)) {
2666-
ret = regmap_raw_read(map, reg, val,
2667-
val_bytes * val_count);
2668-
if (ret != 0)
2669-
return ret;
2670-
} else {
2671-
/*
2672-
* Some devices do not support bulk read or do not
2673-
* support large bulk reads, for them we have a series
2674-
* of read operations.
2675-
*/
2676-
int chunk_stride = map->reg_stride;
2677-
size_t chunk_size = val_bytes;
2678-
size_t chunk_count = val_count;
2679-
2680-
if (!map->use_single_read) {
2681-
chunk_size = map->max_raw_read;
2682-
if (chunk_size % val_bytes)
2683-
chunk_size -= chunk_size % val_bytes;
2684-
chunk_count = total_size / chunk_size;
2685-
chunk_stride *= chunk_size / val_bytes;
2686-
}
2687-
2688-
/* Read bytes that fit into a multiple of chunk_size */
2689-
for (i = 0; i < chunk_count; i++) {
2690-
ret = regmap_raw_read(map,
2691-
reg + (i * chunk_stride),
2692-
val + (i * chunk_size),
2693-
chunk_size);
2694-
if (ret != 0)
2695-
return ret;
2696-
}
2697-
2698-
/* Read remaining bytes */
2699-
if (chunk_size * i < total_size) {
2700-
ret = regmap_raw_read(map,
2701-
reg + (i * chunk_stride),
2702-
val + (i * chunk_size),
2703-
total_size - i * chunk_size);
2704-
if (ret != 0)
2705-
return ret;
2706-
}
2707-
}
2646+
ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
2647+
if (ret != 0)
2648+
return ret;
27082649

27092650
for (i = 0; i < val_count * val_bytes; i += val_bytes)
27102651
map->format.parse_inplace(val + i);
27112652
} else {
2653+
#ifdef CONFIG_64BIT
2654+
u64 *u64 = val;
2655+
#endif
2656+
u32 *u32 = val;
2657+
u16 *u16 = val;
2658+
u8 *u8 = val;
2659+
2660+
map->lock(map->lock_arg);
2661+
27122662
for (i = 0; i < val_count; i++) {
27132663
unsigned int ival;
2714-
ret = regmap_read(map, reg + regmap_get_offset(map, i),
2715-
&ival);
2716-
if (ret != 0)
2717-
return ret;
27182664

2719-
if (map->format.format_val) {
2720-
map->format.format_val(val + (i * val_bytes), ival, 0);
2721-
} else {
2722-
/* Devices providing read and write
2723-
* operations can use the bulk I/O
2724-
* functions if they define a val_bytes,
2725-
* we assume that the values are native
2726-
* endian.
2727-
*/
2728-
#ifdef CONFIG_64BIT
2729-
u64 *u64 = val;
2730-
#endif
2731-
u32 *u32 = val;
2732-
u16 *u16 = val;
2733-
u8 *u8 = val;
2665+
ret = _regmap_read(map, reg + regmap_get_offset(map, i),
2666+
&ival);
2667+
if (ret != 0)
2668+
goto out;
27342669

2735-
switch (map->format.val_bytes) {
2670+
switch (map->format.val_bytes) {
27362671
#ifdef CONFIG_64BIT
2737-
case 8:
2738-
u64[i] = ival;
2739-
break;
2672+
case 8:
2673+
u64[i] = ival;
2674+
break;
27402675
#endif
2741-
case 4:
2742-
u32[i] = ival;
2743-
break;
2744-
case 2:
2745-
u16[i] = ival;
2746-
break;
2747-
case 1:
2748-
u8[i] = ival;
2749-
break;
2750-
default:
2751-
return -EINVAL;
2752-
}
2676+
case 4:
2677+
u32[i] = ival;
2678+
break;
2679+
case 2:
2680+
u16[i] = ival;
2681+
break;
2682+
case 1:
2683+
u8[i] = ival;
2684+
break;
2685+
default:
2686+
ret = -EINVAL;
2687+
goto out;
27532688
}
27542689
}
2690+
2691+
out:
2692+
map->unlock(map->lock_arg);
27552693
}
27562694

2757-
return 0;
2695+
return ret;
27582696
}
27592697
EXPORT_SYMBOL_GPL(regmap_bulk_read);
27602698

0 commit comments

Comments
 (0)