aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/regmap/regmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base/regmap/regmap.c')
-rw-r--r--drivers/base/regmap/regmap.c294
1 files changed, 260 insertions, 34 deletions
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 7d689a15c500..ccdac61ac5e2 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -42,15 +42,6 @@ static int _regmap_bus_formatted_write(void *context, unsigned int reg,
42static int _regmap_bus_raw_write(void *context, unsigned int reg, 42static int _regmap_bus_raw_write(void *context, unsigned int reg,
43 unsigned int val); 43 unsigned int val);
44 44
45static void async_cleanup(struct work_struct *work)
46{
47 struct regmap_async *async = container_of(work, struct regmap_async,
48 cleanup);
49
50 kfree(async->work_buf);
51 kfree(async);
52}
53
54bool regmap_reg_in_ranges(unsigned int reg, 45bool regmap_reg_in_ranges(unsigned int reg,
55 const struct regmap_range *ranges, 46 const struct regmap_range *ranges,
56 unsigned int nranges) 47 unsigned int nranges)
@@ -465,6 +456,7 @@ struct regmap *regmap_init(struct device *dev,
465 456
466 spin_lock_init(&map->async_lock); 457 spin_lock_init(&map->async_lock);
467 INIT_LIST_HEAD(&map->async_list); 458 INIT_LIST_HEAD(&map->async_list);
459 INIT_LIST_HEAD(&map->async_free);
468 init_waitqueue_head(&map->async_waitq); 460 init_waitqueue_head(&map->async_waitq);
469 461
470 if (config->read_flag_mask || config->write_flag_mask) { 462 if (config->read_flag_mask || config->write_flag_mask) {
@@ -821,6 +813,8 @@ static void regmap_field_init(struct regmap_field *rm_field,
821 rm_field->reg = reg_field.reg; 813 rm_field->reg = reg_field.reg;
822 rm_field->shift = reg_field.lsb; 814 rm_field->shift = reg_field.lsb;
823 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb); 815 rm_field->mask = ((BIT(field_bits) - 1) << reg_field.lsb);
816 rm_field->id_size = reg_field.id_size;
817 rm_field->id_offset = reg_field.id_offset;
824} 818}
825 819
826/** 820/**
@@ -942,12 +936,22 @@ EXPORT_SYMBOL_GPL(regmap_reinit_cache);
942 */ 936 */
943void regmap_exit(struct regmap *map) 937void regmap_exit(struct regmap *map)
944{ 938{
939 struct regmap_async *async;
940
945 regcache_exit(map); 941 regcache_exit(map);
946 regmap_debugfs_exit(map); 942 regmap_debugfs_exit(map);
947 regmap_range_exit(map); 943 regmap_range_exit(map);
948 if (map->bus && map->bus->free_context) 944 if (map->bus && map->bus->free_context)
949 map->bus->free_context(map->bus_context); 945 map->bus->free_context(map->bus_context);
950 kfree(map->work_buf); 946 kfree(map->work_buf);
947 while (!list_empty(&map->async_free)) {
948 async = list_first_entry_or_null(&map->async_free,
949 struct regmap_async,
950 list);
951 list_del(&async->list);
952 kfree(async->work_buf);
953 kfree(async);
954 }
951 kfree(map); 955 kfree(map);
952} 956}
953EXPORT_SYMBOL_GPL(regmap_exit); 957EXPORT_SYMBOL_GPL(regmap_exit);
@@ -1039,7 +1043,7 @@ static int _regmap_select_page(struct regmap *map, unsigned int *reg,
1039} 1043}
1040 1044
1041int _regmap_raw_write(struct regmap *map, unsigned int reg, 1045int _regmap_raw_write(struct regmap *map, unsigned int reg,
1042 const void *val, size_t val_len, bool async) 1046 const void *val, size_t val_len)
1043{ 1047{
1044 struct regmap_range_node *range; 1048 struct regmap_range_node *range;
1045 unsigned long flags; 1049 unsigned long flags;
@@ -1091,7 +1095,7 @@ int _regmap_raw_write(struct regmap *map, unsigned int reg,
1091 dev_dbg(map->dev, "Writing window %d/%zu\n", 1095 dev_dbg(map->dev, "Writing window %d/%zu\n",
1092 win_residue, val_len / map->format.val_bytes); 1096 win_residue, val_len / map->format.val_bytes);
1093 ret = _regmap_raw_write(map, reg, val, win_residue * 1097 ret = _regmap_raw_write(map, reg, val, win_residue *
1094 map->format.val_bytes, async); 1098 map->format.val_bytes);
1095 if (ret != 0) 1099 if (ret != 0)
1096 return ret; 1100 return ret;
1097 1101
@@ -1114,21 +1118,42 @@ int _regmap_raw_write(struct regmap *map, unsigned int reg,
1114 1118
1115 u8[0] |= map->write_flag_mask; 1119 u8[0] |= map->write_flag_mask;
1116 1120
1117 if (async && map->bus->async_write) { 1121 /*
1118 struct regmap_async *async = map->bus->async_alloc(); 1122 * Essentially all I/O mechanisms will be faster with a single
1119 if (!async) 1123 * buffer to write. Since register syncs often generate raw
1120 return -ENOMEM; 1124 * writes of single registers optimise that case.
1125 */
1126 if (val != work_val && val_len == map->format.val_bytes) {
1127 memcpy(work_val, val, map->format.val_bytes);
1128 val = work_val;
1129 }
1130
1131 if (map->async && map->bus->async_write) {
1132 struct regmap_async *async;
1121 1133
1122 trace_regmap_async_write_start(map->dev, reg, val_len); 1134 trace_regmap_async_write_start(map->dev, reg, val_len);
1123 1135
1124 async->work_buf = kzalloc(map->format.buf_size, 1136 spin_lock_irqsave(&map->async_lock, flags);
1125 GFP_KERNEL | GFP_DMA); 1137 async = list_first_entry_or_null(&map->async_free,
1126 if (!async->work_buf) { 1138 struct regmap_async,
1127 kfree(async); 1139 list);
1128 return -ENOMEM; 1140 if (async)
1141 list_del(&async->list);
1142 spin_unlock_irqrestore(&map->async_lock, flags);
1143
1144 if (!async) {
1145 async = map->bus->async_alloc();
1146 if (!async)
1147 return -ENOMEM;
1148
1149 async->work_buf = kzalloc(map->format.buf_size,
1150 GFP_KERNEL | GFP_DMA);
1151 if (!async->work_buf) {
1152 kfree(async);
1153 return -ENOMEM;
1154 }
1129 } 1155 }
1130 1156
1131 INIT_WORK(&async->cleanup, async_cleanup);
1132 async->map = map; 1157 async->map = map;
1133 1158
1134 /* If the caller supplied the value we can use it safely. */ 1159 /* If the caller supplied the value we can use it safely. */
@@ -1152,11 +1177,8 @@ int _regmap_raw_write(struct regmap *map, unsigned int reg,
1152 ret); 1177 ret);
1153 1178
1154 spin_lock_irqsave(&map->async_lock, flags); 1179 spin_lock_irqsave(&map->async_lock, flags);
1155 list_del(&async->list); 1180 list_move(&async->list, &map->async_free);
1156 spin_unlock_irqrestore(&map->async_lock, flags); 1181 spin_unlock_irqrestore(&map->async_lock, flags);
1157
1158 kfree(async->work_buf);
1159 kfree(async);
1160 } 1182 }
1161 1183
1162 return ret; 1184 return ret;
@@ -1253,7 +1275,7 @@ static int _regmap_bus_raw_write(void *context, unsigned int reg,
1253 map->work_buf + 1275 map->work_buf +
1254 map->format.reg_bytes + 1276 map->format.reg_bytes +
1255 map->format.pad_bytes, 1277 map->format.pad_bytes,
1256 map->format.val_bytes, false); 1278 map->format.val_bytes);
1257} 1279}
1258 1280
1259static inline void *_regmap_map_get_context(struct regmap *map) 1281static inline void *_regmap_map_get_context(struct regmap *map)
@@ -1318,6 +1340,37 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
1318EXPORT_SYMBOL_GPL(regmap_write); 1340EXPORT_SYMBOL_GPL(regmap_write);
1319 1341
1320/** 1342/**
1343 * regmap_write_async(): Write a value to a single register asynchronously
1344 *
1345 * @map: Register map to write to
1346 * @reg: Register to write to
1347 * @val: Value to be written
1348 *
1349 * A value of zero will be returned on success, a negative errno will
1350 * be returned in error cases.
1351 */
1352int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
1353{
1354 int ret;
1355
1356 if (reg % map->reg_stride)
1357 return -EINVAL;
1358
1359 map->lock(map->lock_arg);
1360
1361 map->async = true;
1362
1363 ret = _regmap_write(map, reg, val);
1364
1365 map->async = false;
1366
1367 map->unlock(map->lock_arg);
1368
1369 return ret;
1370}
1371EXPORT_SYMBOL_GPL(regmap_write_async);
1372
1373/**
1321 * regmap_raw_write(): Write raw values to one or more registers 1374 * regmap_raw_write(): Write raw values to one or more registers
1322 * 1375 *
1323 * @map: Register map to write to 1376 * @map: Register map to write to
@@ -1345,7 +1398,7 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
1345 1398
1346 map->lock(map->lock_arg); 1399 map->lock(map->lock_arg);
1347 1400
1348 ret = _regmap_raw_write(map, reg, val, val_len, false); 1401 ret = _regmap_raw_write(map, reg, val, val_len);
1349 1402
1350 map->unlock(map->lock_arg); 1403 map->unlock(map->lock_arg);
1351 1404
@@ -1369,6 +1422,74 @@ int regmap_field_write(struct regmap_field *field, unsigned int val)
1369} 1422}
1370EXPORT_SYMBOL_GPL(regmap_field_write); 1423EXPORT_SYMBOL_GPL(regmap_field_write);
1371 1424
1425/**
1426 * regmap_field_update_bits(): Perform a read/modify/write cycle
1427 * on the register field
1428 *
1429 * @field: Register field to write to
1430 * @mask: Bitmask to change
1431 * @val: Value to be written
1432 *
1433 * A value of zero will be returned on success, a negative errno will
1434 * be returned in error cases.
1435 */
1436int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
1437{
1438 mask = (mask << field->shift) & field->mask;
1439
1440 return regmap_update_bits(field->regmap, field->reg,
1441 mask, val << field->shift);
1442}
1443EXPORT_SYMBOL_GPL(regmap_field_update_bits);
1444
1445/**
1446 * regmap_fields_write(): Write a value to a single register field with port ID
1447 *
1448 * @field: Register field to write to
1449 * @id: port ID
1450 * @val: Value to be written
1451 *
1452 * A value of zero will be returned on success, a negative errno will
1453 * be returned in error cases.
1454 */
1455int regmap_fields_write(struct regmap_field *field, unsigned int id,
1456 unsigned int val)
1457{
1458 if (id >= field->id_size)
1459 return -EINVAL;
1460
1461 return regmap_update_bits(field->regmap,
1462 field->reg + (field->id_offset * id),
1463 field->mask, val << field->shift);
1464}
1465EXPORT_SYMBOL_GPL(regmap_fields_write);
1466
1467/**
1468 * regmap_fields_update_bits(): Perform a read/modify/write cycle
1469 * on the register field
1470 *
1471 * @field: Register field to write to
1472 * @id: port ID
1473 * @mask: Bitmask to change
1474 * @val: Value to be written
1475 *
1476 * A value of zero will be returned on success, a negative errno will
1477 * be returned in error cases.
1478 */
1479int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
1480 unsigned int mask, unsigned int val)
1481{
1482 if (id >= field->id_size)
1483 return -EINVAL;
1484
1485 mask = (mask << field->shift) & field->mask;
1486
1487 return regmap_update_bits(field->regmap,
1488 field->reg + (field->id_offset * id),
1489 mask, val << field->shift);
1490}
1491EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
1492
1372/* 1493/*
1373 * regmap_bulk_write(): Write multiple registers to the device 1494 * regmap_bulk_write(): Write multiple registers to the device
1374 * 1495 *
@@ -1426,8 +1547,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
1426 return ret; 1547 return ret;
1427 } 1548 }
1428 } else { 1549 } else {
1429 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count, 1550 ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
1430 false);
1431 } 1551 }
1432 1552
1433 if (val_bytes != 1) 1553 if (val_bytes != 1)
@@ -1473,7 +1593,11 @@ int regmap_raw_write_async(struct regmap *map, unsigned int reg,
1473 1593
1474 map->lock(map->lock_arg); 1594 map->lock(map->lock_arg);
1475 1595
1476 ret = _regmap_raw_write(map, reg, val, val_len, true); 1596 map->async = true;
1597
1598 ret = _regmap_raw_write(map, reg, val, val_len);
1599
1600 map->async = false;
1477 1601
1478 map->unlock(map->lock_arg); 1602 map->unlock(map->lock_arg);
1479 1603
@@ -1677,6 +1801,39 @@ int regmap_field_read(struct regmap_field *field, unsigned int *val)
1677EXPORT_SYMBOL_GPL(regmap_field_read); 1801EXPORT_SYMBOL_GPL(regmap_field_read);
1678 1802
1679/** 1803/**
1804 * regmap_fields_read(): Read a value to a single register field with port ID
1805 *
1806 * @field: Register field to read from
1807 * @id: port ID
1808 * @val: Pointer to store read value
1809 *
1810 * A value of zero will be returned on success, a negative errno will
1811 * be returned in error cases.
1812 */
1813int regmap_fields_read(struct regmap_field *field, unsigned int id,
1814 unsigned int *val)
1815{
1816 int ret;
1817 unsigned int reg_val;
1818
1819 if (id >= field->id_size)
1820 return -EINVAL;
1821
1822 ret = regmap_read(field->regmap,
1823 field->reg + (field->id_offset * id),
1824 &reg_val);
1825 if (ret != 0)
1826 return ret;
1827
1828 reg_val &= field->mask;
1829 reg_val >>= field->shift;
1830 *val = reg_val;
1831
1832 return ret;
1833}
1834EXPORT_SYMBOL_GPL(regmap_fields_read);
1835
1836/**
1680 * regmap_bulk_read(): Read multiple registers from the device 1837 * regmap_bulk_read(): Read multiple registers from the device
1681 * 1838 *
1682 * @map: Register map to write to 1839 * @map: Register map to write to
@@ -1788,6 +1945,41 @@ int regmap_update_bits(struct regmap *map, unsigned int reg,
1788EXPORT_SYMBOL_GPL(regmap_update_bits); 1945EXPORT_SYMBOL_GPL(regmap_update_bits);
1789 1946
1790/** 1947/**
1948 * regmap_update_bits_async: Perform a read/modify/write cycle on the register
1949 * map asynchronously
1950 *
1951 * @map: Register map to update
1952 * @reg: Register to update
1953 * @mask: Bitmask to change
1954 * @val: New value for bitmask
1955 *
1956 * With most buses the read must be done synchronously so this is most
1957 * useful for devices with a cache which do not need to interact with
1958 * the hardware to determine the current register value.
1959 *
1960 * Returns zero for success, a negative number on error.
1961 */
1962int regmap_update_bits_async(struct regmap *map, unsigned int reg,
1963 unsigned int mask, unsigned int val)
1964{
1965 bool change;
1966 int ret;
1967
1968 map->lock(map->lock_arg);
1969
1970 map->async = true;
1971
1972 ret = _regmap_update_bits(map, reg, mask, val, &change);
1973
1974 map->async = false;
1975
1976 map->unlock(map->lock_arg);
1977
1978 return ret;
1979}
1980EXPORT_SYMBOL_GPL(regmap_update_bits_async);
1981
1982/**
1791 * regmap_update_bits_check: Perform a read/modify/write cycle on the 1983 * regmap_update_bits_check: Perform a read/modify/write cycle on the
1792 * register map and report if updated 1984 * register map and report if updated
1793 * 1985 *
@@ -1812,6 +2004,43 @@ int regmap_update_bits_check(struct regmap *map, unsigned int reg,
1812} 2004}
1813EXPORT_SYMBOL_GPL(regmap_update_bits_check); 2005EXPORT_SYMBOL_GPL(regmap_update_bits_check);
1814 2006
2007/**
2008 * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
2009 * register map asynchronously and report if
2010 * updated
2011 *
2012 * @map: Register map to update
2013 * @reg: Register to update
2014 * @mask: Bitmask to change
2015 * @val: New value for bitmask
2016 * @change: Boolean indicating if a write was done
2017 *
2018 * With most buses the read must be done synchronously so this is most
2019 * useful for devices with a cache which do not need to interact with
2020 * the hardware to determine the current register value.
2021 *
2022 * Returns zero for success, a negative number on error.
2023 */
2024int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
2025 unsigned int mask, unsigned int val,
2026 bool *change)
2027{
2028 int ret;
2029
2030 map->lock(map->lock_arg);
2031
2032 map->async = true;
2033
2034 ret = _regmap_update_bits(map, reg, mask, val, change);
2035
2036 map->async = false;
2037
2038 map->unlock(map->lock_arg);
2039
2040 return ret;
2041}
2042EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
2043
1815void regmap_async_complete_cb(struct regmap_async *async, int ret) 2044void regmap_async_complete_cb(struct regmap_async *async, int ret)
1816{ 2045{
1817 struct regmap *map = async->map; 2046 struct regmap *map = async->map;
@@ -1820,8 +2049,7 @@ void regmap_async_complete_cb(struct regmap_async *async, int ret)
1820 trace_regmap_async_io_complete(map->dev); 2049 trace_regmap_async_io_complete(map->dev);
1821 2050
1822 spin_lock(&map->async_lock); 2051 spin_lock(&map->async_lock);
1823 2052 list_move(&async->list, &map->async_free);
1824 list_del(&async->list);
1825 wake = list_empty(&map->async_list); 2053 wake = list_empty(&map->async_list);
1826 2054
1827 if (ret != 0) 2055 if (ret != 0)
@@ -1829,8 +2057,6 @@ void regmap_async_complete_cb(struct regmap_async *async, int ret)
1829 2057
1830 spin_unlock(&map->async_lock); 2058 spin_unlock(&map->async_lock);
1831 2059
1832 schedule_work(&async->cleanup);
1833
1834 if (wake) 2060 if (wake)
1835 wake_up(&map->async_waitq); 2061 wake_up(&map->async_waitq);
1836} 2062}