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