xref: /openbmc/linux/drivers/base/regmap/regmap-sdw.c (revision 726ccdba)
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 <linux/types.h>
10 #include "internal.h"
11 
12 static int regmap_sdw_write(void *context, const void *val_buf, size_t val_size)
13 {
14 	struct device *dev = context;
15 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
16 	/* First word of buffer contains the destination address */
17 	u32 addr = le32_to_cpu(*(const __le32 *)val_buf);
18 	const u8 *val = val_buf;
19 
20 	return sdw_nwrite_no_pm(slave, addr, val_size - sizeof(addr), val + sizeof(addr));
21 }
22 
23 static int regmap_sdw_gather_write(void *context,
24 				   const void *reg_buf, size_t reg_size,
25 				   const void *val_buf, size_t val_size)
26 {
27 	struct device *dev = context;
28 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
29 	u32 addr = le32_to_cpu(*(const __le32 *)reg_buf);
30 
31 	return sdw_nwrite_no_pm(slave, addr, val_size, val_buf);
32 }
33 
34 static int regmap_sdw_read(void *context,
35 			   const void *reg_buf, size_t reg_size,
36 			   void *val_buf, size_t val_size)
37 {
38 	struct device *dev = context;
39 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
40 	u32 addr = le32_to_cpu(*(const __le32 *)reg_buf);
41 
42 	return sdw_nread_no_pm(slave, addr, val_size, val_buf);
43 }
44 
45 static const struct regmap_bus regmap_sdw = {
46 	.write = regmap_sdw_write,
47 	.gather_write = regmap_sdw_gather_write,
48 	.read = regmap_sdw_read,
49 	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
50 	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
51 };
52 
53 static int regmap_sdw_config_check(const struct regmap_config *config)
54 {
55 	/* Register addresses are 32 bits wide */
56 	if (config->reg_bits != 32)
57 		return -ENOTSUPP;
58 
59 	if (config->pad_bits != 0)
60 		return -ENOTSUPP;
61 
62 	return 0;
63 }
64 
65 struct regmap *__regmap_init_sdw(struct sdw_slave *sdw,
66 				 const struct regmap_config *config,
67 				 struct lock_class_key *lock_key,
68 				 const char *lock_name)
69 {
70 	int ret;
71 
72 	ret = regmap_sdw_config_check(config);
73 	if (ret)
74 		return ERR_PTR(ret);
75 
76 	return __regmap_init(&sdw->dev, &regmap_sdw,
77 			&sdw->dev, config, lock_key, lock_name);
78 }
79 EXPORT_SYMBOL_GPL(__regmap_init_sdw);
80 
81 struct regmap *__devm_regmap_init_sdw(struct sdw_slave *sdw,
82 				      const struct regmap_config *config,
83 				      struct lock_class_key *lock_key,
84 				      const char *lock_name)
85 {
86 	int ret;
87 
88 	ret = regmap_sdw_config_check(config);
89 	if (ret)
90 		return ERR_PTR(ret);
91 
92 	return __devm_regmap_init(&sdw->dev, &regmap_sdw,
93 			&sdw->dev, config, lock_key, lock_name);
94 }
95 EXPORT_SYMBOL_GPL(__devm_regmap_init_sdw);
96 
97 MODULE_DESCRIPTION("Regmap SoundWire Module");
98 MODULE_LICENSE("GPL v2");
99