regmap: Customisable lock functions

Allow maps to replace the lock function for integration with dynamic
 register accessability states.
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.12 (GNU/Linux)
 
 iQIcBAABAgAGBQJQqsx5AAoJELSic+t+oim9sckP/0qRe75K6Zb+XurzW1xh2jhg
 esI+C9vu/WrJmYN4zGnNytMr/i8v7jQYSFib4aSMcOLHUBYMkqiHlL1unW2lcNNA
 L8E/Dvp/aKCsFuVNb6XlSI0Bio1HtctL5wWOS1fltLp9oohdf6nMrdzs0ghwXUMA
 t0tzKYb9h7aLpbx2OLYdnw06/S9/9Myw+l5byrWJOFV8SGqFee7nchLrhNbDKTqZ
 4nU3r51SG7STWrEZgbFwhZloceZbEZj4d5g5TunAGj739K9AWLfREjp1T3XkU7iu
 a12rEUYhycLAXYTqunFLNKA+ggQH3nCSfZH7w8iyII63sWWOvSiEyomugbh12VpZ
 HjGaR2xhFD+T+LnjT7alKrGicZcofzWb+gXN7hn73kWSml8FDFlfdVX0YYnkRZwd
 1ToXQDs6nGJWVH7lxogoHIm55rzs2Y7QkuoOLVF6N+UTkxYrirwUt1ta5v0HXfDA
 BuYL5ybLw4qAPwV/Q/u29r84XUTeJtZq/DfBscDJZpEXryzoDDu7pOs/KkHX6IJ7
 fJbeGFaduQ5FI+RTWwY6WUqMC/qLQ/Ec9vwjiJEzxoNnWqc4kkELVWoohwjBYkqw
 rE4oUsT7Px2fS0KMbark3tNkt0uvPnYHn9G5olxPk0S2rC0NHrukSGwtEh5cLdqx
 htEX1m8hDfs7ZfweO6My
 =KWk0
 -----END PGP SIGNATURE-----

Merge tag 'lock' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap into for-next

regmap: Customisable lock functions

Allow maps to replace the lock function for integration with dynamic
register accessability states.
This commit is contained in:
Samuel Ortiz 2012-11-20 12:20:58 +01:00
commit 7aae79fd88
3 changed files with 54 additions and 31 deletions

View File

