1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/i2c.h>
15#include <linux/spi/spi.h>
16#include <linux/regmap.h>
17#include <linux/export.h>
18#include <sound/soc.h>
19
20#include <trace/events/asoc.h>
21
22#ifdef CONFIG_REGMAP
23static int hw_write(struct snd_soc_codec *codec, unsigned int reg,
24 unsigned int value)
25{
26 int ret;
27
28 if (!snd_soc_codec_volatile_register(codec, reg) &&
29 reg < codec->driver->reg_cache_size &&
30 !codec->cache_bypass) {
31 ret = snd_soc_cache_write(codec, reg, value);
32 if (ret < 0)
33 return -1;
34 }
35
36 if (codec->cache_only) {
37 codec->cache_sync = 1;
38 return 0;
39 }
40
41 return regmap_write(codec->control_data, reg, value);
42}
43
44static unsigned int hw_read(struct snd_soc_codec *codec, unsigned int reg)
45{
46 int ret;
47 unsigned int val;
48
49 if (reg >= codec->driver->reg_cache_size ||
50 snd_soc_codec_volatile_register(codec, reg) ||
51 codec->cache_bypass) {
52 if (codec->cache_only)
53 return -1;
54
55 ret = regmap_read(codec->control_data, reg, &val);
56 if (ret == 0)
57 return val;
58 else
59 return -1;
60 }
61
62 ret = snd_soc_cache_read(codec, reg, &val);
63 if (ret < 0)
64 return -1;
65 return val;
66}
67
68
69
70
71
72
73
74
75
76static int snd_soc_hw_bulk_write_raw(struct snd_soc_codec *codec,
77 unsigned int reg,
78 const void *data, size_t len)
79{
80
81
82
83
84
85 if (!codec->cache_bypass
86 && !snd_soc_codec_volatile_register(codec, reg)
87 && reg < codec->driver->reg_cache_size)
88 return -EINVAL;
89
90 return regmap_raw_write(codec->control_data, reg, data, len);
91}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
113 int addr_bits, int data_bits,
114 enum snd_soc_control_type control)
115{
116 struct regmap_config config;
117 int ret;
118
119 memset(&config, 0, sizeof(config));
120 codec->write = hw_write;
121 codec->read = hw_read;
122 codec->bulk_write_raw = snd_soc_hw_bulk_write_raw;
123
124 config.reg_bits = addr_bits;
125 config.val_bits = data_bits;
126
127 switch (control) {
128#if defined(CONFIG_REGMAP_I2C) || defined(CONFIG_REGMAP_I2C_MODULE)
129 case SND_SOC_I2C:
130 codec->control_data = regmap_init_i2c(to_i2c_client(codec->dev),
131 &config);
132 break;
133#endif
134
135#if defined(CONFIG_REGMAP_SPI) || defined(CONFIG_REGMAP_SPI_MODULE)
136 case SND_SOC_SPI:
137 codec->control_data = regmap_init_spi(to_spi_device(codec->dev),
138 &config);
139 break;
140#endif
141
142 case SND_SOC_REGMAP:
143
144 codec->using_regmap = true;
145 if (!codec->control_data)
146 codec->control_data = dev_get_regmap(codec->dev, NULL);
147
148 if (codec->control_data) {
149 ret = regmap_get_val_bytes(codec->control_data);
150
151
152 if (ret > 0)
153 codec->val_bytes = ret;
154 }
155 break;
156
157 default:
158 return -EINVAL;
159 }
160
161 if (IS_ERR(codec->control_data))
162 return PTR_ERR(codec->control_data);
163
164 return 0;
165}
166EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
167#else
168int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
169 int addr_bits, int data_bits,
170 enum snd_soc_control_type control)
171{
172 return -ENOTSUPP;
173}
174EXPORT_SYMBOL_GPL(snd_soc_codec_set_cache_io);
175#endif
176