1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright(c) 2015-17 Intel Corporation. 3 4 #include <linux/device.h> 5 #include <linux/module.h> 6 #include <linux/soundwire/sdw.h> 7 #include "internal.h" 8 9 static int regmap_sdw_write(void *context, unsigned int reg, unsigned int val) 10 { 11 struct device *dev = context; 12 struct sdw_slave *slave = dev_to_sdw_dev(dev); 13 14 return sdw_write(slave, reg, val); 15 } 16 17 static int regmap_sdw_read(void *context, unsigned int reg, unsigned int *val) 18 { 19 struct device *dev = context; 20 struct sdw_slave *slave = dev_to_sdw_dev(dev); 21 int read; 22 23 read = sdw_read(slave, reg); 24 if (read < 0) 25 return read; 26 27 *val = read; 28 return 0; 29 } 30 31 static struct regmap_bus regmap_sdw = { 32 .reg_read = regmap_sdw_read, 33 .reg_write = regmap_sdw_write, 34 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE, 35 .val_format_endian_default = REGMAP_ENDIAN_LITTLE, 36 }; 37 38 static int regmap_sdw_config_check(const struct regmap_config *config) 39 { 40 /* All register are 8-bits wide as per MIPI Soundwire 1.0 Spec */ 41 if (config->val_bits != 8) 42 return -ENOTSUPP; 43 44 /* Registers are 32 bits wide */ 45 if (config->reg_bits != 32) 46 return -ENOTSUPP; 47 48 if (config->pad_bits != 0) 49 return -ENOTSUPP; 50 51 return 0; 52 } 53 54 struct regmap *__regmap_init_sdw(struct sdw_slave *sdw, 55 const struct regmap_config *config, 56 struct lock_class_key *lock_key, 57 const char *lock_name) 58 { 59 int ret; 60 61 ret = regmap_sdw_config_check(config); 62 if (ret) 63 return ERR_PTR(ret); 64 65 return __regmap_init(&sdw->dev, ®map_sdw, 66 &sdw->dev, config, lock_key, lock_name); 67 } 68 EXPORT_SYMBOL_GPL(__regmap_init_sdw); 69 70 struct regmap *__devm_regmap_init_sdw(struct sdw_slave *sdw, 71 const struct regmap_config *config, 72 struct lock_class_key *lock_key, 73 const char *lock_name) 74 { 75 int ret; 76 77 ret = regmap_sdw_config_check(config); 78 if (ret) 79 return ERR_PTR(ret); 80 81 return __devm_regmap_init(&sdw->dev, ®map_sdw, 82 &sdw->dev, config, lock_key, lock_name); 83 } 84 EXPORT_SYMBOL_GPL(__devm_regmap_init_sdw); 85 86 MODULE_DESCRIPTION("Regmap SoundWire Module"); 87 MODULE_LICENSE("GPL v2"); 88