@ -31,14 +31,12 @@ struct regmap_format {
unsigned int (*parse_val)(void *buf); unsigned int (*parse_val)(void *buf);
}; };
typedef void (*regmap_lock)(struct regmap *map);
typedef void (*regmap_unlock)(struct regmap *map);
struct regmap { struct regmap {
struct mutex mutex; struct mutex mutex;
spinlock_t spinlock; spinlock_t spinlock;
regmap_lock lock; regmap_lock lock;
regmap_unlock unlock; regmap_unlock unlock;
void *lock_arg; /* This is passed to lock/unlock functions */
struct device *dev; /* Device we do I/O on */ struct device *dev; /* Device we do I/O on */
void *work_buf; /* Scratch buffer used to format I/O */ void *work_buf; /* Scratch buffer used to format I/O */

View File

@ -214,23 +214,27 @@ static unsigned int regmap_parse_32_native(void *buf)
return *(u32 *)buf; return *(u32 *)buf;
} }
static void regmap_lock_mutex(struct regmap *map) static void regmap_lock_mutex(void *__map)
{ {
struct regmap *map = __map;
mutex_lock(&map->mutex); mutex_lock(&map->mutex);
} }
static void regmap_unlock_mutex(struct regmap *map) static void regmap_unlock_mutex(void *__map)
{ {
struct regmap *map = __map;
mutex_unlock(&map->mutex); mutex_unlock(&map->mutex);
} }
static void regmap_lock_spinlock(struct regmap *map) static void regmap_lock_spinlock(void *__map)
{ {
struct regmap *map = __map;
spin_lock(&map->spinlock); spin_lock(&map->spinlock);
} }
static void regmap_unlock_spinlock(struct regmap *map) static void regmap_unlock_spinlock(void *__map)
{ {
struct regmap *map = __map;
spin_unlock(&map->spinlock); spin_unlock(&map->spinlock);
} }
@ -335,14 +339,21 @@ struct regmap *regmap_init(struct device *dev,
goto err; goto err;
} }
if (bus->fast_io) { if (config->lock && config->unlock) {
spin_lock_init(&map->spinlock); map->lock = config->lock;
map->lock = regmap_lock_spinlock; map->unlock = config->unlock;
map->unlock = regmap_unlock_spinlock; map->lock_arg = config->lock_arg;
} else { } else {
mutex_init(&map->mutex); if (bus->fast_io) {
map->lock = regmap_lock_mutex; spin_lock_init(&map->spinlock);
map->unlock = regmap_unlock_mutex; map->lock = regmap_lock_spinlock;
map->unlock = regmap_unlock_spinlock;
} else {
mutex_init(&map->mutex);
map->lock = regmap_lock_mutex;
map->unlock = regmap_unlock_mutex;
}
map->lock_arg = map;
} }
map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8); map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
map->format.pad_bytes = config->pad_bits / 8; map->format.pad_bytes = config->pad_bits / 8;
@ -939,11 +950,11 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
if (reg % map->reg_stride) if (reg % map->reg_stride)
return -EINVAL; return -EINVAL;
map->lock(map); map->lock(map->lock_arg);
ret = _regmap_write(map, reg, val); ret = _regmap_write(map, reg, val);
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
@ -975,11 +986,11 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
if (reg % map->reg_stride) if (reg % map->reg_stride)
return -EINVAL; return -EINVAL;
map->lock(map); map->lock(map->lock_arg);
ret = _regmap_raw_write(map, reg, val, val_len); ret = _regmap_raw_write(map, reg, val, val_len);
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
@ -1011,7 +1022,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
if (reg % map->reg_stride) if (reg % map->reg_stride)
return -EINVAL; return -EINVAL;
map->lock(map); map->lock(map->lock_arg);
/* No formatting is require if val_byte is 1 */ /* No formatting is require if val_byte is 1 */
if (val_bytes == 1) { if (val_bytes == 1) {
@ -1047,7 +1058,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
kfree(wval); kfree(wval);
out: out:
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(regmap_bulk_write); EXPORT_SYMBOL_GPL(regmap_bulk_write);
@ -1137,11 +1148,11 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
if (reg % map->reg_stride) if (reg % map->reg_stride)
return -EINVAL; return -EINVAL;
map->lock(map); map->lock(map->lock_arg);
ret = _regmap_read(map, reg, val); ret = _regmap_read(map, reg, val);
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
@ -1171,7 +1182,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
if (reg % map->reg_stride) if (reg % map->reg_stride)
return -EINVAL; return -EINVAL;
map->lock(map); map->lock(map->lock_arg);
if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass || if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
map->cache_type == REGCACHE_NONE) { map->cache_type == REGCACHE_NONE) {
@ -1193,7 +1204,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
} }
out: out:
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
@ -1300,9 +1311,9 @@ int regmap_update_bits(struct regmap *map, unsigned int reg,
bool change; bool change;
int ret; int ret;
map->lock(map); map->lock(map->lock_arg);
ret = _regmap_update_bits(map, reg, mask, val, &change); ret = _regmap_update_bits(map, reg, mask, val, &change);
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
@ -1326,9 +1337,9 @@ int regmap_update_bits_check(struct regmap *map, unsigned int reg,
{ {
int ret; int ret;
map->lock(map); map->lock(map->lock_arg);
ret = _regmap_update_bits(map, reg, mask, val, change); ret = _regmap_update_bits(map, reg, mask, val, change);
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(regmap_update_bits_check); EXPORT_SYMBOL_GPL(regmap_update_bits_check);
@ -1357,7 +1368,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
if (map->patch) if (map->patch)
return -EBUSY; return -EBUSY;
map->lock(map); map->lock(map->lock_arg);
bypass = map->cache_bypass; bypass = map->cache_bypass;
@ -1385,7 +1396,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
out: out:
map->cache_bypass = bypass; map->cache_bypass = bypass;
map->unlock(map); map->unlock(map->lock_arg);
return ret; return ret;
} }

View File

@ -54,6 +54,9 @@ enum regmap_endian {
REGMAP_ENDIAN_NATIVE, REGMAP_ENDIAN_NATIVE,
}; };
typedef void (*regmap_lock)(void *);
typedef void (*regmap_unlock)(void *);
/** /**
* Configuration for the register map of a device. * Configuration for the register map of a device.
* *
@ -76,6 +79,12 @@ enum regmap_endian {
* @precious_reg: Optional callback returning true if the rgister * @precious_reg: Optional callback returning true if the rgister
* should not be read outside of a call from the driver * should not be read outside of a call from the driver
* (eg, a clear on read interrupt status register). * (eg, a clear on read interrupt status register).
* @lock: Optional lock callback (overrides regmap's default lock
* function, based on spinlock or mutex).
* @unlock: As above for unlocking.
* @lock_arg: this field is passed as the only argument of lock/unlock
* functions (ignored in case regular lock/unlock functions
* are not overridden).
* *
* @max_register: Optional, specifies the maximum valid register index. * @max_register: Optional, specifies the maximum valid register index.
* @reg_defaults: Power on reset values for registers (for use with * @reg_defaults: Power on reset values for registers (for use with
@ -117,6 +126,9 @@ struct regmap_config {
bool (*readable_reg)(struct device *dev, unsigned int reg); bool (*readable_reg)(struct device *dev, unsigned int reg);
bool (*volatile_reg)(struct device *dev, unsigned int reg); bool (*volatile_reg)(struct device *dev, unsigned int reg);
bool (*precious_reg)(struct device *dev, unsigned int reg); bool (*precious_reg)(struct device *dev, unsigned int reg);
regmap_lock lock;
regmap_unlock unlock;
void *lock_arg;
unsigned int max_register; unsigned int max_register;
const struct reg_default *reg_defaults; const struct reg_default *reg_defaults;
@ -182,7 +194,9 @@ typedef void (*regmap_hw_free_context)(void *context);
* Description of a hardware bus for the register map infrastructure. * Description of a hardware bus for the register map infrastructure.
* *
* @fast_io: Register IO is fast. Use a spinlock instead of a mutex * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
* to perform locking. * to perform locking. This field is ignored if custom lock/unlock
* functions are used (see fields lock/unlock of
* struct regmap_config).
* @write: Write operation. * @write: Write operation.
* @gather_write: Write operation with split register/value, return -ENOTSUPP * @gather_write: Write operation with split register/value, return -ENOTSUPP
* if not implemented on a given device. * if not implemented on a given device.