blob: 966de8a136d9004247b94d5a99bb5f8af1a26ac4 [file] [log] [blame]
Vinod Koul7c22ce62018-01-08 15:50:59 +05301// SPDX-License-Identifier: GPL-2.0
2// Copyright(c) 2015-17 Intel Corporation.
3
4#include <linux/device.h>
Pierre-Louis Bossartd9a500b2020-11-25 21:01:28 +08005#include <linux/errno.h>
Vinod Koul7c22ce62018-01-08 15:50:59 +05306#include <linux/module.h>
Pierre-Louis Bossartd9a500b2020-11-25 21:01:28 +08007#include <linux/regmap.h>
Vinod Koul7c22ce62018-01-08 15:50:59 +05308#include <linux/soundwire/sdw.h>
9#include "internal.h"
10
11static 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
Bard Liaod288a5712021-01-22 15:06:30 +080016 return sdw_write_no_pm(slave, reg, val);
Vinod Koul7c22ce62018-01-08 15:50:59 +053017}
18
19static 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
Bard Liaod288a5712021-01-22 15:06:30 +080025 read = sdw_read_no_pm(slave, reg);
Vinod Koul7c22ce62018-01-08 15:50:59 +053026 if (read < 0)
27 return read;
28
29 *val = read;
30 return 0;
31}
32
33static 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
40static 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
56struct 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}
70EXPORT_SYMBOL_GPL(__regmap_init_sdw);
71
72struct 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}
86EXPORT_SYMBOL_GPL(__devm_regmap_init_sdw);
87
88MODULE_DESCRIPTION("Regmap SoundWire Module");
89MODULE_LICENSE("GPL v2");