| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (c) 2017, Linaro Ltd. |
| 3 | |
| 4 | #include <linux/regmap.h> |
| 5 | #include <linux/slimbus.h> |
| 6 | #include <linux/module.h> |
| 7 | |
| 8 | #include "internal.h" |
| 9 | |
| 10 | static int regmap_slimbus_write(void *context, const void *data, size_t count) |
| 11 | { |
| 12 | struct slim_device *sdev = context; |
| 13 | |
| 14 | return slim_write(sdev, addr: *(u16 *)data, count: count - 2, val: (u8 *)data + 2); |
| 15 | } |
| 16 | |
| 17 | static int regmap_slimbus_read(void *context, const void *reg, size_t reg_size, |
| 18 | void *val, size_t val_size) |
| 19 | { |
| 20 | struct slim_device *sdev = context; |
| 21 | |
| 22 | return slim_read(sdev, addr: *(u16 *)reg, count: val_size, val); |
| 23 | } |
| 24 | |
| 25 | static const struct regmap_bus regmap_slimbus_bus = { |
| 26 | .write = regmap_slimbus_write, |
| 27 | .read = regmap_slimbus_read, |
| 28 | .reg_format_endian_default = REGMAP_ENDIAN_LITTLE, |
| 29 | .val_format_endian_default = REGMAP_ENDIAN_LITTLE, |
| 30 | }; |
| 31 | |
| 32 | static const struct regmap_bus *regmap_get_slimbus(struct slim_device *slim, |
| 33 | const struct regmap_config *config) |
| 34 | { |
| 35 | if (config->val_bits == 8 && config->reg_bits == 16) |
| 36 | return ®map_slimbus_bus; |
| 37 | |
| 38 | return ERR_PTR(error: -ENOTSUPP); |
| 39 | } |
| 40 | |
| 41 | struct regmap *__regmap_init_slimbus(struct slim_device *slimbus, |
| 42 | const struct regmap_config *config, |
| 43 | struct lock_class_key *lock_key, |
| 44 | const char *lock_name) |
| 45 | { |
| 46 | const struct regmap_bus *bus = regmap_get_slimbus(slim: slimbus, config); |
| 47 | |
| 48 | if (IS_ERR(ptr: bus)) |
| 49 | return ERR_CAST(ptr: bus); |
| 50 | |
| 51 | return __regmap_init(dev: &slimbus->dev, bus, bus_context: slimbus, config, lock_key, lock_name); |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(__regmap_init_slimbus); |
| 54 | |
| 55 | struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus, |
| 56 | const struct regmap_config *config, |
| 57 | struct lock_class_key *lock_key, |
| 58 | const char *lock_name) |
| 59 | { |
| 60 | const struct regmap_bus *bus = regmap_get_slimbus(slim: slimbus, config); |
| 61 | |
| 62 | if (IS_ERR(ptr: bus)) |
| 63 | return ERR_CAST(ptr: bus); |
| 64 | |
| 65 | return __devm_regmap_init(dev: &slimbus->dev, bus, bus_context: slimbus, config, lock_key, lock_name); |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(__devm_regmap_init_slimbus); |
| 68 | |
| 69 | MODULE_DESCRIPTION("Register map access API - SLIMbus support" ); |
| 70 | MODULE_LICENSE("GPL v2" ); |
| 71 | |